Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7.  
  8. public class TCP_Client
  9.  
  10. {
  11.     public static void main(String[] args) throws IOException
  12.     {
  13.         int port = 6666;
  14.         String ip ="127.0.0.1";
  15.         String msg;
  16.         Scanner scan =new Scanner(System.in);
  17.         try {
  18.             // Connect with server
  19.             Socket connection = new Socket(ip, port);
  20.             System.out.println("Connected! " + connection);
  21.             // Setting up input and output streams
  22.             DataOutputStream outputStr = new DataOutputStream(
  23.                     connection.getOutputStream());
  24.             DataInputStream inputStr = new DataInputStream(
  25.                     new BufferedInputStream(connection.getInputStream()));
  26.             // Start communication
  27.             msg = scan.next();
  28.             outputStr.writeUTF(msg);
  29.             outputStr.flush();
  30.             String message = inputStr.readUTF();
  31.             System.out.println("TCP_Server> " + message);
  32.             // Close streams and socket
  33.             outputStr.close();
  34.             inputStr.close();
  35.             connection.close();
  36.         } catch (IOException ex) {
  37.             System.out.println("Connection error! " + ex);
  38.         }
  39.     }
  40.  
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement