The specific client code is as follows:
<ol class=" list-paddingleft-2"> <li><p>import java.net.*; </p></li> <li><p>import java.io.*; </p></li> <li><p>import org.apache.log4j.Logger; </p></li> <li><p>public class SocketClient { </p></li> <li><p>static Logger log = Logger.getLogger(SocketClient.class.<br>getName()); //日志记录信息 </p></li> <li><p>private String hostName; </p></li> <li><p>private int portNum; </p></li> <li><p>private int delaySecond; // 发文接收返回报文延时 </p></li> <li><p>public SocketClient() { </p></li> <li><p>this.hostName = "192.168.0.1"; </p></li> <li><p>this.portNum = 7000; </p></li> <li><p>this.delaySecond = 50000; </p></li> <li><p>pFileOp = null; </p></li> <li><p>} </p></li> <li><p>private Socket getSocket() { </p></li> <li><p>Socket socket = null; </p></li> <li><p>try { </p></li> <li><p>socket = new Socket(hostName, portNum); </p></li> <li><p>} catch (UnknownHostException e) { </p></li> <li><p>System.out.println("-->未知的主机名:" + hostName + " 异常"); </p></li> <li><p>} catch (IOException e) { </p></li> <li><p>System.out.println("-hostName=" + hostName + " portNum=" </p></li> <li><p>+ portNum + "---->IO异常错误" + e.getMessage()); </p></li> <li><p>} </p></li> <li><p>return socket; </p></li> <li><p>} </p></li> <li><p>public String sendMessage(String strMessage) { </p></li> <li><p>String str = ""; </p></li> <li><p>String serverString = ""; </p></li> <li><p>Socket socket; </p></li> <li><p>try { </p></li> <li><p>socket = getSocket(); </p></li> <li><p>// socket.setKeepAlive(true); </p></li> <li><p>if (socket == null) { // 未能得到指定的Socket对象,Socket通讯为空 </p></li> <li><p>return "0001"; </p></li> <li><p>} </p></li> <li><p>PrintWriter out = new PrintWriter(socket.getOutputStream()); </p></li> <li><p>//log.info("---->发送报文="+strMessage); </p></li> <li><p>out.println(strMessage); </p></li> <li><p>out.flush(); </p></li> <li><p>BufferedReader in = new BufferedReader(new InputStreamReader( </p></li> <li><p>socket.getInputStream())); </p></li> <li><p>long sendTime = System.currentTimeMillis(); </p></li> <li><p>long receiveTime = System.currentTimeMillis(); </p></li> <li><p>boolean received = false; // 成功接收报文 </p></li> <li><p>boolean delayTooLong = false; </p></li> <li><p>serverString = null; </p></li> <li><p>while (!received && !delayTooLong) { </p></li> <li><p>if (socket.getInputStream().available() > 0) { </p></li> <li><p>// serverString = in.readLine(); </p></li> <li><p>char tagChar[]; </p></li> <li><p>tagChar = new char[1024]; </p></li> <li><p>int len; </p></li> <li><p>String temp; </p></li> <li><p>String rev = ""; </p></li> <li><p>if ((len = in.read(tagChar)) != -1) { </p></li> <li><p>temp = new String(tagChar, 0, len); </p></li> <li><p>rev += temp; </p></li> <li><p>temp = null; </p></li> <li><p>} </p></li> <li><p>serverString = rev; </p></li> <li><p>} </p></li> <li><p>receiveTime = System.currentTimeMillis(); </p></li> <li><p>if (serverString != null) </p></li> <li><p>received = true; // 字符串不为空,接收成功 </p></li> <li><p>if ((receiveTime - sendTime) > delaySecond) </p></li> <li><p>delayTooLong = true; // 接收等待时间过长,超时 </p></li> <li><p>} </p></li> <li><p>in.close(); </p></li> <li><p>out.close(); </p></li> <li><p>str=serverString; </p></li> <li><p>if (delayTooLong) str="2190"; //超时标志为真,返回超时码 </p></li> <li><p>if (!received) str ="2190"; </p></li> <li><p>socket.close(); </p></li> <li><p>} catch (UnknownHostException e) { </p></li> <li><p>log.error("---->出现未知主机错误! 主机信息=" + this.hostName + <br>" 端口号=" </p></li> <li><p>+ this.portNum + " 出错信息=" + e.getMessage()); </p></li> <li><p>str = "2191"; </p></li> <li><p>// System.exit(1); </p></li> <li><p>} catch (IOException e) { </p></li> <li><p>log.error("---->出现IO异常! 主机信息=" + this.hostName + <br>" 端口号=" </p></li> <li><p>+ this.portNum + " 出错信息=" + e.getMessage()); </p></li> <li><p>e.printStackTrace(); </p></li> <li><p>str = "2191"; </p></li> <li><p>} catch (Exception e) { </p></li> <li><p>str="2177"; </p></li> <li><p>log.error("---->出现未知异常" + e.getMessage()); </p></li> <li><p>} finally { </p></li> <li><p>socket = null; </p></li> <li><p>str.trim(); </p></li> <li><p>//log.info("--->返回的socket通讯字符串="+str); </p></li> <li><p>return str; </p></li> <li><p>} </p></li> <li><p>} </p></li> <li><p>} </p></li> </ol>
The above is the detailed content of How to write code to implement communication client using Socket in Java. For more information, please follow other related articles on the PHP Chinese website!