java如何使用Socket实现多线程多人聊天,提供源码最好了。希望说详细一点。
5条回答 默认 最新
- LemonSmile_ 2015-05-10 02:11关注
简单的聊天室:
========================================================================================== server: import java.net.*; import java.io.*; import java.util.*; public class ServerSocketDemo { public static void main(String[] args) { ArrayList list = new ArrayList(); ServerSocket ss = null; try { ss =new ServerSocket(8189); while(true) { Socket s=ss.accept(); list.add(s); new ReverseDemo(s,list); } }catch( Exception ex ) { } try { ss.close(); }catch( Exception ex ) { } } } import java.net.*; import java.io.*; import java.util.*; public class ReverseDemo extends Thread { BufferedReader in=null; PrintWriter out=null; String str="abc"; ArrayList list; Socket s; public ReverseDemo(Socket s,ArrayList list) { this.list=list; this.s=s; this.start(); } public void run() { try { InputStream inStream = s.getInputStream(); OutputStream outStream = s.getOutputStream(); PrintWriter out = new PrintWriter(outStream, true /* autoFlush */); System.out.println(s.getInetAddress().getHostAddress()+" connected-------"); out.println("连接成功!"); byte[] buf = new byte[1024]; while(true) { int bytes_read = inStream.read( buf ); if( bytes_read < 0 ) { break; } else { broadcast( buf, 0, bytes_read ); } } } catch (Exception ex) { ex.printStackTrace(); } finally { list.remove( s ); } } void broadcast( byte[] b, int offset, int length ) { for( int i = 0, n = list.size(); i < n; i++ ) { Socket sock = (Socket) list.get( i ); if( sock != s ) { try { sock.getOutputStream().write( b, offset, length ); sock.getOutputStream().flush(); }catch( Exception ex ) { } } } } } =================================================================================== client: /** @version 1.20 2004-08-03 @author Cay Horstmann */ import java.io.*; import java.net.*; import java.util.*; /** This program makes a socket connection to the atomic clock in Boulder, Colorado, and prints the time that the server sends. */ public class SocketTest { private Socket sock_; private byte[] name_; public SocketTest( String host, int port, byte[] name ) throws Exception { name_ = name; sock_ = new Socket( host, port ); Thread th = new Thread( createMsgRecvRunnable() ); th.start(); byte[] b = new byte[ 1024 ]; int bytes_read = readMsg( b ); while( bytes_read > 0 ) { sendMsg( b, 0, bytes_read ); bytes_read = readMsg( b ); } } private Runnable createMsgRecvRunnable() { return new Runnable() { public void run() { try { InputStream in = sock_.getInputStream(); byte[] buf = new byte[1024]; while( true ) { int bytes_read = in.read( buf ); if( bytes_read < 0 ) { break; } else { System.out.write( buf, 0, bytes_read ); } } }catch( Exception ex ) { } } }; } private int readMsg( byte[] b ) throws Exception { return System.in.read( b ); } private void sendMsg( byte[] msg, int offset, int length ) throws Exception { sock_.getOutputStream().write( name_ ); sock_.getOutputStream().write( msg, offset, length ); sock_.getOutputStream().flush(); } public static void main( String[] args ) { try { String name = null; if( args.length > 0 ) { name = args[0] + ":"; } else { name = "unknown:"; } SocketTest test = new SocketTest( "localhost", 8189, name.getBytes() ); }catch( Exception ex ) { ex.printStackTrace(); } } }
多线程参考:
Java Socket网络编程--聊天室的实现(多线程实现无需等待对方响应版本):http://wenku.baidu.com/link?url=LUqWTEcoIuO7wEzby14517TqNaDqOmKUEXrvGOegWKIa2Oq5tscyGkiRa0jkGPxxuGryTf4yhhvfoc2S-w6tsKJNoPDccI83gsfqYCLeokG本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥50 树莓派安卓APK系统签名
- ¥15 maple软件,用solve求反函数出现rootof,怎么办?
- ¥65 汇编语言除法溢出问题
- ¥15 Visual Studio问题
- ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
- ¥20 求一个html代码,有偿
- ¥100 关于使用MATLAB中copularnd函数的问题
- ¥20 在虚拟机的pycharm上
- ¥15 jupyterthemes 设置完毕后没有效果
- ¥15 matlab图像高斯低通滤波