java如何使用Socket实现多线程多人聊天,提供源码最好了。希望说详细一点。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
简单的聊天室:
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:
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();
        }
    }
}