1.单线程TCP服务端
1 public class TcpChatServer { 2 3 private Integer port=8000; 4 private ServerSocket serverSocket; 5 6 public static void main(String[] args)throws Exception { 7 8 new TcpChatServer().service(); 9 } 10 11 public TcpChatServer() throws Exception { 12 //启动一个服务监听一个指定的端口port 13 serverSocket=new ServerSocket(port); 14 System.out.println("service started!"); 15 } 16 17 private void service(){ 18 while (true) { 19 Socket socket=null; 20 try { 21 //serversocket开始监听服务,客户端和服务端交互的socket 22 socket=serverSocket.accept(); 23 System.out.println(String.format("client:%s port:%s",socket.getInetAddress(),socket.getPort())); 24 //获取客户端的数据对象 25 BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); 26 //写入客户端的数据对象 27 PrintWriter writer=new PrintWriter(socket.getOutputStream(),true); 28 String word; 29 while ((word=reader.readLine())!=null) { 30 //相应请求 31 writer.println(reply(word)); 32 //如果用户输入bye则退出循环 33 if(word.equals("bye")){ 34 break; 35 } 36 } 37 } catch (IOException e) { 38 System.err.println(e.getMessage()); 39 }finally{ 40 //一定要关闭socket 41 if(socket!=null){ 42 try { 43 socket.close(); 44 } catch (IOException e) { 45 System.err.println(e.getMessage()); 46 } 47 } 48 } 49 } 50 } 51 52 //回复相应 53 private String reply(String word){ 54 return "you said :"+word; 55 } 56 }
运行...在cmd下访问....使用telnet访问....
但是有一个问题...如果再开一个telnet访问的话输入什么都是没有效果的...只有等到第一个结束之后才会对第二个访问的telnet进行回复,如下图.
搞一个多线程的Socket服务端.
1 public class TcpThreadChatServer { 2 3 private Integer port=8000; 4 private ServerSocket serverSocket; 5 6 public static void main(String[] args)throws Exception { 7 8 new TcpThreadChatServer().service(); 9 } 10 11 public TcpThreadChatServer() throws Exception { 12 //启动一个服务监听一个指定的端口port 13 serverSocket=new ServerSocket(port); 14 System.out.println("service started!"); 15 16 } 17 18 private void service(){ 19 20 while (true) { 21 Socket socket=null; 22 23 try { 24 //serversocket开始监听服务,客户端和服务端交互的socket 25 socket=serverSocket.accept(); 26 //对于每次请求,多线程应答 可以开多个可以对多个客户端进行回应. 27 new Thread(new Worker(socket)).start(); 28 } catch (IOException e) { 29 System.err.println(e.getMessage()); 30 } 31 } 32 } 33 34 class Worker implements Runnable{ 35 public Worker(Socket socket) { 36 this.socket = socket; 37 } 38 private Socket socket; 39 40 @Override 41 public void run() { 42 43 try { 44 System.out.println(String.format("client:%s port:%s",socket.getInetAddress(),socket.getPort())); 45 //获取客户端的数据对象 46 BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); 47 //写入客户端的数据对象 48 PrintWriter writer=new PrintWriter(socket.getOutputStream(),true); 49 String word; 50 while ((word=reader.readLine())!=null) { 51 //相应请求 52 writer.println(reply(word)); 53 //如果用户输入bye则退出循环 54 if(word.equals("bye")){ 55 break; 56 } 57 } 58 } catch (Exception e) { 59 System.err.println(e.getMessage()); 60 }finally{ 61 if(socket!=null){ 62 try { 63 socket.close(); 64 } catch (IOException e) { 65 System.err.println(e.getMessage()); 66 } 67 } 68 } 69 } 70 } 71 72 //回复相应 73 private String reply(String word){ 74 return "you said :"+word; 75 } 76 }
可以开启多个telnet访问 并同时处理反馈.....
可以写对应的如下客户端...通过控制台访问该服务端,不再使用telnet...
1 public class TcpChatClient {//客户端和服务端通话都是过socket 2 3 private Socket socket; 4 private String host="localhost";//要请求的服务器地址 5 private Integer port=8000; 6 7 public TcpChatClient() throws Exception { 8 //初始化一个客户端socket 9 socket=new Socket(host, port); 10 System.out.println("connect ok!"); 11 } 12 13 public static void main(String[] args) throws Exception { 14 15 new TcpChatClient().say(); 16 } 17 18 private void say() throws Exception { 19 //两个reader对象 20 //本地的system.in 的reader 21 BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in)); 22 //和服务器交互的writer【PrintWriter 的自动flush 功能】 这个ture参数非常重要 . 23 PrintWriter writer=new PrintWriter(socket.getOutputStream(),true); 24 //和服务器交互的reader 25 BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); 26 27 String word; 28 String readLine; 29 while ((word=localReader.readLine())!=null){ 30 //请求服务端来获取数据 31 writer.println(word); 32 //打印到控制台上 33 readLine = reader.readLine(); 34 System.out.println(readLine); 35 36 //如果用户输入bye则退出循环 37 if (word.equals("bye")){ 38 socket.close(); 39 break; 40 } 41 } 42 } 43 }