欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Web项目中使用Socket通信多线程、长连接的方法

程序员文章站 2024-03-09 16:34:53
很多时候在javaweb项目中我们需要用到socket通信来实现功能,在web中使用socket我们需要建立一个监听程序,在程序启动时,启动socket监听。我们的应用场景...

很多时候在javaweb项目中我们需要用到socket通信来实现功能,在web中使用socket我们需要建立一个监听程序,在程序启动时,启动socket监听。我们的应用场景是在java项目中,需要外接如一个硬件设备,通过tcp通信,获取设备传上来的数据,并对数据做回应。

先看一下web的监听代码:

import javax.servlet.servletcontextevent;
import javax.servlet.servletcontextlistener;
public class attendsocetlistener implements servletcontextlistener{
private socketthread socketthread;
public void contextdestroyed(servletcontextevent arg) { 
if(null!=socketthread && !socketthread.isinterrupted()) 
{ 
socketthread.closesocketserver(); 
socketthread.interrupt(); 
} 
} 
@override 
public void contextinitialized(servletcontextevent arg) { 
// todo auto-generated method stub 
if(null==socketthread) 
{ 
//新建线程类 
socketthread=new socketthread(null); 
//启动线程 
socketthread.start(); 
} 
} 
}

创建线程:

import java.io.ioexception;
import java.net.serversocket;
import java.net.socket;
import java.util.arraylist;
import java.util.list;
public class socketthread extends thread
{
private serversocket serversocket = null; 
public socketthread(serversocket serverscoket){ 
try { 
if(null == serversocket){ 
this.serversocket = new serversocket(); 
system.out.println("socket start"); 
} 
} catch (exception e) { 
system.out.println("socketthread创建socket服务出错"); 
e.printstacktrace(); 
} 
} 
public void run(){ 
while(true){ 
try { 
if(serversocket==null){
break;
}else if(serversocket.isclosed()){
break;
}
socket socket = serversocket.accept(); 
if(null != socket && !socket.isclosed()){ 
//处理接受的数据 
thread t = new thread(new socketoperate(socket)); 
t.start(); 
}else{
break;
}
}catch (exception e) { 
system.out.println("socketthread创建socket服务出错"); 
e.printstacktrace(); 
} 
} 
} 
public void closesocketserver(){ 
try { 
if(null!=serversocket && !serversocket.isclosed()) 
{ 
serversocket.close(); 
} 
} catch (ioexception e) { 
// todo auto-generated catch block 
e.printstacktrace(); 
} 
} 
}

处理接受到的数据:

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.net.socket;
public class socketoperate implements runnable { 
private socket socket; 
//该线程所处理的socket所对应的输入流 
bufferedreader br = null; 
string str = null; 
string content = null; 
inputstreamreader reader=null; 
public socketoperate(socket socket) throws ioexception 
{ 
this.socket = socket; 
reader = new inputstreamreader(this.socket.getinputstream(),"utf-"); 
br = new bufferedreader(reader); 
} 
@override 
public void run() 
{ 
try 
{ 
// 采用循环不断从socket中读取客户端发送过来的数据 
while (true) 
{ 
content = readfromclient(); 
system.out.println(content);
if (content == null) 
{ 
break; 
} 
outputstream os = socket.getoutputstream(); 
os.write(("res, ok,<年班,小明>, ,#" + "\n").getbytes("utf-")); 
os.flush();
} 
} 
catch (ioexception e) 
{ 
e.printstacktrace(); 
} 
} 
//定义读取客户端数据的方法 
private string readfromclient() 
{ 
try 
{ 
str = br.readline(); 
return str; 
} 
//如果捕捉到异常,表明该socket对应的客户端已经关闭 
catch (ioexception e) 
{ 
try {
br.close();
reader.close();
socket.close();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
} 
} 
return null; 
} 
}

客户端代码:

package
import java.io.*;
import java.net.*;
public class talkclient {
public static void main(string args[]) throws unknownhostexception, ioexception {
socket socket=new socket("...",);
printwriter os=new printwriter(socket.getoutputstream());
bufferedreader is=new bufferedreader(new inputstreamreader(socket.getinputstream()));
int i=;
while(socket.isconnected()){
os.print("beat,,,,.,,#"+"\n");
os.flush();
system.out.println("client:"+i);
system.out.println("server:"+is.readline());
i++;
} 
//继续循环
os.close(); //关闭socket输出流
is.close(); //关闭socket输入流
socket.close(); //关闭socket
}
}

以上所述是小编给大家介绍的java web项目中使用socket通信多线程、长连接的方法,希望对大家有所帮助!