如何使Java套接字工作?如此迷茫

时间:2018-02-09 19:47:55

标签: java sockets

这是我服务器的代码,它应该从用户那里获取输入,将其打印到控制台,然后将其发送回用户。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class DateServer {
    public static void main(String[] args) throws Exception { 
        ServerSocket listener = new ServerSocket(10219);

        Socket s = listener.accept();
        InputStreamReader in = new InputStreamReader(s.getInputStream());
        BufferedReader input = new BufferedReader(in);
        PrintWriter out = new PrintWriter(s.getOutputStream());
        out.println("connected");
        out.flush();

        System.out.println("connected");
        String test;

        while (true) {
            try {
                test = input.readLine();
                System.out.println(test);
                out.println(test + " is what I recieved"); 
                out.flush();
            } catch(Exception X) {System.out.println(X);}
        }
    }
}

这是客户端的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;   
import java.io.PrintWriter;
import java.net.Socket;
import java.util.*;

public class DateClient {
    public static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) throws Exception {
        System.out.println("Enter IP Address of a machine that is");
        System.out.println("running the date service on port 10219:");
        String serverAddress = keyboard.next();
        Socket s = new Socket(serverAddress, 10219);
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));      
        PrintWriter out = new PrintWriter(s.getOutputStream());
        System.out.println(input.readLine());
        while(true){
            try{
                System.out.println(input.readLine());
                out.println(keyboard.next());
                out.flush();
            } catch(Exception X){System.out.println(X);}
        }
    }
}

这是为了在LAN网络上工作。我不知道它为什么不起作用,所有发生的事情都是客户端会得到消息" connected"无论输入到客户端的是什么,都不会发生任何其他事情。对于java来说,我是一个菜鸟,但经过一堆谷歌搜索和搜索java库之后,我似乎无法让它发挥作用。我做错了什么?

3 个答案:

答案 0 :(得分:1)

您从服务器向客户端发送一行,但在您的客户端中等待两行,然后才接受将用户输入发送到服务器。

请注意input.readLine()会在收到数据之前阻止,您是否可以在此处发现deadlock

服务器:

out.println("connected");

while (true) {
    try {
        input.readLine();
    }
}

客户端:

input.readLine();
while(true) {
    try {
        input.readLine();
        out.println(keyboard.next());
    }
}

(修剪掉多余的代码以显示有问题的陈述序列)

答案 1 :(得分:0)

您的客户端和服务器都相互等待,试图input.readLine()

如果您删除服务器out.println("connected")及其相应的客户端input.readLine(),则可以很容易地看到这一点。

在客户端上,您应该首先编写 ,然后才能阅读响应。尝试重新排序以下行:

System.out.println(input.readLine());
out.println(keyboard.next());
out.flush();

获取

out.println(keyboard.next());
out.flush();
System.out.println(input.readLine());

答案 2 :(得分:0)

在客户端中,尝试更改

PrintWriter out = new PrintWriter(s.getOutputStream());
System.out.println(input.readLine());
while(true){
    try{
        System.out.println(input.readLine());
        out.println(keyboard.next());
        out.flush();
    } catch(Exception X){System.out.println(X);}
}

PrintWriter out = new PrintWriter(s.getOutputStream());
while(true){
    try{
        System.out.println(input.readLine());
        out.println(keyboard.nextLine());
        out.flush();
    } catch(Exception X){System.out.println(X);}
}

您的客户端正在尝试读取两行,但您的服务器只发送一行,然后轮询输入,因此两者都被锁定。此外,您的服务器正在逐行读取,您的客户端应该逐行发送数据。