网络问题!(多线程客户端/服务器)

发布于 2024-08-18 09:12:35 字数 3395 浏览 6 评论 0原文

这是我的主类,属于服务器应用程序!但令人惊讶的是,在不运行客户端应用程序的情况下,这些句子将被写入控制台中。请您帮我解释一下为什么?谢谢。

我的主类:

public class Main {

static Socket client = null;
static ServerSocket server = null;

// We can have 10 clients' connections
static ClientThread t[] = new ClientThread[10];

public static void main(String args[]) {
    System.out.println("Server is starting...");
    System.out.println("Server is listening...");
    try {
        server = new ServerSocket(5050);
        System.out.println("Client Connected...");


        while (true) {

            client = server.accept();
            for (int i = 0; i <= 9; i++) {
                if (t[i] == null) {
                    (t[i] = new ClientThread(client, t)).start();
                    break;
                }
            }
        }
    } catch (IOException e) {
        System.out.println(e);
    }
}
}

// This client thread opens the input and the output streams for a particular client,
// ask the client's name, informs all the clients currently connected to the
// server about the fact that a new client has joined the chat room,
// and as long as it receive data, echos that data back to all other clients.
// When the client leaves the chat room this thread informs also all the
// clients about that and terminates.

class ClientThread extends Thread {

DataInputStream is = null;
PrintStream os = null;
Socket clientSocket = null;
ClientThread t[];

public ClientThread(Socket clientSocket, ClientThread[] t) {
    this.clientSocket = clientSocket;
    this.t = t;
}

@Override
public void run() {
    String line;
    String name;
    try {
        is = new DataInputStream(clientSocket.getInputStream());
        os = new PrintStream(clientSocket.getOutputStream());
        os.println("Enter your name.");
        name = is.readLine();
        os.println("Hello " + name + " to our chat room.\nTo leave enter /quit in a new line");
        for (int i = 0; i <= 9; i++) {
            if (t[i] != null && t[i] != this) {
                t[i].os.println("*** A new user " + name + " entered the chat room !!! ***");
            }
        }
        while (true) {
            line = is.readLine();
            if (line.startsWith("/quit")) {
                break;
            }
            for (int i = 0; i <= 9; i++) {
                if (t[i] != null) {
                    t[i].os.println("<" + name + "> " + line);
                }
            }
        }
        for (int i = 0; i <= 9; i++) {
            if (t[i] != null && t[i] != this) {
                t[i].os.println("*** The user " + name + " is leaving the chat room !!! ***");
            }
        }

        os.println("*** Bye " + name + " ***");

        // Clean up:
        // Set to null the current thread variable such that other client could
        // be accepted by the server

        for (int i = 0; i <= 9; i++) {
            if (t[i] == this) {
                t[i] = null;
            }
        }

        // close the output stream
        // close the input stream
        // close the socket

        is.close();
        os.close();
        clientSocket.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}}

在控制台中:

init:
deps-jar:
compile-single:
run-single:
Server is starting...
Server is listening...
Client Connected...

this is my Main class which belongs to the server application! but it is really surprizing that without running the client application,these sentences will be written in the console.would you please help me why?thanks.

my Main class:

public class Main {

static Socket client = null;
static ServerSocket server = null;

// We can have 10 clients' connections
static ClientThread t[] = new ClientThread[10];

public static void main(String args[]) {
    System.out.println("Server is starting...");
    System.out.println("Server is listening...");
    try {
        server = new ServerSocket(5050);
        System.out.println("Client Connected...");


        while (true) {

            client = server.accept();
            for (int i = 0; i <= 9; i++) {
                if (t[i] == null) {
                    (t[i] = new ClientThread(client, t)).start();
                    break;
                }
            }
        }
    } catch (IOException e) {
        System.out.println(e);
    }
}
}

// This client thread opens the input and the output streams for a particular client,
// ask the client's name, informs all the clients currently connected to the
// server about the fact that a new client has joined the chat room,
// and as long as it receive data, echos that data back to all other clients.
// When the client leaves the chat room this thread informs also all the
// clients about that and terminates.

class ClientThread extends Thread {

DataInputStream is = null;
PrintStream os = null;
Socket clientSocket = null;
ClientThread t[];

public ClientThread(Socket clientSocket, ClientThread[] t) {
    this.clientSocket = clientSocket;
    this.t = t;
}

@Override
public void run() {
    String line;
    String name;
    try {
        is = new DataInputStream(clientSocket.getInputStream());
        os = new PrintStream(clientSocket.getOutputStream());
        os.println("Enter your name.");
        name = is.readLine();
        os.println("Hello " + name + " to our chat room.\nTo leave enter /quit in a new line");
        for (int i = 0; i <= 9; i++) {
            if (t[i] != null && t[i] != this) {
                t[i].os.println("*** A new user " + name + " entered the chat room !!! ***");
            }
        }
        while (true) {
            line = is.readLine();
            if (line.startsWith("/quit")) {
                break;
            }
            for (int i = 0; i <= 9; i++) {
                if (t[i] != null) {
                    t[i].os.println("<" + name + "> " + line);
                }
            }
        }
        for (int i = 0; i <= 9; i++) {
            if (t[i] != null && t[i] != this) {
                t[i].os.println("*** The user " + name + " is leaving the chat room !!! ***");
            }
        }

        os.println("*** Bye " + name + " ***");

        // Clean up:
        // Set to null the current thread variable such that other client could
        // be accepted by the server

        for (int i = 0; i <= 9; i++) {
            if (t[i] == this) {
                t[i] = null;
            }
        }

        // close the output stream
        // close the input stream
        // close the socket

        is.close();
        os.close();
        clientSocket.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}}

in the console:

init:
deps-jar:
compile-single:
run-single:
Server is starting...
Server is listening...
Client Connected...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2024-08-25 09:12:35

您创建一个 Socket 并在接受连接之前声明客户端已连接:

        server = new ServerSocket(5050);
        System.out.println("Client Connected...");

您应该打印

客户端已连接

客户端在该行之后

client = server.accept();

,该行会阻塞,直到客户端实际连接

you create a Socket and declare that the client is connected before accepting a connection :

        server = new ServerSocket(5050);
        System.out.println("Client Connected...");

you should print

client is connected

after this line

client = server.accept();

which blocks until a client actually connects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文