SHARE
TWEET

Untitled

a guest Jun 16th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class myFirstTCPServer {
  5.     public static void main(String[] args) throws IOException {
  6.  
  7.         int servPort = 4999;
  8.         ServerSocket Sy = new ServerSocket(servPort);
  9.         Socket servSocket = Sy.accept();
  10.  
  11.         InputStreamReader in = new InputStreamReader(servSocket.getInputStream());
  12.         BufferedReader bf = new BufferedReader(in);
  13.         String str = bf.readLine();
  14.  
  15.         while (!(str.equals("done"))){
  16.  
  17.             System.out.println("client connected");
  18.  
  19.  
  20.             InetAddress address = InetAddress.getLocalHost();
  21.             String ip = address.getHostAddress();
  22.             System.out.println("IP: " + ip);
  23.             System.out.println("Port: " + servPort);
  24.  
  25.             System.out.println("Message from client: " + str.toUpperCase());
  26.  
  27.             PrintWriter pr = new PrintWriter(servSocket.getOutputStream());
  28.             pr.println(str);
  29.             pr.flush();
  30.         }
  31.         servSocket.close();
  32.     }
  33. }
  34.      
  35. import java.net.*;
  36. import java.io.*;
  37. import java.util.Scanner;
  38.  
  39. public class myFirstTCPClient {
  40.     public static void main(String[] args) throws IOException {
  41.         String S;
  42.         Scanner input = new Scanner(System.in);
  43.         System.out.println("Enter a sentence");
  44.         S = input.nextLine();
  45.         Socket clntSocket = new Socket(InetAddress.getLocalHost(), 4999);
  46.  
  47.         while (!(S.equals("done"))){
  48.             double sent = System.nanoTime();
  49.             PrintWriter pr = new PrintWriter(clntSocket.getOutputStream());
  50.             pr.println(S);
  51.             pr.flush();
  52.  
  53.             InputStreamReader in = new InputStreamReader(clntSocket.getInputStream());
  54.             BufferedReader bf = new BufferedReader(in);
  55.  
  56.             String str = bf.readLine();
  57.             System.out.println("Message from server: " + str);
  58.             double received = System.nanoTime();
  59.             double total = received - sent;
  60.             System.out.println("Round Trip Time: " + (total/1000000.0));
  61.         }
  62.         clntSocket.close();
  63.     }
  64. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top