• Post Reply Bookmark Topic Watch Topic
  • New Topic

client server gui  RSS feed

 
mike proger
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I send information back and forth between client and server. I know in a command line you can use
toClient.println("whatever you want to send on the client side");
toClient.flush();
on the client side, and use

String data = (String) fromClient.readLine();
System.out.println(data);
to print whatever was sent from the client on the server side. How do I do the same type of thing in a gui. I have been looking for some time and have not been able to find an answer.
 
mike proger
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry accidently posted twice, I got an error the first time I tried to post
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The process is the same. A GUI is just an interface.

(am I wrong?)
 
mike proger
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok on the server side i don't want to read from a line
String data = (String) fromClient.readLine();

how would i read an object or variable from the client.
 
mike proger
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try {
Socket s = new Socket("localhost", port);
InputStreamReader is1 = new InputStreamReader(s.getInputStream());
BufferedReader fromServer = new BufferedReader(is1);
PrintWriter toServer = new PrintWriter(s.getOutputStream());
System.out.println("Connected to server");
InputStreamReader is2 = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(is2);
do {
System.out.print("Enter message or \"DONE\" to exit: ");
userInput = keyboard.readLine();
toServer.println(userInput);
toServer.flush();
System.out.println("Sent to server: " + userInput);
serverMsg = fromServer.readLine();
System.out.println("Received from server: " + serverMsg);
} while (!userInput.equalsIgnoreCase("DONE"));
fromServer.close();
toServer.close();
System.out.println("Connection is closed");
}
catch(IOException err) {
System.out.println(err.getMessage());

Looking at this code snipped would I have to something besides printWriter

PrintWriter toServer = new PrintWriter(s.getOutputStream());

that statement. is their another class that is used better. If I used printWriter is their a method I can use instead of
toServer.println(userInput);
as above.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!