Java并发套接字编程

时间:2018-04-28 12:36:35

标签: java concurrency distributed-computing

下面是我的简单并发服务器的代码。每当我运行多个客户端时,服务器只打印出第一个客户端的输入。我不确定我做错了什么。任何帮助,将不胜感激。

public static void main(String[] args) {
    try {
        ServerSocket serverSocket = new ServerSocket(8001);
        while (true){
            Socket clientSocket = serverSocket.accept();
            System.out.println(clientSocket);
            ConcurrentServer client = new ConcurrentServer(clientSocket);
            client.start();
        }
    } catch (IOException i){}
}

public void run(){
    try {
        inputStream = new BufferedReader(new InputStreamReader(concurrentSocket.getInputStream()));
        outputStream = new PrintWriter(new OutputStreamWriter(concurrentSocket.getOutputStream()));

        String testString = inputStream.readLine();
        System.out.println(testString);


    } catch (IOException i){}
}

3 个答案:

答案 0 :(得分:0)

此代码可帮助您了解如何同时运行多个客户端。 :)

此代码的作用是什么? TCP客户端向服务器发送字符串,TCP服务器以UPPERCASE格式发回字符串&服务器可以通过多个连接同时执行此操作。

我有包含3个文件用于服务器,还有一个用于测试服务器具有多个客户端(ClientTest.java)

<强> Main.java

import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        try {
            new Server(3000).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

<强> Server.java

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;

public class Server {

    private ServerSocket sSocket;
    private boolean run;
    private int port;

    public Server(int port) throws IOException {
        this.port = port;
        this.sSocket = new ServerSocket(this.port);
    }

    public void start() {

        this.run = true;
        Logger.getLogger(getClass().getName()).info("Server is listening on port: " + port);

        try {
            while (run) {
                Socket cs = sSocket.accept();
                Logger.getLogger(getClass().getName())
                        .info("New Client Connected! " + cs.getPort());
                new Thread(new Client(cs)).start(); // Put to a new thread.
            }
        } catch (IOException e) {
            Logger.getLogger(getClass().getName()).severe(e.getMessage());
        }
    }

    public void stop() {
        this.run = false;
    }
}

Client.java (服务器上的客户端进程)

import java.io.*;
import java.net.Socket;
import java.util.logging.Logger;

public class Client implements Runnable {

    private Socket clientSocket;

    private DataOutputStream out; // write for the client
    private BufferedReader in; // read from the client

    public Client(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {
        // Do client process
        outToClient(inFromClient().toUpperCase());
        closeConnection();
    }

    private String inFromClient() {

        String messageFromClient = "";

        /*
         *  Do not use try with resources because once -
         *  - it exits the block it will close your client socket too.
         */
        try {
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            messageFromClient = in.readLine();
        } catch (IOException e) {
            Logger.getLogger(getClass().getName()).severe("InFromClientErr - " + e.getMessage());
        }

        return messageFromClient.trim().equals("") ? "No Inputs given!" : messageFromClient;
    }

    private void outToClient(String message) {
        try {
            out = new DataOutputStream(clientSocket.getOutputStream());
            out.writeBytes(message);
        } catch (IOException e) {
            Logger.getLogger(getClass().getName()).severe("OutToClientErr - " + e.getMessage());
        }
    }

    private void closeConnection() {
        try {
            in.close();
            out.close();
            clientSocket.close();
        } catch (NullPointerException | IOException e) {
            Logger.getLogger(getClass().getName()).severe(e.getMessage());
        }
    }
}

ClientTest.java (适用于测试客户)

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class ClientTest {

    public static void main(String[] args) {

        Socket clientSocket;

        try {
            clientSocket = new Socket("localhost", 3000);

            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            outToServer.writeBytes(new Scanner(System.in).nextLine() + '\n'); // Get user input and send.
            System.out.println(inFromServer.readLine()); // Print the server response.

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:-1)

问题出在客户身上。不是服务器。套接字在for循环之外声明,因此只创建了一个连接。如下所示:

public static void main(String[] args) {
    try {
        socket = new Socket("127.0.0.1", 8001);

        for (int i = 0; i < 5; i++){
            System.out.println("Starting client: " + i);
            ConcurrentClient concurrentClient = new ConcurrentClient(socket, i);
            concurrentClient.run();
        }
    } catch (IOException io) {
    }
}

Socket应该在for循环中声明,如下所示:

public static void main(String[] args) {
    try {
        for (int i = 0; i < 5; i++){
            socket = new Socket("127.0.0.1", 8001);
            System.out.println("Starting client: " + i);
            ConcurrentClient concurrentClient = new ConcurrentClient(socket, i);
            concurrentClient.run();
        }
    } catch (IOException io) {
    }
}

答案 2 :(得分:-2)

我真的不知道你为什么需要如此复杂的输入和输出流结构。最好使用等待新输入的Scanner。 您也可以使用PrintWriter输出对话结果。

这是接受多个客户端的服务器:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ConcurrentServer extends Thread {
private Socket concurrentSocket;

public ConcurrentServer(Socket clientSocket) {
    this.concurrentSocket = clientSocket;
}

public static void main(String[] args) {
    try {
        ServerSocket serverSocket = new ServerSocket(8001);
        while (true){
            Socket clientSocket = serverSocket.accept();
            System.out.println(clientSocket);
            ConcurrentServer client = new ConcurrentServer(clientSocket);
            client.start();
        }
    } catch (IOException i){}
}

public void run(){
    try {
        InputStream inputStream = concurrentSocket.getInputStream();
        Scanner scanner = new Scanner(inputStream);
        OutputStream outputStream = concurrentSocket.getOutputStream();
        PrintWriter pw = new PrintWriter(outputStream);

        while(scanner.hasNextLine()){
            String line = scanner.nextLine();
            System.out.println(line);
            pw.println("message: " + line);
            pw.flush();
        }
    } catch (IOException i){}
}

}