我是靠谱客的博主 等待大叔,最近开发中收集的这篇文章主要介绍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();
}
}

最后

以上就是等待大叔为你收集整理的socket 长连接的全部内容,希望文章能够帮你解决socket 长连接所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部