socket 长连接

[代码] web.xml 跳至 [1] [2] [3] [4]
?
1
2
3
4
5
6
<!-- socket监听 start -->
<listener>
<description>Socket服务随web服务启动而启动</description>
<listener-class>demo.socket.SocketListener</listener-class>
</listener>
<!-- socket监听 end -->
[2].[代码] SocketListener.java(服务端代码) 跳至 [1] [2] [3] [4]
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class SocketListener implements ServletContextListener {
private ListenSocket socketThread;

/**
* 销毁 当servlet容器终止web应用时调用该方法
*/
public void contextDestroyed(ServletContextEvent arg0) {
if(null != socketThread && !socketThread.isInterrupted()){
socketThread.closeSocketServer();//关闭线程
socketThread.interrupt();//中断线程
}
}

/**
* 初始化 当servlet容器启动web应用时调用该方法
*/
public void contextInitialized(ServletContextEvent arg0) {
if(null == socketThread){
socketThread = new ListenSocket();
socketThread.start();
}
}

}
[3].[代码] ListenSocket.java(服务端代码) 跳至 [1] [2] [3] [4]
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class ListenSocket extends Thread {
private static final int SERVER_PORT = 10000; //端口号
private int count = 0;//连接客户端数
private ServerSocket ss = null;//服务端socket

public ListenSocket(){
try {
if(null==ss){
this.ss = new ServerSocket(SERVER_PORT);
}
} catch (IOException e) {
e.printStackTrace();
}
}

public void run() {
System.out.println("-------主监听线程----------start----------");
try {
while(true){
Socket client = ss.accept();
count += 1;
Thread c_thread = new CreateServerThread(client,count);
c_thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}



class CreateServerThread extends Thread {
private Socket client; //当前所连接客户端
private int index;//当前线程编号
private BufferedReader in = null;//从客户端接受到的信息
private OutputStream out = null;//返回给客户端的信息

public CreateServerThread(Socket client,int index) throws IOException {
this.client = client;
this.index = index;
}

public void run(){
System.out.println("-------当前连接的客户端数为----------" + index + "----------");
String ms = "Callback accepted " + client.getInetAddress() + ":" + client.getPort();
System.out.println(ms);

try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));//接收请求的流
out = client.getOutputStream();//写入缓存
int len = 0;//监听到的字符串长度
String str = "";//监听到的字符串
char buf[] = new char[4096];
while((len = in.read(buf)) > 0){
//读取
str += new String(buf,0,len);
System.out.println("---------获取到第"+index+"个客户端的报文:"+str);
if(str!=null && !"".equals(str)){
out.write(("服务端已接受到第"+index+"个客户端发送的报文").getBytes());
}else{
System.out.println("---------第"+index+"个客户端所传报文为空");
out.write("-1".getBytes());
break;
}
}
} catch (IOException e) {
System.out.println("---------服务端与第"+index+"个客户端交互时异常:"+e.getMessage());
}finally{
try {
if(client!=null){
client.close();
count -= 1;
System.out.println("---------关闭第"+index+"个客户端连接,当前连接的客户端个数为"+count);
}
} catch (IOException e) {
System.out.println("---------第"+index+"个客户端关闭异常:"+e.getMessage());
}
}
}
}

public void closeSocketServer(){
try{
if(ss!=null && !ss.isClosed()){
ss.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
[4].[代码] Client.java(客户端代码) 跳至 [1] [2] [3] [4]
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Client extends Thread{
Socket socket;
BufferedReader in;
OutputStream out;
String str;

public Client(String str){
this.str = str;
}

public void run() {
try {
socket = new Socket("127.0.0.1", 10000);
//客户端发送给服务端消息
out = socket.getOutputStream();
out.write(str.getBytes());

//接收到的服务端返回信息
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int len = 0;
String msg = "";
char buf[] = new char[4096];
while((len = in.read(buf)) > 0){
msg += new String(buf,0,len);
System.out.println("------服务端返回信息:"+msg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args){
new Client("hello_1").start();
new Client("hello_2").start();
new Client("hello_3").start();
new Client("hello_4").start();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值