0
當我嘗試將我的客戶端套接字與服務器連接時,我必須在自動連接時鍵入一個字母。服務器已經制作好了,並且在沒有客戶端的情況下也能正常工作當我鍵入它的作品時,它應該自動連接。爲什麼我必須鍵入一個字母才能連接服務器
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class TCPClient {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
String input=scan.next();
String text;
BufferedReader inFromUser;
Socket clientSocket;
inFromUser = new BufferedReader(new InputStreamReader(System.in));
clientSocket = new Socket("HH-PC", 4567);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while(true) {
input = scan.next();
if(input.equals("T")){
outToServer.writeBytes("T\r\n");
System.out.println(inFromServer.readLine());
}
else if(input.equals("S")){
outToServer.writeBytes("S\r\n");
System.out.println(inFromServer.readLine());
}
else if(input.equals("Z")){
outToServer.writeBytes("Z\r\n");
System.out.println(inFromServer.readLine());
}
else if(input.equals("D")){
System.out.println("Write a message");
text=inFromUser.readLine();
outToServer.writeBytes("D "+text + "\r\n");
System.out.println(inFromServer.readLine());
}
else if(input.equals("DW")){
outToServer.writeBytes("DW\r\n");
System.out.println(inFromServer.readLine());
}
else if(input.equals("Q")){
clientSocket.close();
System.out.println("The server is disconnected");
break;
}
}
}
}