java socket 实现多个一对一聊天

此程序能够实现同一网络下多个一对一聊天,必须服务器先启动 ,然后客户端启动并且服务器ip要填正确,并且每个客户端的自身编号必须唯一。

 

服务器端:

java socket 实现多个一对一聊天
package cn.com.test09;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Set;

public class t12 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       new ServerOb(8080);
    }

}
class ServerOb{
    private int port;
    ServerSocket ss;
    public ServerOb(int p){
        this.port=p;
        
        try {
            ss = new ServerSocket(port);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while(true){
            try {
                
                Socket soc = ss.accept();
                new Thread(new clientO(soc)).start();
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println(" ServerOb 构造函数  出错");
                e.printStackTrace();
            }
        }
    }
}
class clientO implements Runnable{
    public static HashMap<String, Socket> map= new HashMap<String, Socket>();
    private  String number = null;
    private Socket soc;
    private  DataInputStream in;
    private DataOutputStream out;
     public clientO(Socket s){
      this.soc=s;
      try {
        this.in=new DataInputStream(soc.getInputStream());
        this.out=new DataOutputStream(soc.getOutputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
     getKey();
     }
     public String getNumber(){
        
         try {
             number=in.readUTF();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return number;
     }
     public void  getKey(){
         map.put(getNumber(), soc);//得到帐号保存Map
         Set<String> ke = map.keySet();
        if(ke!=null)
         try {
             out.writeUTF(ke.toString());
             out.flush();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        
        while(true){
         getOtherNumber();
        }
        
    }
    public void  getOtherNumber(){
        try {
            String oth = in.readUTF();
            String value = in.readUTF();
            Socket other = map.get(oth);
            String str=number +"对你说:\r\n"+value;
            DataOutputStream asd = new DataOutputStream(other.getOutputStream());
            asd.writeUTF(str);
            asd.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
}
java socket 实现多个一对一聊天

 

客户端:

java socket 实现多个一对一聊天
package cn.com.test09;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class t13 {

    public static void main(String[] args) {
        
    new ClientOO("192.168.0.105",8080,"1");//服务器ip  服务器端口    自身编号
    }

}
class  ClientOO{
    private String path;
    private int port;
    private String name;
    private  DataInputStream in;
    private DataOutputStream out;
    public ClientOO(String path,int port,String name){
        this.path=path;
        this.port=port;
        this.name=name;
        try {
            Socket s=new Socket(path,port);
            this.in=new DataInputStream(s.getInputStream());
            this.out=new DataOutputStream(s.getOutputStream());
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        send();
        
        hhhhh();
    }
    public void send(){
        String s = null;
        try {
            out.writeUTF(name);
            out.flush();
            
            
            s=in.readUTF();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("在线列表:"+s);
    }
    public  void hhhhh(){
        new  MyServerReader1(in).start();
        new MyServerWriter1(out).start();
    }
}
class MyServerReader1 extends Thread {
    private DataInputStream dis;
    public MyServerReader1(DataInputStream dis) {
        this.dis = dis;
    }

    public void run() {
        String info;
        try {
           while (true) {
               // 如果对方,即客户端没有说话,那么就会阻塞到这里,
                // 这里的阻塞并不会影响到其他线程
               info = dis.readUTF();
                // 如果状态有阻塞变为非阻塞,那么就打印接受到的信息
               System.out.println("对方说: " + info);
                if (info.equals("bye")) {
                    System.out.println("对方下线,程序退出!");
                   System.exit(0);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

// 从键盘获得输入流并写入信息到客户端
class MyServerWriter1 extends Thread {
   private DataOutputStream dos;

    public MyServerWriter1(DataOutputStream dos) {
        this.dos = dos;
    }

    public void run() {
        // 读取键盘输入流
        InputStreamReader isr = new InputStreamReader(System.in);
        // 封装键盘输入流
        BufferedReader br = new BufferedReader(isr);
        String info;
        try {
            while (true) {
                info = br.readLine();
                dos.writeUTF(info);
                dos.flush();
                if (info.equals("bye")) {
                    System.out.println("自己下线,程序退出!");
                    System.exit(0);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
java socket 实现多个一对一聊天

 

java socket 实现多个一对一聊天,布布扣,bubuko.com

java socket 实现多个一对一聊天

上一篇:Java键盘读入方法


下一篇:java.lang.Exception: Socket bind failed 服务器端口冲突-->修改端口