Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import java.io.*; import java.net.*;
  2.  
  3. public class echoclient { public static void main(String[] args) throws IOException { //Set up the socket, in and out variables Socket echoSocket = null; PrintWriter echoClientOutput = null; BufferedReader echoClientInput = null;
  4.  
  5. //Localhost is the generic name of the computer that your echoserver is running on //Change port 4321 to the port that your server is connected to the code then connects the input and output
  6.  
  7. try {
  8. echoSocket = new Socket("localhost", 4321); //Make the connection to the server socket //Create the Output and Input echoClientOutput = new PrintWriter(echoSocket.getOutputStream(), true); echoClientInput = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Can't find host"); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: 4321."); System.exit(1); } //Connect the user input BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String fromUser; String fromServer; //Type some input, press return and see what comes back System.out.println("echoclient up and running"); while (true) { fromUser = stdIn.readLine(); //Read from the user if (fromUser != null) { System.out.println("Client: " + fromUser); echoClientOutput.println(fromUser); //Write to the server } fromServer = echoClientInput.readLine(); //Wait and read from the server System.out.println("Server: " + fromServer); } } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement