![](https://csdnimg.cn/release/download_crawler_static/5824467/bg4.jpg)
30 s1.close();
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 }
35 }
如果服务器端没有打开,这时候运行客户端,出现的结果:
1 java.net.ConnectException: Connection refused: connect
2 at java.net.PlainSocketImpl.socketConnect(Native Method)
3 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
4 at
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
5 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
6 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
7 at java.net.Socket.connect(Socket.java:518)
8 at java.net.Socket.connect(Socket.java:468)
9 at java.net.Socket.<init>(Socket.java:365)
10 at java.net.Socket.<init>(Socket.java:179)
11 at com.b510.socket1703.TestClient.main(TestClient.java:19)
如果服务器端先打开服务,这时候客户端连接服务器端,出现的效果:
1 你好,客户端地址信息: /127.0.0.1 客户端通信端口号: 2800
2 i'm a server ,my name is hongten!
============================================================
2.实现单线程的聊天室
============================================================
服务器端代码:
1 package com.b510.socket1704.simplechatroom;
2
3 import java.io.*;