TCP多线程JAVA实现

TCP多线程Java具体代码实现

客户端

import java.io.*;
import java.net.*;

public class EchoClient {
    public static void main(String[] args) throws Exception {
        String userInput = null;
        String echoMessage = null;

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        Socket socket = new Socket("127.0.0.1", 8189);
        System.out.println("Connected to Server");

        InputStream inStream = socket.getInputStream();
        OutputStream outStream = socket.getOutputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
        PrintWriter out = new PrintWriter(outStream);

        while ((userInput = stdIn.readLine()) != null){
            out.println(userInput);
            out.flush();
            echoMessage =in.readLine();
            System.out.println("Echo from server: " + echoMessage);
        }

        socket.close();

    }
}

服务端

import java.io.*;
import java.net.*;

public class MultiThreadEchoServer {
    public static void main(String[] args) throws Exception{
        ServerSocket listenSocket = new ServerSocket(8189);
        Socket socket = null;
        int count = 0;
        System.out.println("Server listening at 8189");

        while (true){
            socket = listenSocket.accept();
            count ++;
            System.out.println("The total number of clients is " + count);
            ServerThread serverThread = new ServerThread(socket, count);
            serverThread.run();
        }
    }
}

服务端线程段

import java.io.*;
import java.net.*;

public class ServerThread {
    Socket socket = null;
    int count;
    public ServerThread(Socket socket, int count){
        this.socket = socket;
        this.count = count;
    }

    public void run(){
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        PrintWriter pw = null;
        try{
            is = socket.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            String info = null;
            while ((info = br.readLine()) != null){
                System.out.println("Message from client" + count + ":" + info);
                pw.println(info);
                pw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (pw != null)
                    pw.close();
                if (os != null)
                    os.close();
                if (br != null)
                    br.close();
                if (isr != null)
                    isr.close();
                if (is != null)
                    is.close();
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值