BaseCode之socket工具类:SocketHelper.java

来源:互联网 发布:时时彩后三计划软件 编辑:程序博客网 时间:2024/05/28 11:48
import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.net.Socket;public class SocketHelper {    private String ip = null;    private int port = 0;    private Socket socket = null;    private DataOutputStream outputStream = null;    private DataInputStream inputStream = null;    public SocketHelper(String ip, int port) {        this.ip = ip;        this.port = port;    }    public void createConnection() throws Exception {        try {            socket = new Socket(ip, port);            socket.setKeepAlive(false);        } finally {            if (socket != null) {                socket.close();            }        }    }    public synchronized void sendMessage(String sendMessage) throws Exception {        try {            if (outputStream == null) {                outputStream = new DataOutputStream(socket.getOutputStream());            }            byte b[] = sendMessage.getBytes("GBK");            outputStream.write(b);            outputStream.flush();        } finally {            if (outputStream != null) {                outputStream.close();            }        }    }    public synchronized String getMessage() throws Exception {        return getMessage(-1);    }    public synchronized String getMessage(int readSize) throws Exception {        try {            if (inputStream == null) {                inputStream =                        new DataInputStream(                                new BufferedInputStream(                                        socket.getInputStream()));            }            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();            if (inputStream != null) {                if (readSize != -1 && readSize > 0) {                    for (int i = 0; i < readSize; i++) {                        int read = inputStream.read();                        if (read == -1) {                            break;                        } else {                            byteArrayOutputStream.write(read);                        }                    }                } else {                    while (true) {                        int read = inputStream.read();                        if (read <= 0) {                            break;                        } else {                            byteArrayOutputStream.write(read);                        }                    }                }                return new String(byteArrayOutputStream.toByteArray(), "GBK");            }        } finally {            if (inputStream != null) {                inputStream.close();            }        }        return null;    }    public void shutDownConnection() {        try {            if (outputStream != null) {                outputStream.close();            }            if (inputStream != null) {                inputStream.close();            }            if (socket != null) {                socket.shutdownInput();                socket.shutdownOutput();                socket.close();            }        } catch (Exception e) {        }    }    public static void main(String[] args) throws Exception {        String ip = "127.0.0.1";        int port = 8080;        String send = "待发送的信息";        SocketHelper socketHelper = null;        String recvMsg = null;        try{            socketHelper = new SocketHelper(ip, port);            socketHelper.createConnection();            socketHelper.sendMessage(send);            recvMsg = socketHelper.getMessage();        } finally {            socketHelper.shutDownConnection();        }        System.out.println("服务器返回信息:" + recvMsg);    }}

原创粉丝点击