今天终于有时间来实践一下Java socket的使用方法了,需要注意几个问题:
(1)、使用socket.getInputStream 读取数据时,该方法是阻塞方法,即如果管道流中没有数据时,使用inputStream.read(byte)是会一直阻塞,所以如果想要使用inputStream.read(new byte[]) > 0 来结束
循环是行不通的;
(2)、由于我的目的是不停的交互数据,所以不能IO流关闭,也不能把socket会话关闭,否则数据交换就会停止。
贴代码做记录:
1、服务端代码:
package com.server; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import com.base.Constant; public class ServerMain { /** * @param args */ public static void main(String[] args) { ServerSocket serverSocket = null; System.out.println("ServerSocket Begin........"); int num = 0; try { serverSocket = new ServerSocket(Constant.PORT); //使用循环方式一直等待客户端的连接 while(true){ num ++; Socket accept = serverSocket.accept(); //启动一个新的线程,接管与当前客户端的交互会话 new Thread(new ServerThread(accept),"Client "+num).start(); } } catch (IOException e) { e.printStackTrace(); } finally{ try { serverSocket.close(); System.out.println("----> serverSocket closed."); } catch (IOException e) { e.printStackTrace(); } } } } /** * @author JCC * 服务器处理客户端会话的线程 */ class ServerThread implements Runnable { Socket socket = null; public ServerThread(Socket socket){ System.out.println("Create a new ServerThread..."); this.socket = socket; } @Override public void run() { InputStream in = null; OutputStream out = null; try { in = socket.getInputStream(); out = socket.getOutputStream(); //使用循环的方式,不停的与客户端交互会话 while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } //处理客户端发来的数据 doRead(in); System.out.println("send Message to client."); //发送数据回客户端 doWrite(out); } } catch (IOException e) { e.printStackTrace(); } finally{ try { in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 读取数据 * @param in * @return */ public boolean doRead(InputStream in){ //引用关系,不要在此处关闭流 try { byte[] bytes = new byte[in.available()]; in.read(bytes); System.out.println("line:"+new String(bytes).trim()); } catch (IOException e) { e.printStackTrace(); } return true; } /** * 写入数据 * @param out * @return */ public boolean doWrite(OutputStream out){ //引用关系,不要在此处关闭流 try { out.write("welcome you client.".getBytes()); out.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } }
2、客户端代码:
package com.client; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import com.base.Constant; class ClientMain { /** * @param args */ public static void main(String[] args) { Socket socket = null; System.out.println("ClientSocket Begin........"); try { for(int i = 0;i<5;i++){ socket = new Socket(Constant.SERVER_IP,Constant.PORT); new Thread(new ClientThread(socket,i),"ClientThread "+i).start(); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * 客户端线程 * @author JCC * */ class ClientThread implements Runnable{ Socket socket = null; int id = 0; public ClientThread(Socket socket,int id){ this.socket = socket; this.id = id; } @Override public void run() { OutputStream out = null; InputStream in = null; System.out.println("Begin to Chat to server..."); try { out = socket.getOutputStream(); in = socket.getInputStream(); //循环发送与服务端不停的交互数据 while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } doWrite(out); System.out.println("begin read message from server."); doRead(in); } } catch (IOException e) { e.printStackTrace(); } finally{ try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 读取服务端数据 * @param in * @return */ public static boolean doRead(InputStream in){ //引用关系,不要在此处关闭流 byte[] bytes = new byte[1024]; try { in.read(bytes); System.out.println("line:"+new String(bytes).trim()); } catch (IOException e) { e.printStackTrace(); } return true; } /** * 发送数据到服务端 * @param out * @return */ public boolean doWrite(OutputStream out){ //引用关系,不要在此处关闭流 String line = "Hello server, I am client = "+id +"\n"; line = line +"I want you to do something for me"; try { out.write(line.getBytes()); out.flush(); } catch (IOException e) { e.printStackTrace(); } return true; } }
3、常量类:
package com.base; public interface Constant { public static final String SERVER_IP = "192.168.1.44"; public static final int PORT = 4567; }
执行结果:
(1)服务端结果:
ServerSocket Begin........ Create a new ServerThread... Create a new ServerThread... Create a new ServerThread... Create a new ServerThread... Create a new ServerThread... line: send Message to client. line: send Message to client. line: send Message to client. line:Hello server, I am client = 3 I want you to do something for me send Message to client. line:Hello server, I am client = 4 I want you to do something for me send Message to client. line:Hello server, I am client = 0 I want you to do something for me send Message to client. line:Hello server, I am client = 1 I want you to do something for me send Message to client. line:Hello server, I am client = 2 I want you to do something for me send Message to client. line: send Message to client. line: send Message to client. line:Hello server, I am client = 0 I want you to do something for me send Message to client. line:Hello server, I am client = 1 I want you to do something for me send Message to client. line:Hello server, I am client = 2 I want you to do something for me send Message to client. line:Hello server, I am client = 3 I want you to do something for me send Message to client. line:Hello server, I am client = 4 I want you to do something for me send Message to client. line:Hello server, I am client = 0 I want you to do something for me send Message to client.
(2)客户端结果:
ClientSocket Begin........ Begin to Chat to server... Begin to Chat to server... Begin to Chat to server... Begin to Chat to server... Begin to Chat to server... begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server. line:welcome you client. begin read message from server.