如何从客户端读取多行数据传输到服务器。

huangapple go评论77阅读模式
英文:

how to read multiple lines from client to server

问题

// Client:

try {

    Scanner scanner = new Scanner(System.in);

    DataInputStream inputStream = new DataInputStream(socket.getInputStream());
    DataOutputStream outputStream;

    System.out.println("Write something to client:");

    while(scanner.hasNext()) {

        System.out.println("Write something to client:");
        outputStream = new DataOutputStream(socket.getOutputStream());

        String message = scanner.nextLine();

        outputStream.writeUTF(message);
    }

} catch (IOException e) {
    System.out.println("[ERROR] Unable to get streams from server");
}

// ClientThread:

@Override
public void run() {

    try {

        DataInputStream inputStream = new DataInputStream(socket.getInputStream());
        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

        System.out.println(inputStream.readUTF());

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

// Server:

public Server() {

    try {
        serverSocket = new ServerSocket(port);
        System.out.println("[SERVER] Server initialized successfully");
        // consider using!
        Runtime.getRuntime().addShutdownHook(new Thread());

    } catch (IOException e) {
        System.out.println("[ERROR] Unable to launch server on port " + port);
    }

    while (true) {
        Socket socket = null;

        try {
            socket = serverSocket.accept();
        } catch (IOException e) {
            System.out.println("[ERROR] Unable to accept client request");
        }

        System.out.println("[SERVER] New user joined the chat: " + socket);

        groupCounter++;
        ClientThread client = new ClientThread("Client " + groupCounter, socket);

        Thread thread = new Thread(client);
        thread.start();

        groupClients.add(client);

        //System.out.println(groupCounter);

    }
}
英文:

So i'm trying to send multiple lines of code everytime i type something in the client console. However when doing this it only prints the output of the client in the server once, what i would like to do is print the clients output in the server everytime after entering a line.

Client:

	try {
Scanner scanner = new Scanner(System.in);
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
DataOutputStream outputStream;
System.out.println("Write something to client:");
while(scanner.hasNext()) {
System.out.println("Write something to client:");
outputStream = new DataOutputStream(socket.getOutputStream());
String message = scanner.nextLine();
outputStream.writeUTF(message);
}
} catch (IOException e) {
System.out.println("[ERROR] Unable to get streams from server");
}
}

ClientThread:

	@Override
public void run() {
try {
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
System.out.println(inputStream.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
}

Server:
public Server() {

	try {
serverSocket = new ServerSocket(port);
System.out.println("[SERVER] Server initialized successfully");
// consider using!
Runtime.getRuntime().addShutdownHook(new Thread());
} catch (IOException e) {
System.out.println("[ERROR] Unable to launch server on port " + port);
}
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept();
} catch (IOException e) {
System.out.println("[ERROR] Unable to accept client request");
}
System.out.println("[SERVER] New user joined the chat: " + socket);
groupCounter++;
ClientThread client = new ClientThread("Client " + groupCounter, socket);
Thread thread = new Thread(client);
thread.start();
groupClients.add(client);
//System.out.println(groupCounter);
}

答案1

得分: 2

问题出在服务器端,serverSocket.accept()会停止执行并等待客户端连接到服务器套接字。这就是为什么您每次只收到一条消息。

在ClientThread中添加一个无限循环,以确保它持续读取客户端套接字输入。

try {
    while (true) {
        DataInputStream inputStream = new DataInputStream(socket.getInputStream());
        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

        System.out.println(inputStream.readUTF());
    }
} catch (IOException e) {
    e.printStackTrace();
}
英文:

The problem is in the server side, serverSocket.accept() stops the execution and waits for a client to connect to the server socket. That's why you only receive one message every time.

Add an infinite loop in the ClientThread to make sure it keeps on reading the client socket input.

try {
while (true) {
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
System.out.println(inputStream.readUTF());
}
} catch (IOException e) {
e.printStackTrace();
}

huangapple
  • 本文由 发表于 2020年8月25日 23:05:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63581788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定