Hi, I'm trying to read output from a server. In the client I have done:
When I click a button on my main java frame (I'm using one as a simple GUI), it will somehow get serverMessage, and display it in a text field. The problem is it doesn't seem to be executing that second last line there, because it's not a console application (as stated, uses a java frame as a GUI). At least I think that's the problem... since serverMessage doesn't seem to get a String value from the readLine().
Or is it possible my sockets aren't connecting? It works when I implement both client and server (without GUI) as console apps...
Help? Thanks.
PS: here's the server code:
Code:
Socket clientSocket = new Socket("127.0.0.1", 7777);
DataOutputStream toServer = new DataOutputStream(clientSocket.getOutputStream());
toServer.writeBytes("Hello" + '\n');
BufferedReader fromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String serverMessage = fromServer.readLine();
clientSocket.close();
Or is it possible my sockets aren't connecting? It works when I implement both client and server (without GUI) as console apps...
Help? Thanks.
PS: here's the server code:
Code:
String clientMessage;
String capitalizedMessage;
ServerSocket welcomeSocket = new ServerSocket(7777);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
System.out.println("Connection established.");
BufferedReader fromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream toClient = new DataOutputStream(connectionSocket.getOutputStream());
clientMessage = fromClient.readLine();
capitalizedMessage = clientMessage.toUpperCase() + '\n';
toClient.writeBytes(capitalizedMessage);
}
Comment