0
你好,我有一個客戶端服務器程序,服務器端工作,而客戶端有問題。客戶端連接後會打印出一條消息,但是在用戶輸入(尚未使用)後,我收到此錯誤消息。服務器打印出一個帶有行號和序號的文本文件(序號不太正確),並且應該將行號,序號和文本行發送到客戶端。Java客戶端/服務器錯誤:連接重置
服務器:
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)
我以爲服務器中的outToClient發送線路給客戶端 – mrAnderson
不,它調用'writeBytes()'。請注意客戶端*是*發送線路到服務器,並注意爲什麼。 – EJP