Java Socket通讯一个相当广泛的使用,其实在有些时候我们在遇到困难的时候还是需要去找一找相关代码的介绍。最近工作需要,做了一点Java Socket通讯的东西,积累一点经验,与大家共分享。
其中通过短连接方式接收Java Socket通讯返回消息,死循环,分别判断接收和超时,来确定连接连接状况。通过字节流的形式获取socket返回消息,主要是因为通过readline的方式,在我们系统通讯中存在异常。
具体客户端代码如下:
- import java.net.*;
- import java.io.*;
- import org.apache.log4j.Logger;
- public class SocketClient {
- static Logger log = Logger.getLogger(SocketClient.class.
getName()); //日志记录信息- private String hostName;
- private int portNum;
- private int delaySecond; // 发文接收返回报文延时
- public SocketClient() {
- this.hostName = "192.168.0.1";
- this.portNum = 7000;
- this.delaySecond = 50000;
- pFileOp = null;
- }
- private Socket getSocket() {
- Socket socket = null;
- try {
- socket = new Socket(hostName, portNum);
- } catch (UnknownHostException e) {
- System.out.println("-->未知的主机名:" + hostName + " 异常");
- } catch (IOException e) {
- System.out.println("-hostName=" + hostName + " portNum="
- + portNum + "---->IO异常错误" + e.getMessage());
- }
- return socket;
- }
- public String sendMessage(String strMessage) {
- String str = "";
- String serverString = "";
- Socket socket;
- try {
- socket = getSocket();
- // socket.setKeepAlive(true);
- if (socket == null) { // 未能得到指定的Socket对象,Socket通讯为空
- return "0001";
- }
- PrintWriter out = new PrintWriter(socket.getOutputStream());
- //log.info("---->发送报文="+strMessage);
- out.println(strMessage);
- out.flush();
- BufferedReader in = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- long sendTime = System.currentTimeMillis();
- long receiveTime = System.currentTimeMillis();
- boolean received = false; // 成功接收报文
- boolean delayTooLong = false;
- serverString = null;
- while (!received && !delayTooLong) {
- if (socket.getInputStream().available() > 0) {
- // serverString = in.readLine();
- char tagChar[];
- tagChar = new char[1024];
- int len;
- String temp;
- String rev = "";
- if ((len = in.read(tagChar)) != -1) {
- temp = new String(tagChar, 0, len);
- rev += temp;
- temp = null;
- }
- serverString = rev;
- }
- receiveTime = System.currentTimeMillis();
- if (serverString != null)
- received = true; // 字符串不为空,接收成功
- if ((receiveTime - sendTime) > delaySecond)
- delayTooLong = true; // 接收等待时间过长,超时
- }
- in.close();
- out.close();
- str=serverString;
- if (delayTooLong) str="2190"; //超时标志为真,返回超时码
- if (!received) str ="2190";
- socket.close();
- } catch (UnknownHostException e) {
- log.error("---->出现未知主机错误! 主机信息=" + this.hostName +
" 端口号="- + this.portNum + " 出错信息=" + e.getMessage());
- str = "2191";
- // System.exit(1);
- } catch (IOException e) {
- log.error("---->出现IO异常! 主机信息=" + this.hostName +
" 端口号="- + this.portNum + " 出错信息=" + e.getMessage());
- e.printStackTrace();
- str = "2191";
- } catch (Exception e) {
- str="2177";
- log.error("---->出现未知异常" + e.getMessage());
- } finally {
- socket = null;
- str.trim();
- //log.info("--->返回的socket通讯字符串="+str);
- return str;
- }
- }
- }
以上就是对Java Socket通讯的详细介绍。希望大家有所帮助。
【编辑推荐】