Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. // A Java program for a Client
  2. import java.net.*;
  3. import java.io.*;
  4.  
  5. public class Client1
  6. {
  7.     // initialize socket and input output streams
  8.     private Socket socket            = null;
  9.     private DataInputStream  input   = null;
  10.     private DataOutputStream out     = null;
  11.  
  12.     // constructor to put ip address and port
  13.     public Client1(String address, int port)
  14.     {
  15.         // establish a connection
  16.         try
  17.         {
  18.             socket = new Socket(address, port);
  19.             System.out.println("Connected");
  20.  
  21.             // takes input from terminal
  22.             input  = new DataInputStream(System.in);
  23.  
  24.             // sends output to the socket
  25.             out    = new DataOutputStream(socket.getOutputStream());
  26.         }
  27.         catch(UnknownHostException u)
  28.         {
  29.             System.out.println(u);
  30.         }
  31.         catch(IOException i)
  32.         {
  33.             System.out.println(i);
  34.         }
  35.  
  36.         // string to read message from input
  37.         String line = "";
  38.  
  39.         // keep reading until "Over" is input
  40.         while (!line.equals("Over"))
  41.         {
  42.             try
  43.             {
  44.                 line = input.readLine();
  45.                 out.writeUTF(line);
  46.                 //System.out.println(line);
  47.             }
  48.             catch(IOException i)
  49.             {
  50.                 System.out.println(i);
  51.             }
  52.         }
  53.  
  54.         // close the connection
  55.         try
  56.         {
  57.             input.close();
  58.             out.close();
  59.             socket.close();
  60.         }
  61.         catch(IOException i)
  62.         {
  63.             System.out.println(i);
  64.         }
  65.     }
  66.  
  67.     public static void main(String args[])
  68.     {
  69.         Client1 client = new Client1("127.0.0.1", 5000);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement