java 数据输出流异常

发布于 2024-09-07 20:51:13 字数 2215 浏览 3 评论 0原文

当我第二次输入某些内容时(在输入主机名之后),第 24 行会抛出异常 - 服务器工作正常。代码

import java.io.*;
import java.net.*;

class TCPclient {
    public static void main(String[] args) throws Exception {
        String hostname, msg;
        InetAddress hostaddress;
        BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in));
        System.out.println("Please type host\n");
        hostname = inFromUser.readLine();   //I type localhost
        hostaddress = InetAddress.getByName(hostname);
        Socket cSocket = new Socket(hostaddress, 44444);
        String cAddress = cSocket.getLocalSocketAddress().toString();
        DataOutputStream outToServer = new DataOutputStream (cSocket.getOutputStream());
        while (true)
        {
            msg = inFromUser.readLine();
            System.out.println(msg);
            if (msg.equals("exit"))
            {
                System.out.println("exit");
                break;
            }
            outToServer.writeBytes(cAddress + " said : " + msg + '\n'); //this line throws an exception the second time it runs
        }
        cSocket.close();
    }
}

我是java新手,所以我想我错过了一些明显的东西。异常内容如下:

线程“main”中出现异常 java.net.SocketException:软件 导致连接中止:套接字写入 错误 在 java.net.SocketOutputStream.socketWrite0(本机方法) 在 java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) 在 java.net.SocketOutputStream.write(SocketOutputStream.java:115) 在 java.io.DataOutputStream.writeBytes(DataOutputStream.java:259) 在 TCPclient.main(TCPClient.java:52) Java 结果:1

服务器:

import java.io.*;
import java.net.*;

class TCPServer {
   public static void main(String argv[]) throws Exception {
      String clientSentence;
      ServerSocket welcomeSocket = new ServerSocket(44444);
      while(true) {
         Socket connectionSocket = welcomeSocket.accept();
         BufferedReader inFromClient = new BufferedReader(
                new InputStreamReader(connectionSocket.getInputStream( ) ) );
         clientSentence = inFromClient.readLine();
         System.out.println(clientSentence + "\n");
      }
   }
}

The exception is thrown in line 24 the second time I type something (after I have typed the host name) - server works right. Code

import java.io.*;
import java.net.*;

class TCPclient {
    public static void main(String[] args) throws Exception {
        String hostname, msg;
        InetAddress hostaddress;
        BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in));
        System.out.println("Please type host\n");
        hostname = inFromUser.readLine();   //I type localhost
        hostaddress = InetAddress.getByName(hostname);
        Socket cSocket = new Socket(hostaddress, 44444);
        String cAddress = cSocket.getLocalSocketAddress().toString();
        DataOutputStream outToServer = new DataOutputStream (cSocket.getOutputStream());
        while (true)
        {
            msg = inFromUser.readLine();
            System.out.println(msg);
            if (msg.equals("exit"))
            {
                System.out.println("exit");
                break;
            }
            outToServer.writeBytes(cAddress + " said : " + msg + '\n'); //this line throws an exception the second time it runs
        }
        cSocket.close();
    }
}

I am new in java so I am missing something obvious I guess. Exception reads :

Exception in thread "main"
java.net.SocketException: Software
caused connection abort: socket write
error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
at java.io.DataOutputStream.writeBytes(DataOutputStream.java:259)
at TCPclient.main(TCPClient.java:52) Java
Result: 1

Server :

import java.io.*;
import java.net.*;

class TCPServer {
   public static void main(String argv[]) throws Exception {
      String clientSentence;
      ServerSocket welcomeSocket = new ServerSocket(44444);
      while(true) {
         Socket connectionSocket = welcomeSocket.accept();
         BufferedReader inFromClient = new BufferedReader(
                new InputStreamReader(connectionSocket.getInputStream( ) ) );
         clientSentence = inFromClient.readLine();
         System.out.println(clientSentence + "\n");
      }
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑看君怀她人 2024-09-14 20:51:13

您的客户端创建一个套接字并一遍又一遍地向该套接字写入数据。另一方面,您的服务器执行以下操作:

ServerSocket welcomeSocket = new ServerSocket(44444);
while(true) {
   Socket connectionSocket = welcomeSocket.accept();

接受传入连接,读取一行,然后放弃它(我猜测在垃圾收集时套接字的完成会关闭连接)。然后它等待新的连接。

因此,要解决眼前的问题,请尝试

    Socket connectionSocket = welcomeSocket.accept();
    BufferedReader inFromClient = new BufferedReader(
            new InputStreamReader(connectionSocket.getInputStream( ) ) );

在 while 循环之前移动。

Your client creates one socket and writes over and over again to that one socket. Your server, on the other hand, does this:

ServerSocket welcomeSocket = new ServerSocket(44444);
while(true) {
   Socket connectionSocket = welcomeSocket.accept();

That accepts the incoming connection, reads one line, and then abandons it (and I'm guessing on the socket's finalize when being garbage collected it closes the connection). Then it waits for a new connection.

So to fix your immediate problem, try moving

    Socket connectionSocket = welcomeSocket.accept();
    BufferedReader inFromClient = new BufferedReader(
            new InputStreamReader(connectionSocket.getInputStream( ) ) );

before the while loop.

真心难拥有 2024-09-14 20:51:13

输入第二行之间要等待多长时间?这可能与套接字空闲有关。

同样,使用这样的服务器代码,您只会看到第一条消息。试试这个:

import java.io.*;
import java.net.*;

class TCPServer {
    public static void main(String argv[]) throws Exception {
        String clientSentence;
        ServerSocket welcomeSocket = new ServerSocket(44444);
        Socket connectionSocket = welcomeSocket.accept();
        BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        while (true) {
            clientSentence = inFromClient.readLine();
            System.out.println(clientSentence + "\n");
        }
    }
}

How long do you wait between typing second line? It might have something to do with socket being idle.

Also with the server code like this you will see only first message. Try this:

import java.io.*;
import java.net.*;

class TCPServer {
    public static void main(String argv[]) throws Exception {
        String clientSentence;
        ServerSocket welcomeSocket = new ServerSocket(44444);
        Socket connectionSocket = welcomeSocket.accept();
        BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        while (true) {
            clientSentence = inFromClient.readLine();
            System.out.println(clientSentence + "\n");
        }
    }
}
む无字情书 2024-09-14 20:51:13

尝试:

while (true)
{
    if(inFromUser.readLine() != null)
    {        
        msg = inFromUser.readLine();

        System.out.println(msg);
        if (msg.equals("exit"))
        {
           System.out.println("exit");
           break;
        }

        outToServer.writeBytes(cAddress + " said : " + msg + "\n");
    }
}

注意变化:

if(inFromUser.readLine() != null)
{ 

... "\n"); 

不是

... '\n');

试一试。这可能是一个太简单的解决方案,但它确实是:)

Try:

while (true)
{
    if(inFromUser.readLine() != null)
    {        
        msg = inFromUser.readLine();

        System.out.println(msg);
        if (msg.equals("exit"))
        {
           System.out.println("exit");
           break;
        }

        outToServer.writeBytes(cAddress + " said : " + msg + "\n");
    }
}

Note the changes:

if(inFromUser.readLine() != null)
{ 

and

... "\n"); 

not

... '\n');

Give it a shot. It's probably too simple a solution, but it's something :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文