Advertisement
Guest User

Untitled

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