Socket上的服务器客户端通信无法双向工作

时间:2013-10-17 07:16:36

标签: socket.io serversocket

我是Socket Programming的新手,非常清楚我犯了一个愚蠢的错误。不幸的是,无法弄清楚发生了什么。我在同一台机器上使用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());

}

}

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

1 个答案:

答案 0 :(得分:1)

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

 readFromClient();
    writeToClient();

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

在上面的场景中,服务器和客户端都试图读取...而应该是写入和其他读取。所以我需要做的就是改变订单

    writeToClient();
 readFromClient();

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