Advertisement
Guest User

Untitled

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