Java Socket TCP编程(Server端多线程处理)

 package com;

 import java.io.*;
import java.net.Socket; /**
* Socket Client
* <p>
* Created by Administrator on 2018/5/2.
*/
public class Client { public static void main(String[] args) { try { // 1.初始化客户端socket连接
Socket socket = new Socket("localhost", 8888); // 2.client发送消息
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.write("[name:jim, pwd:123]");
printWriter.flush();
socket.shutdownOutput(); // 3.client接收消息
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str;
while ((str = bufferedReader.readLine()) != null) {
System.out.println("I am Client, now get message from Server:" + str);
} // 4.关闭资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close(); printWriter.close();
outputStream.close();
socket.close(); } catch (IOException e) {
e.printStackTrace();
}
}
}
 package com;

 import java.io.*;
import java.net.ServerSocket;
import java.net.Socket; /**
* Socket Server
* <p>
* Created by Administrator on 2018/5/2.
*/
public class Server { public static void main(String[] args) { try { // 初始化服务端socket连接,并监听8888端口的socket请求
ServerSocket serverSocket = new ServerSocket(8888); System.out.println("****** I am Server, now begin to wait the client ******"); int count = 0; // 处理socket请求
Socket socket = null;
while (true) { socket = serverSocket.accept();
ServerThread serverThread = new ServerThread(socket);
System.out.println("client host address is: " + socket.getInetAddress().getHostAddress());
serverThread.start();
count++;
System.out.println("now client count is: " + count); }
} catch (IOException e) {
e.printStackTrace();
}
}
}
 package com;

 import java.io.*;
import java.net.Socket; /**
* Created by Administrator on 2018/5/3.
*/
public class ServerThread extends Thread { private Socket socket; public ServerThread(Socket socket) {
this.socket = socket;
} @Override
public void run() { InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
OutputStream outputStream = null;
PrintWriter printWriter = null; try { // server接收消息
inputStream = socket.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader); String str;
if ((str = bufferedReader.readLine()) != null) {
System.out.println("I am Server, now get message from Client: " + str);
}
socket.shutdownInput(); // server发送消息
outputStream = socket.getOutputStream();
printWriter = new PrintWriter(outputStream);
printWriter.write("[welcome to you!]");
printWriter.flush(); } catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (printWriter != null) {
printWriter.close();
}
if (outputStream != null) {
outputStream.close(); }
if (bufferedReader != null) {
bufferedReader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
上一篇:java Socket Tcp 浏览器和服务器(一)


下一篇:*1LL在c++中的意义