可以在tomcat環境下用Java程式實現socket通訊,程式碼如下:
服務端:
/**
*
*/
package com.dnion.socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
***************************************************************
* 專案名稱:JavaThread
* 程式名稱:JabberServer
* 日期:2015-7-11 下午21:01:12
* @version
public class JabberServer {
public static int PORT = 8080;
public static void main(String[] agrs) {
ServerSocket s = null;
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
//設定服務端的埠號
s = new ServerSocket(PORT);
System.out.println("ServerSocket Start:"+s);
//等待請求,此方法會一直阻塞,直到獲得請求才往下走
socket = s.accept();
System.out.println("Connection accept socket:"+socket);
//用於接收客戶端發來的請求
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//用於傳送返回資訊,可以不需要裝飾這麼多io流使用緩衝流時傳送資料要注意呼叫.flush()方法
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
while(true){
String str = br.readLine();
if(str.equals("END")){
break;
}
System.out.println("Client Socket Message:"+str);
Thread.sleep(1000);
pw.println("Message Received");
pw.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
System.out.println("Close.....");
br.close();
pw.close();
socket.close();
s.close();
} catch (Exception e2) {
客戶端:
import java.io.IOException;
* 專案名稱:JavaThread 程式名稱:JabberClient
public class JabberClient {
* 方法名:main
* @param @param args
* @return void
public static void main(String[] args) {
//客戶端socket指定伺服器的地址和埠號
socket = new Socket("127.0.0.1", JabberServer.PORT);
System.out.println("Socket=" + socket);
//同伺服器原理一樣
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())));
for (int i = 0; i < 10; i++) {
pw.println("howdy " + i);
System.out.println(str);
pw.println("END");
} finally {
System.out.println("close......");
} catch (IOException e) {
可以在tomcat環境下用Java程式實現socket通訊,程式碼如下:
服務端:
/**
*
*/
package com.dnion.socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
***************************************************************
* 專案名稱:JavaThread
* 程式名稱:JabberServer
* 日期:2015-7-11 下午21:01:12
* @version
***************************************************************
*/
public class JabberServer {
public static int PORT = 8080;
public static void main(String[] agrs) {
ServerSocket s = null;
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
//設定服務端的埠號
s = new ServerSocket(PORT);
System.out.println("ServerSocket Start:"+s);
//等待請求,此方法會一直阻塞,直到獲得請求才往下走
socket = s.accept();
System.out.println("Connection accept socket:"+socket);
//用於接收客戶端發來的請求
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//用於傳送返回資訊,可以不需要裝飾這麼多io流使用緩衝流時傳送資料要注意呼叫.flush()方法
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
while(true){
String str = br.readLine();
if(str.equals("END")){
break;
}
System.out.println("Client Socket Message:"+str);
Thread.sleep(1000);
pw.println("Message Received");
pw.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
System.out.println("Close.....");
try {
br.close();
pw.close();
socket.close();
s.close();
} catch (Exception e2) {
}
}
}
}
客戶端:
/**
*
*/
package com.dnion.socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
/**
***************************************************************
* 專案名稱:JavaThread 程式名稱:JabberClient
* @version
***************************************************************
*/
public class JabberClient {
/**
* 方法名:main
*
* @param @param args
* @return void
*/
public static void main(String[] args) {
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
//客戶端socket指定伺服器的地址和埠號
socket = new Socket("127.0.0.1", JabberServer.PORT);
System.out.println("Socket=" + socket);
//同伺服器原理一樣
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())));
for (int i = 0; i < 10; i++) {
pw.println("howdy " + i);
pw.flush();
String str = br.readLine();
System.out.println(str);
}
pw.println("END");
pw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
System.out.println("close......");
br.close();
pw.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}