lundi 23 février 2015

Why do I've to type a letter in order to connect with a server



When I try to connect my client socket with a server I've to type a letter while it should connect automatically. The server is already made and works as it should without the client. When I type the letter it works but it should connect automaticallly.



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;
}

}
}


}




Aucun commentaire:

Enregistrer un commentaire