您好我有一个客户端服务器程序,服务器端工作,而客户端有一些问题。客户端连接后打印出一条消息,但在一些用户输入(尚未使用)后,我收到此错误。服务器打印出一个带有行号和序列号的文本文件(seq nums还不是很正确),并且应该将行号,序列号和文本行发送给客户端。
服务器:
public class STSServer {
/**
* @param args the command line arguments
*/
public static void main(String argv[]) throws Exception
{
String startLineFromClient;
String endLineFromClient;
String stringLineNumber;
String stringSeqNumber;
String test = "hello";
int convertStart;
int convertEnd;
int subtractStartEnd;
int temp;
int byteTransfer;
int lineNumber = 0;
int sequenceNumber;
ServerSocket welcomeSocket = new ServerSocket(6789);
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
try{
File myFile = new File("alice.txt");
FileReader fileReader = new FileReader(myFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
startLineFromClient = inFromClient.readLine();
endLineFromClient = inFromClient.readLine();
convertStart = Integer.parseInt(startLineFromClient);
convertEnd = Integer.parseInt(endLineFromClient);
while((line = bufferedReader.readLine()) != null)
{
sequenceNumber = line.length();
System.out.print(lineNumber);
stringLineNumber = Integer.toString(lineNumber);
outToClient.writeBytes(stringLineNumber);
System.out.print(" ");
System.out.print(sequenceNumber);
stringSeqNumber = Integer.toString(sequenceNumber);
outToClient.writeBytes(stringSeqNumber);
System.out.print(" ");
System.out.print(line);
outToClient.writeBytes(line);
System.out.println("");
lineNumber++;
//stringLineNumber = Integer.toString(lineNumber);
//stringSeqNumber = Integer.toString(sequenceNumber);*/
// outToClient.writeBytes(test);
}
fileReader.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
客户端: 公共类STSClient {
public static void main(String argv[])throws Exception
{
String startLine;
String endLine;
String lineNumber;
String seqNumber;
String line;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while (true)
{
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("+++++++++++++++ Simple TCP Service (STS)+++++");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("Command allowed by the server for this client:");
System.out.println("download [starting line #] [ending line #]");
System.out.print("Command: ");
startLine = inFromUser.readLine();
endLine = inFromUser.readLine();
outToServer.writeBytes(startLine + '\n');
outToServer.writeBytes(endLine + '\n');
lineNumber = inFromServer.readLine();
seqNumber = inFromServer.readLine();
line = inFromServer.readLine();
System.out.print(lineNumber);
System.out.print(seqNumber);
System.out.print(line);
System.out.println("");
}
}
}
错误:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at stsclient.STSClient.main(STSClient.java:42)
答案 0 :(得分:0)
您的客户正在读行。
您的服务器没有发送线路。
您的服务器在不关闭套接字的情况下退出,而套接字会在Windows上重置。
您的客户仍然停留在等待线路终结器的第一个readLine()
。
改为重置连接。
解决方案:发送线路,不要在不关闭套接字的情况下退出。