Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SocketException recv失败,但原因是什么?_Java_Sockets_Tcp - Fatal编程技术网

Java SocketException recv失败,但原因是什么?

Java SocketException recv失败,但原因是什么?,java,sockets,tcp,Java,Sockets,Tcp,我有一个客户端和一个服务器。客户端只需向服务器发送一行输入,然后打印响应 我得到一份工作 SocketException (Software caused connection abort: recv failed) [...] at java.io.InputStreamReader.read(InputStreamReader.java:168) at hw3.Client.readLine(Client.java:37) at hw3.Client.mai

我有一个客户端和一个服务器。客户端只需向服务器发送一行输入,然后打印响应

我得到一份工作

SocketException (Software caused connection abort: recv failed) 
    [...]
    at java.io.InputStreamReader.read(InputStreamReader.java:168)
    at hw3.Client.readLine(Client.java:37)
    at hw3.Client.main(Client.java:28)
调试器告诉我在读取时套接字没有关闭,还有什么会导致此异常

我想我会因为线程问题而遇到问题,有什么事情会被认为是“做错了”吗

作为记录,ServerLogic类如下所示。我的退出代码是1,不是-999,所以不是套接字。close()失败了

class ServerLogic
{
    Socket socket;

    public
    ServerLogic(Socket s)
    {
        this.socket = s;
    }

    public void run()
      throws IOException
    {
        try(InputStreamReader in = new InputStreamReader(socket.getInputStream());
            OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream()))
        {
            StringBuilder sb = new StringBuilder();
            while(in.ready()) {
                char c = (char) in.read();
                if(c == '\n') {
                    String str = process(sb);
                    if(str != null) out.write(str);
                    else return;
                } else {
                    sb.append(in.read());
                }
            }
        } finally {
            try {
                socket.close();
            } catch(IOException ex) {
                ex.printStackTrace(System.err);
                System.exit(-999);
            }
        }
    }

    private static String process(StringBuilder sb)
    { /* ... */ }

服务器希望换行符
\n
终止输入,但您从未从客户端发送
sc.nextLine()
返回输入行,但不包括终止换行符。
while(in.ready())
循环最终结束,服务器关闭套接字而不发送响应。

服务器希望换行符
\n
终止输入,但您从未从客户端发送输入
sc.nextLine()
返回输入行,但不包括终止换行符。
while(in.ready())
循环最终结束,服务器关闭套接字而不发送响应。

尝试在写入后在客户端的clout上调用
flush()
。不,这没有解决问题。我也不会使用in.ready()。没有意义。如果它立即返回null怎么办?什么都没发生。。。扔掉那个。。。只需在写入后在客户端的clout上重新尝试调用
flush()
。不,这没有解决问题。我也不会在.ready()中使用。没有意义。如果它立即返回null怎么办?什么都没发生。。。扔掉那个。。。刚读过手动给sc.nextLine()添加一个换行符没有解决它(
byte[]cl=(sc.nextLine()+'\n').getBytes(“UTF-8”);
),我理解正确了你的答案吗?我还尝试将
clout
更改为
PrintWriter
,并使用
clout.println(sc.nextLine())
,这也不起作用。手动向sc.nextLine()添加一个换行符没有解决它(
byte[]cl=(sc.nextLine()+'\n').getBytes(“UTF-8”);
),我是否正确理解了你的答案?我还尝试将
clout
更改为
PrintWriter
,并使用
clout.println(sc.nextLine())
,这也不起作用。
class ServerLogic
{
    Socket socket;

    public
    ServerLogic(Socket s)
    {
        this.socket = s;
    }

    public void run()
      throws IOException
    {
        try(InputStreamReader in = new InputStreamReader(socket.getInputStream());
            OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream()))
        {
            StringBuilder sb = new StringBuilder();
            while(in.ready()) {
                char c = (char) in.read();
                if(c == '\n') {
                    String str = process(sb);
                    if(str != null) out.write(str);
                    else return;
                } else {
                    sb.append(in.read());
                }
            }
        } finally {
            try {
                socket.close();
            } catch(IOException ex) {
                ex.printStackTrace(System.err);
                System.exit(-999);
            }
        }
    }

    private static String process(StringBuilder sb)
    { /* ... */ }