0

我是 Socket 编程的新手,我很清楚我犯了一个愚蠢的错误。不幸的是,无法弄清楚发生了什么。我在同一台机器上使用 Java 套接字创建了一个基本的多客户端-服务器系统(应该没有任何区别),但令我惊讶的是,我只能

  • 将消息从服​​务器发送到客户端,然后在客户端上读取。 或者
  • 将消息从客户端发送到服务器,然后在服务器上读取。

我无法实现的是

  • 将消息从服​​务器发送到客户端,然后在客户端上读取。
  • 将消息从客户端发送到服务器,然后在服务器上读取。

我附上我的代码。

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

public class ServerSocketMain {

public static void main(String[] args) throws IOException {

    @SuppressWarnings("resource")
    ServerSocket serverSocket = new ServerSocket(1234);

    while(true){
        System.out.println("Server Waiting for Client to Connect ....");
        Thread thread = new Thread(new ServerReaderWriter(serverSocket.accept()));
        System.out.println("Client connected....");
        thread.start();

    }



}

}


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ServerReaderWriter implements Runnable {

private Socket socket;

public ServerReaderWriter(Socket socket) {
    this.socket = socket;
}

@Override
public void run() {
    try {

        readFromClient();
        writeToClient();

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

}

private void writeToClient() throws IOException {
    DataOutputStream writeToClient = new DataOutputStream(this.socket.getOutputStream());
    writeToClient.writeBytes("Communication from Client Thread "
            + Thread.currentThread().getName() + "\n");

}

private void readFromClient() throws IOException {
    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(
            this.socket.getInputStream()));

    System.out.println("Client Communication : " + inFromClient.readLine());

}

}


import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ClientSocketMain {

public static void main(String[] args) throws UnknownHostException, IOException {
    ExecutorService executorService = Executors.newFixedThreadPool(10);
    for (int i = 0; i < 10; i++) {
        executorService.submit(new ClientReaderWriter(new Socket("localhost", 1234)));
    }

    executorService.shutdown();

}

}




import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class ClientReaderWriter implements Runnable {

private Socket socket;

public ClientReaderWriter(Socket socket) {
    this.socket = socket;
}

@Override
public void run() {

    try {

        readFromServer(socket.getInputStream());
        writeToServer(socket.getOutputStream());

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

private void writeToServer(OutputStream outputStream) throws IOException {
    DataOutputStream writeToServer = new DataOutputStream(outputStream);

    writeToServer.writeBytes("Communication from Client Thread "
            + Thread.currentThread().getName() + "\n");

}

private void readFromServer(InputStream inputStream) throws IOException {
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
            inputStream));

    System.out.println(inFromServer.readLine());

}

}

我只是想知道我的客户端套接字是否在读写操作完成之前就关闭了..不确定只是大声猜测。请帮忙!!

4

1 回答 1

1

事实证明......我犯了一个愚蠢的错误......在我上面分享的代码片段中,排序非常重要,我忽略了这个事实。

 readFromClient();
    writeToClient();

 readFromServer(socket.getInputStream());
    writeToServer(socket.getOutputStream());

在上述情况下,服务器和客户端都在尝试读取......而不是一个应该是写入和其他读取。所以我需要做的就是改变订单

    writeToClient();
 readFromClient();

 readFromServer(socket.getInputStream());
    writeToServer(socket.getOutputStream());
于 2013-10-17T08:49:26.987 回答