1

在我第二次输入内容时(在我输入主机名之后),第 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新手,所以我想我错过了一些明显的东西。异常读取:

线程“主”java.net.SocketException 中的异常:软件导致连接中止:java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) 处 java.net.SocketOutputStream.socketWrite0(Native Method) 处的套接字写入错误。 net.SocketOutputStream.write(SocketOutputStream.java:115) at java.io.DataOutputStream.writeBytes(DataOutputStream.java:259) at 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");
      }
   }
}
4

3 回答 3

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循环之前。

于 2010-06-30T17:34:04.267 回答
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");
        }
    }
}
于 2010-06-30T17:35:02.990 回答
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');

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

于 2010-06-30T19:20:56.290 回答