0

我编写了一个基本代码,其中发送者向接收者发送消息,接收者正在打印它。我是一个天真的套接字程序员。我查看了各种帖子,但仍然无法解决此问题。这是我的代码:

TCPServer.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.*;
import java.io.*;
import java.util.*;


import org.omg.CORBA.Any;


public class TCPServer {

    public static void main(String args[]) throws IOException{
        TCPServer tcpServer=new TCPServer();
        ServerSocket serverSocket=new ServerSocket(8777);
        Socket sock=serverSocket.accept();
        BufferedReader inputSenderBuffer=new BufferedReader(new InputStreamReader(sock.getInputStream()));
        DataOutputStream outputToSenderBuffer=new DataOutputStream(sock.getOutputStream());
        outputToSenderBuffer.flush();
        //outputToSenderBuffer.close();
        char[] inputArray=new char[2000];

        int i,lengthMessage;
        int a=1;
        System.out.println("We are here");
        while(a<1000)
        {
            System.out.println("Inside this while loop");
            String inputSender=inputSenderBuffer.readLine();
            lengthMessage=inputSender.length();
        for(i=0;i<lengthMessage;i++)
        {
            System.out.println("putting the received message into buffer");
            inputArray[i]=inputSender.charAt(i);
            System.out.println(inputArray[i]);
        }
        }
    }
}

TCPClient.java

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;

public class TCPClient {

    public static void main(String args[])
    {

        try {
            Socket sock=new Socket("localhost",8777);
            DataOutputStream outputToReceiverBuffer=new DataOutputStream(sock.getOutputStream());
            System.out.println("Sending test message");
            int i=1000;
            while(i<1000)
            {
            outputToReceiverBuffer.writeBytes("1AAAAAAAAAA");
            outputToReceiverBuffer.flush();
            //outputToReceiverBuffer.close();
            }


        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Exception at the TCPServer.java console

Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at TCPServer.main(TCPServer.java:31)

请帮我解决这个问题。

4

1 回答 1

2

你没有写任何东西:

int i=1000;
while(i<1000)

而且你永远不会关闭套接字。因此,当发送方退出时,连接会被重置,等等。

当你解决这个问题时,你正在阅读以下几行:

String inputSender=inputSenderBuffer.readLine();

但你不是在写行:

outputToReceiverBuffer.writeBytes("1AAAAAAAAAA");
outputToReceiverBuffer.flush();

而不是使用DataOutputStream.writeBytes()你应该使用BufferedWriter.write()/newLine().

您也没有检查readLine()for null 的结果,这表明对等方已关闭连接,因此您应该停止读取并关闭您的套接字。您应该依靠它而不是任意计数器来控制您的读取循环。

于 2016-02-25T03:23:50.770 回答