Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. /*
  5.   This is the client side of the TCP client/server example
  6. */
  7.  
  8. public class FSC {
  9.     public static void main (String args[])throws Exception{
  10.  
  11.           /* if you have time at the end take a look at the exceptions that might be raised and think about nicer ways of handling them */
  12.         String reply;
  13.         // prepare to get input from the console
  14.         // A BufferedReader provides nice methods like readLine()
  15.         // and is more efficient than just using InputStreamReader
  16.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  17.         // create the client socket
  18.         // This is just a generic Socket object - we need to specify
  19.         // the address (or name) of machine running your server,
  20.         // plus the port number your server is listening on
  21.         // if this method doesn't throw an exception, then we have
  22.         // successfully opened a connection to the server
  23.         // If you get an error like "connection refused", then the
  24.         // server probably isn't running
  25.         Socket clientSocket = new Socket("localhost", 2845);
  26.         //InetAddress host = clientSocket.getInetAddress();
  27.        
  28.         // a DataOutputStream is a portable way of feeding Java types
  29.         // into something that expects a stream of bytes (a socket doesn't
  30.         // know about strings/int etc, it just sees bytes)
  31.         // Java IO is ridiculously complicated ...
  32.         DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  33.        
  34.         // BufferedReader gives us the readLine() method, and an
  35.         // InputStreamReader decodes bytes into characters
  36.         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  37.  
  38.         // read the actual sentence from the console
  39.         System.out.print("Password: ");
  40.         String messageCmd = inFromUser.readLine();
  41.         String messagePrm = inFromUser.readLine();
  42.        
  43.         // write a stream of bytes to the socket
  44.         // we need the end-of-line character so the server knows when
  45.         // the client has stopped sending (actually so the readLine()
  46.         // method works)
  47.         sendCommand(messageCmd, messagePrm, outToServer, inFromServer);
  48.         //outToServer.writeBytes(message + "\n");
  49.        
  50.         // get the response from the server - this method waits until
  51.         // the server has sent a whole string
  52.         reply = inFromServer.readLine();
  53.  
  54.         System.out.println(reply);
  55.  
  56.         // close the socket and the connection
  57.         clientSocket.close();
  58.     }
  59.     public static String sendCommand(String messageCmd, String messagePrm, DataOutputStream outToServer, BufferedReader inFromServer) throws IOException{
  60.        
  61.         // a DataOutputStream is a portable way of feeding Java types
  62.         // into something that expects a stream of bytes (a socket doesn't
  63.         // know about strings/int etc, it just sees bytes)
  64.         // Java IO is ridiculously complicated ...
  65.        
  66.         return outToServer.writeBytes(messageCmd + " " + messagePrm + "\n");
  67.     }
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement