欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

tomcat可以做socket通信嗎

傅智翔2年前52瀏覽0評論

tomcat可以做socket通信嗎?

可以在tomcat環(huán)境下用Java程序?qū)崿F(xiàn)Socket通信,代碼如下:

服務(wù)端:

/**

*

*/

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;

/**

***************************************************************

* 項(xiàng)目名稱:JavaThread

* 程序名稱:JabberServer

* 日期:2015-7-11 下午21:01:12

* 作者:Administrator

* @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 {

//設(shè)定服務(wù)端的端口號

s = new ServerSocket(PORT);

System.out.println("ServerSocket Start:"+s);

//等待請求,此方法會一直阻塞,直到獲得請求才往下走

socket = s.accept();

System.out.println("Connection accept socket:"+socket);

//用于接收客戶端發(fā)來的請求

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

//用于發(fā)送返回信息,可以不需要裝飾這么多io流使用緩沖流時(shí)發(fā)送數(shù)據(jù)要注意調(diào)用.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;

/**

***************************************************************

* 項(xiàng)目名稱:JavaThread 程序名稱:JabberClient

* 作者:Administrator

* @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指定服務(wù)器的地址和端口號

socket = new Socket("127.0.0.1", JabberServer.PORT);

System.out.println("Socket=" + socket);

//同服務(wù)器原理一樣

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();

}

}

}

}