socket一个多线程多客户端即时聊天学习

时间:2022-11-03 08:58:02

转载:http://www.cnblogs.com/notifier/archive/2010/09/26/1835323.html


1.Socket是代表两台机器之间网络连接的对象(java.net.Socket)。

   Socket的建立如下,参数分别是服务器端的IP地址和端口号: Socket socket  =  new Socket("167.5.75.1",5000);

2.    客户端(Client)Socket的使用 

 2.1 从Socket读出数据步骤

12345678910111213141516 //
1.创建Socket连接,告知Server的IP地址以及端口号
Socket
socket =
new Socket("127.0.0.1",4242); 
//
2.创建InputStreamReader,用于读取socket输入流
InputStreamReader
stream =
new InputStreamReader(socket.getInputStream());
//
3.使用BufferedReader链接输入流
BufferedReader
br =
new BufferedReader(stream);
 //
4.读出数据
String
line =
null;
while ((line = br.readLine()) != null){System.out.println("Today's advice is: " + line);} //
5. 关闭输入流BufferedReader
br.close();

  2.2 向Scoket写入数据步骤: 

123456789 //
1.创建Socket连接,告知Server的IP地址以及端口号
Socket
socket =
new Socket("127.0.0.1",4242);
//
2.创建PrintWriter对象,用以接收socket输出流
PrintWriter
writer =
new PrintWriter(socket.getOutputStream());
//
3.使用PrintWriter对象写出输出数据
String
advice =
"Today's advice";
writer.println(advice);//
4. 关闭连接
writer.close();

3.  服务器端(Server)Socket的使用 

1234567891011121314151617 //
1.创建一个SercerSocket,使用4242端口监听客户端请求
ServerSocket
serverSocket =
new ServerSocket(4242);
System.out.println("The server is started, listening on port 4242"); while (true){//
2.ServerSocket的accept()在等待用户连接的时候闲置;在用户连接上来的时候,返回一个Socket来与客户端通信
Socket
socket = serverSocket.accept();
//
3.创建PrintWriter对象,用以接收socket输出流
PrintWriter
writer =
new PrintWriter(socket.getOutputStream());
//
4.使用PrintWriter对象写出输出数据
String
advice =
"notifier's blog";
writer.println(advice); //
5. 关闭连接
writer.close();}

4.   线程的状态

       线程总共有5种状态:

  1. 新建 (Thread t = new Thread())

  2. 就绪 (t.start())

  3. 运行

  4. 堵塞

    线程被block的原因很多,比如: 等待IO操作, sleep(), 等待被占用对象释放

     5. 死亡

5.    解决线程同步化问题的方法是: 对使用到共享对象的方法使用synchronized

   需要注意的是:

       虽说是方法进行了synchronized,但锁不是加在方法上的而是对象上的,也就是说,是synchronized方法获取对象锁。如果对象(类)有两个或者多个synchronized方法,就表示两个线程不能同时进入同一个方法,也不能同时进入不同的方法。 因为同一时间,只有一个方法在占有对象锁。

6.    synchronized代码块

       有时候在一个方法中做了很多事情,但只有一部分逻辑是需要synchronized的,这时候我们可以使用synchronized代码块。如下,其中this表示当前对象: 

12345678910111213 public void function(){    doSomething();         //以下方法需要同步化    synchronized (this)    {        doCriticalStuff();        moreCriticalStuff();    }         doSomeOtherThing();}

7. 以下是一个Socket简单的例子:

客户端代码及详细注释:

12345678910111213141516171819202122232425262728293031323334353637383940414243 /** * @author notifier * @create 2010-9-25 上午10:12:10 * @version 1.0 */public class DailyAdviceClient{    public static void main(String[] args)    {        DailyAdviceClient client = new DailyAdviceClient();        client.receiveMsg();    }         public void receiveMsg()    {        try        {            // 1.创建Socket连接,告知Server的IP地址以及端口号            Socket socket = new Socket("127.0.0.1",4242);            // 2.创建InputStreamReader,用于读取socket输入流            InputStreamReader stream = new InputStreamReader(socket                    .getInputStream());            // 3.使用BufferedReader链接输入流            BufferedReader br = new BufferedReader(stream);             // 4.读出数据            String line = null;            while ((line = br.readLine()) != null)            {                System.out.println("Today's advice is: " + line);            }             // 5. 关闭输入流BufferedReader            br.close();        }catch (UnknownHostException e)        {            e.printStackTrace();        }catch (IOException e)        {            e.printStackTrace();        }    }}

服务器端代码及详细注释:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 /** * @author notifier * @create 2010-9-25 下午07:06:54 * @version 1.0 */public class SimpleChatServer{    // 保存客户端列表    private ArrayList clientList = new ArrayList();;         public static void main(String[] args)    {        new SimpleChatServer().startUp();    }         /**     * 负责服务器端的启动     *     */    public void startUp()    {        try        {            // 创建服务器端ServerSocket连接,监听端口号5000            ServerSocket serverSocket = new ServerSocket(5000);                         // 轮询等待客户端请求            while(true)            {                // 等待客户端请求,无请求则闲置;有请求到来时,返回一个对该请求的socket连接                Socket clientSocket = serverSocket.accept();                                 // 将该客户端加入到列表中                PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());                clientList.add(writer);                                 // 创建ClientHandler对象,通过socket连接通信                Thread t = new Thread(new ClientHandler(clientSocket));                t.start();                 System.out.println("有Client连进来");            }        }catch(Exception e)        {            e.printStackTrace();        }    }         /**     * 客户端处理类, 主要负责:     * 1.接收客户端发来的消息     * 2.将消息转发其他客户端     * @author sdniu     * @create 2010-9-26 上午10:00:18     * @version 1.0     */    public class ClientHandlerimplements Runnable    {        private BufferedReader reader;        private Socket socket;                 /**         * ClientHandler的构造函数         * @param clientSocket         */        public ClientHandler(Socket clientSocket)        {                         try            {                // 得到socket连接                socket = clientSocket;                                 // 得到客户端发来的消息                InputStreamReader isReader = new InputStreamReader(socket.getInputStream());                reader = new BufferedReader(isReader);            }catch (IOException e)            {                e.printStackTrace();            }        }                 public void run()        {            String message;            try            {                while((message = reader.readLine()) != null)                {                    System.out.println("客户端消息: " + message);                    // 将客户端发来的消息转发所有客户端                    notifyAllClients(message);                }            }catch (IOException e)            {                e.printStackTrace();            }        }    }         /**     *     * @param message     */    public void notifyAllClients(String message)    {        // 得到客户端列表的迭代器,语法格式为 Iterator it = clientList.iterator();        Iterator it = clientList.iterator();                 while(it.hasNext())        {            try            {                // 得到的Iterator别忘了强制转换回PrintWriter                PrintWriter writer = (PrintWriter) it.next();                writer.println(message);                writer.flush();            }catch (Exception e)            {                e.printStackTrace();            }                  }    }}

8. 多线程Socket编程的例子, 代码比较长, 放在下载里了, 链接如下:

     http://files.cnblogs.com/notifier/SimpleChatroom.7z

站内资源:http://download.csdn.net/detail/u014736095/8027261