程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java Web項目中應用Socket通訊多線程、長銜接的辦法

Java Web項目中應用Socket通訊多線程、長銜接的辦法

編輯:關於JAVA

Java Web項目中應用Socket通訊多線程、長銜接的辦法。本站提示廣大學習愛好者:(Java Web項目中應用Socket通訊多線程、長銜接的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java Web項目中應用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通訊多線程、長銜接的辦法,願望對年夜家有所贊助!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved