Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package me.julian.serversocket;
  2.  
  3. import android.support.annotation.Nullable;
  4.  
  5. import java.io.DataInputStream;
  6. import java.io.DataOutputStream;
  7. import java.net.Socket;
  8.  
  9. import java.net.*;
  10. import java.io.*;
  11.  
  12. public class Client {
  13.     public Client(String address, int port) {
  14.  
  15.         Socket socket            = null;
  16.         DataInputStream  input   = null;
  17.         DataOutputStream out     = null;
  18.  
  19.         try {
  20.             socket = new Socket(address, port);
  21.             System.out.println("Connected");
  22.  
  23.             input  = new DataInputStream(System.in);
  24.  
  25.             out    = new DataOutputStream(socket.getOutputStream());
  26.         }
  27.         catch(UnknownHostException u) {
  28.             System.out.println(u);
  29.         }
  30.         catch(IOException i) {
  31.             System.out.println(i);
  32.         }
  33.  
  34.         String line = "";
  35.  
  36.         while (!line.equals("close")) {
  37.             try {
  38.                 line = input.readLine();
  39.                 out.writeUTF(line);
  40.             }
  41.             catch(IOException i) {
  42.                 System.out.println(i);
  43.             }
  44.         }
  45.  
  46.         try {
  47.             input.close();
  48.             out.close();
  49.             socket.close();
  50.         }
  51.         catch(IOException i) {
  52.             System.out.println(i);
  53.         }
  54.     }
  55.  
  56.     public static void main(String args[]) {
  57.         System.out.println(args);
  58.         Client client = new Client("127.0.0.1", 5000);
  59.     }
  60.  
  61.     public static void run() {
  62.         Client client = new Client("127.0.0.1", 5000);
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement