socket实现服务端多线程,客户端重复输入

服务端

package wanfeng;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server2 extends Thread {
    private ServerSocket ss=null;
    private int clientcounter=0;
    public Server2()
    {
        try {
            ss=new ServerSocket(9999);
        } catch (IOException e) {
            System.out.println("绑定端口失败!"+e.getMessage());
        }
    }
    public  void run()
    {
        Socket s= null;
        try {
            while(true) {
                s = ss.accept();
                clientcounter++;
                SockerThread sockerThread = new SockerThread(s, clientcounter);//没次接受一个,开启一个多线程
                sockerThread.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(ss!=null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    public static void main(String[] args) {
         Server2 server2=new Server2();
         server2.start();
    }
}

服务端实现开启多线程

package wanfeng;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SockerThread extends Thread {

    private Socket s = null;
    private  int clientcounter=0;
    public SockerThread(Socket s,int clientcounter)
    {
        this.s=s;
        this.clientcounter=clientcounter;
        System.out.println("有新线程连接了:当前线程数为:"+clientcounter+"线程id:"+currentThread().getId());
    }
    public void run()
    {
       //InputStream inputStream=null;
        try {
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(s.getInputStream()));
             /*inputStream=s.getInputStream();
            byte[] buf=new byte[1024];
            int num=inputStream.read(buf);
            String string = new String(buf,0,num);*/
             String string = "";
             while((string = bufferedReader.readLine())!=null) {
                 System.out.println(s.getInetAddress().toString() + ": " + string + "  " + "客户端id: " + clientcounter);
             }
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端

package wanfeng;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client2 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        Socket socket = new Socket("127.0.0.1",9999);
        //OutputStream outputStream=socket.getOutputStream();
        PrintWriter out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        String str="";
        while((str=scanner.nextLine())!=null)
        {
          out.println(str);
          out.flush();
          if(str.equals("q"))
              break;
        }

        socket.close();

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值