SHARE
TWEET

hi

a guest Jun 15th, 2017 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7. import java.net.ServerSocket;
  8. import java.net.InetAddress;
  9. import java.net.Socket;
  10. import java.net.*;
  11. import java.io.*;
  12.  
  13. import javax.swing.JOptionPane;
  14.  
  15. public class Client
  16. {
  17.    private static Socket socket;
  18.       public static void main(String args[])
  19.       {
  20.          try
  21.          {
  22.             /* Not technically needed
  23.             URL whatismyip = new URL("http://checkip.amazonaws.com");
  24.             BufferedReader in = new BufferedReader(new InputStreamReader(
  25.             whatismyip.openStream()));
  26.             String ip = in.readLine(); //you get the IP as a String
  27.             System.out.println(ip);
  28.             */
  29.            
  30.             String host = "localhost";
  31.             int port = 43500;
  32.             InetAddress address = InetAddress.getByName(host);
  33.             socket = new Socket(address, port);
  34.            
  35.             //Send the message to the server
  36.             OutputStream os = socket.getOutputStream();
  37.             OutputStreamWriter osw = new OutputStreamWriter(os);
  38.             BufferedWriter bw = new BufferedWriter(osw);
  39.            
  40.             String username = JOptionPane.showInputDialog("Please enter in a Username: ");
  41.             String sendUser = username + "\n";
  42.             String password = JOptionPane.showInputDialog("Please enter in a Password: ");
  43.             String sendPass = password + "\n";
  44.            
  45.             bw.write(sendUser);
  46.             bw.write(sendPass);
  47.             bw.flush();
  48.             System.out.println("Username: Server : "+sendUser);
  49.             System.out.println("Password: Server : "+sendPass);
  50.            
  51.             InputStream is = socket.getInputStream();
  52.             InputStreamReader isr = new InputStreamReader(is);
  53.             BufferedReader br = new BufferedReader(isr);
  54.            
  55.             String message1 = br.readLine();
  56.             String message2 = br.readLine();
  57.             System.out.println("Message received from the server : " +message1);
  58.             System.out.println("Message received from the server : " +message2);
  59.                
  60.             }
  61.          catch (Exception exception)
  62.          {
  63.             exception.printStackTrace();
  64.          }
  65.          finally
  66.          {
  67.             //Closing the socket
  68.             try
  69.             {
  70.                socket.close();
  71.             }
  72.             catch(Exception e)
  73.             {
  74.                e.printStackTrace();
  75.             }
  76.          }
  77.      
  78.       }
  79. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top