Java Socket网络编程--聊天室的实现(多线程实现无需等待对方响应版本)

来源:互联网 发布:人伤查勘岗 知乎 编辑:程序博客网 时间:2024/06/05 16:05
<!--Title:Socket ProgrammingAuthor:Lovingshu'sDate:2012/5/16 21:43Remark:明天要和韦少爷一起出差,本说好是用Web Service的,但是后来拿到文档后决定用socket来做...于是就有了这篇socket程序,可以实现无等待聊天(多线程实现,自己的想法!)-->一,服务端:package com.samael.socket;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class MyServer {class ThGetMsg extends Thread{@Overridepublic void run() {if(fromClient==null) return;while(true){try {System.out.println("Message From Client:["+fromClient.readLine()+"]");this.sleep(50);} catch (IOException e) {break;} catch (InterruptedException e) {e.printStackTrace();}}}}public static BufferedReader fromClient=null;public static void main(String[] args) {try {//get server socketServerSocket server=new ServerSocket(3166);//get socket from clientSocket socket=server.accept();System.out.println("The IP["+socket.getInetAddress()+"] is connected!");//then we will get all the streams we need//get information from keyboardBufferedReader input=new BufferedReader(new InputStreamReader(System.in));//get information from clientfromClient=new BufferedReader(new InputStreamReader(socket.getInputStream()));Thread th=new MyServer().new ThGetMsg();th.start();//get printer for clientPrintWriter toClient=new PrintWriter(socket.getOutputStream());String inputContent="";System.out.print("Typing Words And Press [Enter] To Send The Messge:");inputContent=input.readLine();while(!input.equals("termination")){toClient.println(inputContent);toClient.flush();inputContent=input.readLine();}input.close();toClient.close();fromClient.close();th.stop();} catch (IOException e) {e.printStackTrace();}}}二,客户端:package com.samael.socket;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;import com.samael.socket.MyServer.ThGetMsg;public class MyClient {class ThGetMsg extends Thread{@Overridepublic void run() {if(fromServer==null) return;while(true){try {System.out.println("Message From Server:["+fromServer.readLine()+"]");this.sleep(50);} catch (IOException e) {break;} catch (InterruptedException e) {e.printStackTrace();}}}}public static BufferedReader fromServer=null;public static void main(String[] args) {try {Socket socket=new Socket("localhost", 3166);fromServer=new BufferedReader(new InputStreamReader(socket.getInputStream()));BufferedReader input=new BufferedReader(new InputStreamReader(System.in));PrintWriter toServer=new PrintWriter(socket.getOutputStream());Thread th=new MyClient().new ThGetMsg();th.start();String toServerContent="";System.out.print("Typing Words And Press [Enter] To Send The Messge:");toServerContent=input.readLine();while(!toServerContent.equals("termination")){toServer.println(toServerContent);toServer.flush();toServerContent=input.readLine();}input.close();toServer.close();fromServer.close();th.stop();} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}最终效果:Server End:How?Message From Client:[Hey~buddy~Where am I?]You're In My OfficeMessage From Client:[Really?This Is Your Office?It's So Wonderful!]For Sure Buddy!Client End:Typing Words And Press [Enter] To Send The Messge:Message From Server:[How?]Hey~buddy~Where am I?Message From Server:[You're In My Office]Really?This Is Your Office?It's So Wonderful!Message From Server:[For Sure Buddy!]<!--At Last:Good luck boys!-->

下载地址点击这里: