为什么它说客户端InputStream中没有行?(套接字编程)

时间:2017-04-17 17:54:53

标签: java sockets

我尝试编写一个运行服务器的简单程序,然后接受两个客户端。然后其中一个尝试将字符串发送到另一个客户端。 但我的代码不起作用,我也不知道为什么。

这是我的TestClient类:

public class TestClient extends Thread{

int id;
String Name;
Socket client;
boolean isAsk;


public TestClient(int id,String clientName,boolean isAsk) throws IOException {
    this.id=id;
    this.Name=clientName;
    this.isAsk=isAsk;
}


public void connectTheClientToTheLocalHost(ServerSocket server){
    try {
        client = new Socket("localhost",1111);
        server.accept();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void readFromTerminal(){
    try {
        InputStream is=client.getInputStream();
        OutputStream os = client.getOutputStream();
        PrintWriter pw = new PrintWriter(os);
        pw.println("sdklfsdklfk");
        pw.flush();
        pw.close();
    }catch (IOException e){
        e.printStackTrace();
    }
}
public void closeTheCientSocket(){
    try {
        client.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public void write(){
    try {

        Scanner sc = new Scanner(client.getInputStream());
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("file1.txt")));

        String st =sc.nextLine();

        bw.write(st);

        bw.close();

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

@Override
public void run() {

    if(isAsk){

        readFromTerminal();
    }
    else{

        write();
    }
}

这是主要功能:

public class PCServer {


public static void main(String[] args){

    try {
        ServerSocket s = new ServerSocket(1111);
        TestClient t1=(new TestClient(1,"reza",true));
        TestClient t2=(new TestClient(2,"man",false));
        t1.connectTheClientToTheLocalHost(s);

        t1.start();
        Scanner sc = new Scanner(t1.client.getInputStream());
        String st=sc.nextLine();
        System.out.println(st);
        t1.closeTheCientSocket();

        t2.connectTheClientToTheLocalHost(s);
        PrintWriter pw = new PrintWriter(t2.client.getOutputStream());
        pw.println(st);
        pw.flush();

        t2.start();
        t2.closeTheCientSocket();


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

}

实际上这段代码在

中返回一个异常
String st=sc.nextLine();   
主函数中的

表示没有找到行。 有什么问题?

1 个答案:

答案 0 :(得分:0)

java中的ServerSocket通常以另一种方式使用。

如果需要点对点连接,则一个主机会创建一个ServerSocket并接受连接。例子:

第一个主持人示例:

try(ServerSocket serverSocket = new ServerSocket(5555);
    Socket socket = serverSocket.accept();
    // it more convenient to use DataInputStream instead of Scanner I think
    DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
    DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());) {

    while (!Thread.currentThread().isInterrupted()) {
        String msg = dataInputStream.readUTF();
        System.out.println("got request: " + msg);

        dataOutputStream.writeUTF("1-response");
        dataOutputStream.flush();
    }
} catch (IOException e) {
    e.printStackTrace();
}

第二个主持人示例:

try(Socket socket = new Socket("127.0.0.1", 5555);
    DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
    DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())) {

    while (!Thread.currentThread().isInterrupted()) {
        dataOutputStream.writeUTF("2-request");
        dataOutputStream.flush();

        String msg = dataInputStream.readUTF();
        System.out.println("got response: " + msg);
    }
} catch (IOException e) {
    e.printStackTrace();
}

如果您希望一台主机通过服务器(代理)与另一台主机通话,那么您需要主机上的平面Java套接字和代理上的ServerSocket,并且代理必须将从一台主机接收的消息传输到另一台主机。例子:

经纪人(在单独的线程或进程中运行)

try {
    List<Socket> sockets = new ArrayList<>();

    ServerSocket serverSocket = new ServerSocket(5555);

    // accepting connections from 2 clients
    for (int i = 0; i < 2; i++) {
        Socket socket = serverSocket.accept();
        sockets.add(socket);
    }

    // streams for first host
    InputStream hostOneInputStream = sockets.get(0).getInputStream();
    DataInputStream hostOneDataInputStream = new DataInputStream(sockets.get(0).getInputStream());
    DataOutputStream hostOneDataOutputStream = new DataOutputStream(sockets.get(0).getOutputStream());

    // streams for second host
    InputStream hostTwoInputStream = sockets.get(1).getInputStream();
    DataInputStream hostTwoDataInputStream = new DataInputStream(sockets.get(1).getInputStream());
    DataOutputStream hostTwoDataOutputStream = new DataOutputStream(sockets.get(1).getOutputStream());

    while (!Thread.currentThread().isInterrupted()) {
        if (hostOneInputStream.available() > 0) {
            String msg = hostOneDataInputStream.readUTF();
            System.out.println("got message from host 1: " + msg);

            hostTwoDataOutputStream.writeUTF(msg);
            hostTwoDataOutputStream.flush();
            System.out.println("message " + msg + " sent to host two");
        }

        if (hostTwoInputStream.available() > 0) {
            String msg = hostTwoDataInputStream.readUTF();
            System.out.println("got message from host 2: " + msg);

            hostOneDataOutputStream.writeUTF(msg);
            hostOneDataOutputStream.flush();
            System.out.println("message " + msg + " sent to host one");
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

第一个主机(在单独的线程或进程中运行)

try(Socket socket = new Socket("127.0.0.1", 5555);
    DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
    DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())) {

    while (!Thread.currentThread().isInterrupted()) {
        dataOutputStream.writeUTF("1");
        dataOutputStream.flush();

        String msg = dataInputStream.readUTF();
        System.out.println("got msg: " + msg);
        TimeUnit.SECONDS.sleep(5);
    }
} catch (IOException | InterruptedException e) {
    e.printStackTrace();
}

第二个主机(在单独的线程或进程中运行)

try(Socket socket = new Socket("127.0.0.1", 5555);
    DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
    DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())) {

    while (!Thread.currentThread().isInterrupted()) {
        String msg = dataInputStream.readUTF();
        System.out.println("got msg: " + msg);
        TimeUnit.SECONDS.sleep(5);

        dataOutputStream.writeUTF("2");
        dataOutputStream.flush();
    }
} catch (IOException | InterruptedException e) {
    e.printStackTrace();
}