[java] serverclient

Viewer

copydownloadembedprintName: serverclient
  1. import java.net.*;
  2. import java.util.Scanner;
  3. import java.io.*;
  4. public class server {
  5.  
  6.         public static void main(String[] args) throws Exception {
  7.                 try {
  8.                        
  9.                 ServerSocket s = new ServerSocket(7777);
  10.                 System.out.println("Server is up... Waiting for the client to connect");
  11.                 Socket ss = s.accept();
  12.                
  13.                 DataOutputStream sout = new DataOutputStream(ss.getOutputStream());
  14.                 DataInputStream din = new DataInputStream(ss.getInputStream());
  15.                
  16.                 String str="",str2="";
  17.                 str=din.readUTF();
  18.                 System.out.println("client says:" + str);
  19.                
  20.                
  21.                 sout.writeUTF("u are welcome");
  22.                 sout.flush();
  23.                
  24.                
  25.                 str2=din.readUTF();
  26.                 System.out.println("client says: " + str2);
  27.                 if(str2=="bye") {
  28.                         System.out.println("server goes down");
  29.                         s.close();
  30.                        
  31.                 }
  32.                 }catch (UnknownHostException e) {
  33.                         e.printStackTrace();
  34.                 } catch (IOException e) {
  35.                         e.printStackTrace();
  36.                 }
  37.                 }
  38.         }
  39.  
  40.  
  41.  
  42.  
  43. import java.net.*;
  44. import java.io.*;
  45. import java.util.Scanner;
  46.  
  47. public class client {
  48.  
  49.         public static void main(String[] args) {
  50.                
  51.                 try {
  52.                         Scanner key = new Scanner(System.in);
  53.                         Socket ss = new Socket("localhost"7777);
  54.                         DataOutputStream ssout = new DataOutputStream(ss.getOutputStream());
  55.                         DataInputStream ssout1 = new DataInputStream(ss.getInputStream());
  56.                        
  57.                        
  58.                         ssout.writeUTF("Hello from client. Thank you for connecting me to the server");
  59.                         ssout.flush();
  60.                         String str="";
  61.                        
  62.                        
  63.                         str = ssout1.readUTF();
  64.                         System.out.println("server says:" + str);
  65.                        
  66.                         String x = key.nextLine();
  67.                         ssout.writeUTF(x);
  68.                         ssout.flush();
  69.                        
  70.                 } catch (UnknownHostException e) {
  71.                         e.printStackTrace();
  72.                 } catch (IOException e) {
  73.                         e.printStackTrace();
  74.                 }
  75.         }
  76.  
  77. }
  78.  
  79.  

Editor

You can edit this paste and save as new:


File Description
  • serverclient
  • Paste Code
  • 02 Mar-2021
  • 1.69 Kb
You can Share it: