C/S通信,实现多重及时连接通信

来源:互联网 发布:联通拨号软件 编辑:程序博客网 时间:2024/05/22 08:50

Server端

Code:
  1. import java.io.*;   
  2. import java.net.*;   
  3. public class Server {   
  4.     public static int num=0;    //用于记录连接的数目   
  5.     static ServerSocket ss;     //服务器端套接字   
  6.     static int len=0;           //记录每次收到数据的长度   
  7.     public static void main(String[] args) {   
  8.             try {   
  9.                 ss = new ServerSocket(3471);   
  10.                 System.out.println("服务器启动完毕,等待客户端连接!");   
  11.                 Runnable run = new Runnable() {   
  12.                     public void run() {   
  13.                         while(true) {   
  14.                             try {   
  15.                                 Thread.sleep(10);   
  16.                                 final Socket client = ss.accept();   //接受客户端的连接   
  17.                                 System.out.println("客户端"+(++num)+"连接成功!");   
  18.                                 final InputStream in = client.getInputStream();  //获取客户端输入流   
  19.                                 final byte[] b = new byte[1024];   
  20.                             Runnable innerRun = new Runnable() {   
  21.                                     public void run() {   
  22.                                         while(true) {   
  23.                                             try {   
  24.                                                 Thread.sleep(10);   
  25.                                                 if((len=in.read(b))>0) {   
  26.                                                     String str = new String(b,0,len-1);   
  27.                                                     //下面应实现转发,服务器相当于中转站,在此只做输出查看   
  28.                                                     System.out.println("来自客户端"+client.getInetAddress().getHostAddress()+"的消息:"+str);   
  29.                                                 }   
  30.                                             } catch(IOException e) {   
  31.                                                 e.printStackTrace();   
  32.                                             } catch(InterruptedException e) {   
  33.                                                 e.printStackTrace();   
  34.                                             }   
  35.                                         }   
  36.                                     }   
  37.                                 };   
  38.                                 new Thread(innerRun).start();   
  39.                             } catch(IOException e) {   
  40.                                 e.printStackTrace();   
  41.                             } catch(InterruptedException e) {   
  42.                                 e.printStackTrace();   
  43.                             }   
  44.                         }   
  45.                     }   
  46.                 };   
  47.                 new Thread(run).start();   
  48.             } catch (IOException e) {   
  49.                 e.printStackTrace();   
  50.             }   
  51.     }   
  52. }  

Client端

Code:
  1. import java.io.*;   
  2. import java.net.*;   
  3. public class Client {   
  4.     public static void main(String[] args) {   
  5.         try {   
  6.             Socket s = new Socket("ip地址",3471);    //ip地址是要连接的服务器的ip地址   
  7.             OutputStream out = s.getOutputStream();   
  8.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
  9.             String str = new String();    //向服务器发送消息   
  10.             while(true) {   
  11.                 str = br.readLine();   
  12.                 out.write((str+"/n").getBytes());   
  13.             }   
  14.         } catch (UnknownHostException e) {   
  15.             e.printStackTrace();   
  16.         } catch (IOException e) {   
  17.             e.printStackTrace();   
  18.         }   
  19.     }   
  20. }  

实现了服务器与多个客户端同时连接,无规律交互通信,要想真正做出多方通信,应该实现转发功能,服务器端确定后,只需要提供对方ip。在此过程中应该做了先发到服务器,服务器再发到指定地点的处理,但对用户来说只需要提供对方ip即可,无需知道服务器地址。需要进一步的完善。