java DataOutputStream异常

时间:2010-06-30 16:55:42

标签: java exception dataoutputstream

第二次输入内容时(第二次输入主机名后)第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:软件   导致连接中止:套接字写入   错误           at java.net.SocketOutputStream.socketWrite0(Native Method)           在java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)           在java.net.SocketOutputStream.write(SocketOutputStream.java:115)           at 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");
      }
   }
}

3 个答案:

答案 0 :(得分:4)

您的客户端创建一个套接字并反复写入该一个套接字。另一方面,您的服务器执行此操作:

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

接受传入的连接,读取一行,然后放弃它(我猜测套接字的最终结果是垃圾收集它关闭连接)。然后它等待新的连接。

因此,要解决您的问题,请尝试移动

    Socket connectionSocket = welcomeSocket.accept();
    BufferedReader inFromClient = new BufferedReader(
            new InputStreamReader(connectionSocket.getInputStream( ) ) );
在while循环之前

答案 1 :(得分:1)

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

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

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");
        }
    }
}

答案 2 :(得分:0)

尝试:

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');

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