实现多线程的sockerserver and client

实现多线程的sockerserver and client,第1张

实现多线程的sockerserver and client

 server端

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    // there is another java class named Server_Thread

    public static void main(String[] args) throws Exception {
        try {
            ServerSocket serverSocket = new ServerSocket(5000);
            System.out.println("Welcome to my servern" +
                     "my server is waiting for Clients");
            // using while to keep accept client
            int i = 1;
            String ip = (InetAddress.getLocalHost().getHostAddress());
            System.out.println("The ip address is :"+ip);
            while (true) {
                // server accept the client connection request
                Socket serverClient = serverSocket.accept();
                System.out.println(" Connected to the Server Successfully!n"+ip+" says");
                // send the request to a separate thread and create a new one
                new Server_Thread(serverClient).start();
                System.out.println("this is the "+i+" require");
                // i++ too show that how many number of clint there
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

serverThread类

重写了thread 的 run方法

最后返回一些quote给client证明返回成功

import java.net.Socket;

public class Server_Thread extends Thread{
    static String hello = "From Server: Hello world";
    Socket socket;

    public Server_Thread(Socket s){
        socket = s;
    }

    @Override
    public void run() {
        try {
            String ip = (InetAddress.getLocalHost().getHostAddress());
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            String quote = pick_quote();
            // output the quote
            bufferedWriter.write(ip+" says : "+quote);
            bufferedWriter.flush();
            //Refresh the buffer stream
            bufferedWriter.close();

        }catch(Exception e){
            System.out.println(e);
        }
    }
    private String pick_quote(){
        int math = (int)(Math.random()*10);
        // randomly pick one quote
        String quote = "If Winter comes, can Spring be far behind ?";
        if(math<3){
             quote = "If Winter comes, can Spring be far behind ?";
        }else if (math<6){
             quote = "Keep calm and carry on!";
        }else{
             quote = "For my life too short for waiting";
        }

        String Return = "nHello!this is my servern" +"There is a quote for you.)n"+quote;
        return Return;
    }

}

最后是client的代码我写了一些循环连接测试多线程(但是好像失败了因为代码处理的很快我不知道thread.sleep是否有效)仅供参考

import java.net.UnknownHostException;
// learning client from https://blog.csdn.net/qq_27525611/article/details/102633014
public class Client {
    public static String ip;
    public static int port;
    public Client() {
        try {
            Socket socket = new Socket(ip,port);
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String message;
            while((message = bufferedReader.readLine()) != null) {
                System.out.println(message);
            }
            socket.close();
        } catch (UnknownHostException var5) {
            var5.printStackTrace();
        } catch (IOException var6) {
            var6.printStackTrace();
        }

    }

    public static void main(String[] args) {
        String serverName = args[0];
        int port = Integer.parseInt(args[1]);
        for (int i = 0; i < 10; i++) {
            new Client();
        }

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5522425.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存