Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 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.         // BufferedReader gives us the readLine() method, and an
  29.         // InputStreamReader decodes bytes into characters
  30.         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  31.  
  32.         // read the actual sentence from the console
  33.         System.out.print("Password: ");
  34.         String messageCmd = inFromUser.readLine();
  35.         String messagePrm = inFromUser.readLine();
  36.  
  37.         // write a stream of bytes to the socket
  38.         // we need the end-of-line character so the server knows when
  39.         // the client has stopped sending (actually so the readLine()
  40.         // method works)
  41.         sendCommand(messageCmd, messagePrm);
  42.         //outToServer.writeBytes(message + "\n");
  43.        
  44.         // get the response from the server - this method waits until
  45.         // the server has sent a whole string
  46.         reply = inFromServer.readLine();
  47.  
  48.         System.out.println(reply);
  49.  
  50.         // close the socket and the connection
  51.         clientSocket.close();
  52.     }
  53.     public static String sendCommand(String messageCmd, String messagePrm){
  54.        
  55.         // a DataOutputStream is a portable way of feeding Java types
  56.         // into something that expects a stream of bytes (a socket doesn't
  57.         // know about strings/ints etc, it just sees bytes)
  58.         // Java IO is ridiculously complicated ...
  59.        
  60.        
  61.         DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  62.  
  63.         outToServer.writeBytes(messageCmd + " " + messagePrm + "\n");
  64.        
  65.     }
  66.    
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement