此程序能够实现同一网络下多个一对一聊天,必须服务器先启动 ,然后客户端启动并且服务器ip要填正确,并且每个客户端的自身编号必须唯一。
服务器端:
packagecn.com.test09; importjava.io.DataInputStream; importjava.io.DataOutputStream; importjava.io.IOException; importjava.net.ServerSocket; importjava.net.Socket; importjava.util.HashMap; importjava.util.Set; public classt12 { public static voidmain(String[] args) { //TODO Auto-generated method stub new ServerOb(8080); } } classServerOb{ private intport; ServerSocket ss; public ServerOb(intp){ this.port=p; try{ ss = newServerSocket(port); } catch(IOException e1) { //TODO Auto-generated catch block e1.printStackTrace(); } while(true){ try{ Socket soc =ss.accept(); new Thread(newclientO(soc)).start(); } catch(IOException e) { //TODO Auto-generated catch block System.out.println(" ServerOb 构造函数 出错"); e.printStackTrace(); } } } } class clientO implementsRunnable{ public static HashMap<String, Socket> map= new HashMap<String, Socket>(); private String number = null; privateSocket soc; privateDataInputStream in; privateDataOutputStream out; publicclientO(Socket s){ this.soc=s; try{ this.in=newDataInputStream(soc.getInputStream()); this.out=newDataOutputStream(soc.getOutputStream()); } catch(IOException e) { //TODO Auto-generated catch block e.printStackTrace(); } getKey(); } publicString getNumber(){ try{ number=in.readUTF(); } catch(IOException e) { //TODO Auto-generated catch block e.printStackTrace(); } returnnumber; } public voidgetKey(){ 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 voidrun() { //TODO Auto-generated method stub while(true){ getOtherNumber(); } } public voidgetOtherNumber(){ try{ String oth =in.readUTF(); String value =in.readUTF(); Socket other =map.get(oth); String str=number +"对你说: "+value; DataOutputStream asd = newDataOutputStream(other.getOutputStream()); asd.writeUTF(str); asd.flush(); } catch(IOException e) { //TODO Auto-generated catch block e.printStackTrace(); } } }
客户端:
packagecn.com.test09; importjava.io.BufferedReader; importjava.io.DataInputStream; importjava.io.DataOutputStream; importjava.io.IOException; importjava.io.InputStreamReader; importjava.net.Socket; importjava.net.UnknownHostException; public classt13 { public static voidmain(String[] args) { new ClientOO("192.168.0.105",8080,"1");//服务器ip 服务器端口 自身编号 } } classClientOO{ privateString path; private intport; privateString name; privateDataInputStream in; privateDataOutputStream out; public ClientOO(String path,intport,String name){ this.path=path; this.port=port; this.name=name; try{ Socket s=newSocket(path,port); this.in=newDataInputStream(s.getInputStream()); this.out=newDataOutputStream(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 voidsend(){ 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 voidhhhhh(){ newMyServerReader1(in).start(); newMyServerWriter1(out).start(); } } class MyServerReader1 extendsThread { privateDataInputStream dis; publicMyServerReader1(DataInputStream dis) { this.dis =dis; } public voidrun() { 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 extendsThread { privateDataOutputStream dos; publicMyServerWriter1(DataOutputStream dos) { this.dos =dos; } public voidrun() { //读取键盘输入流 InputStreamReader isr = newInputStreamReader(System.in); //封装键盘输入流 BufferedReader br = newBufferedReader(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(); } } }