Java Socket多线程服务端、客户端
- //主方法
- package com.huhai.demo.socket;
- public class Main {
- /** 功能说明:
- * @param args
- * @author 胡海
- * @throws InterruptedException
- * @since 2011-11-8 下午05:08:05
- */
- public static void main(String[] args) throws InterruptedException {
- Thread serverTthread = new Thread(new Runnable(){
- public void run(){
- new SocketServer();
- }
- });
- serverTthread.start();
- Thread.sleep(1000);
- Thread clientThread = new Thread(new Runnable(){
- public void run(){
- new SocketClient();
- }
- });
- clientThread.start();
- }
- }
- //客户端
- <pre name="code" class="java">package com.huhai.demo.socket;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- public class SocketClient {
- private Socket socket;
- private int port = 10000;
- private String hostIp = "127.0.0.1";
- public SocketClient(){
- try {
- String message = null;
- BufferedReader in = null;
- PrintWriter out = null;
- BufferedReader line = null;
- while(!SocketCommand.CONST_QUIT.equals(message)){
- socket = new Socket(hostIp, port);
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- out = new PrintWriter(socket.getOutputStream(), true);
- line = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("please input some characters :");
- message = line.readLine();
- out.println(message);
- System.out.println("Back message from server:[" + in.readLine() + "]");
- }
- System.out.println("Client exit.");
- line.close();
- out.close();
- in.close();
- socket.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- new SocketClient();
- }
- }
//服务端
- package com.huhai.demo.socket;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class SocketServer {
- private ServerSocket serverSocket;
- private Socket socket;
- private int port = 10000;
- private ExecutorService executeService = Executors.newFixedThreadPool(10);
- public SocketServer(){
- try {
- serverSocket = new ServerSocket(port);
- // serverSocket = new ServerSocket(port, 10, InetAddress.getByName("192.168.1.102"));
- System.out.println("SocketServer have been started.[" + serverSocket + "]");
- while(true){
- socket = serverSocket.accept();
- executeService.submit(new Runnable(){
- public void run(){
- try{
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
- String line = in.readLine();
- System.out.println("Server Received:[" + line + "]");
- out.println("Server Received:[" + line + "]");
- out.close();
- in.close();
- }catch (Exception e) {
- e.printStackTrace();
- }finally{
- try {
- socket.close();
- } catch (IOException e) {
- System.out.println("close socket error.");
- e.printStackTrace();
- }
- }
- }
- });
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- try {
- if(null != serverSocket){
- serverSocket.close();
- System.out.println("serverSocket close");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args){
- new SocketServer();
- }
- }
//常量
- package com.huhai.demo.socket;
- public class SocketCommand {
- public static final String CONST_QUIT = "quit";
- }
运行结果: