Advertisement
fensa08

#OS LAB1/5

Mar 23rd, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. #OS LAB1/5 made by Fensa08
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.Socket;
  8. import java.io.BufferedReader;
  9. import java.io.DataInputStream;
  10. import java.io.IOException;
  11. import java.net.ServerSocket;
  12. import java.net.Socket;
  13.  
  14.  
  15. public class TCPClient {
  16.  
  17.       public static void main(String []args) throws IOException {
  18.  
  19.  
  20.         Socket s = new Socket("localhost", 9875);
  21.         DataOutputStream out = new DataOutputStream(s.getOutputStream());
  22.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  23.         int i = 0;
  24.         String line = in.readLine();
  25.         String pom[] = line.split(" ");
  26.         while(i < 4){
  27.  
  28.             if( i == 0){
  29.                 double x = Double.parseDouble(pom[0]);
  30.                 out.writeDouble(x);
  31.                 i++;
  32.             }
  33.  
  34.             if( i == 1){
  35.                 long x = Long.parseLong(pom[1]);
  36.                 out.writeLong(x);
  37.                 i++;
  38.             }
  39.  
  40.  
  41.             if( i == 2){
  42.                 boolean x = Boolean.parseBoolean(pom[2]);
  43.                 out.writeBoolean(x);
  44.                 i++;
  45.             }
  46.  
  47.             if(i == 3){
  48.                 out.writeUTF(pom[3]);
  49.                 i++;
  50.             }
  51.  
  52.         }
  53.  
  54.  
  55.         out.close();
  56.         s.close();
  57.         System.out.println("\n\n End of connection");
  58.     }
  59. }
  60.  
  61. -----------------------------------------------------------------------------------------------------------------------------------
  62.  
  63.  public class TCPServer {
  64.  
  65.     public static void main(String []args) throws IOException {
  66.  
  67.  
  68.         ServerSocket s = new ServerSocket(9875);
  69.         Socket socket = s.accept();
  70.         System.out.println("Connected");
  71.         DataInputStream in = new DataInputStream(socket.getInputStream());
  72.         //BufferedReader br = new BufferedReader()
  73.  
  74.         double a = 0;
  75.         long b = 0;
  76.         boolean c = false;
  77.         String input = null;
  78.         int i = 0;
  79.  
  80.  
  81.         while(i < 4){
  82.  
  83.  
  84.             if( i == 0){
  85.                 a = in.readDouble();
  86.                 i++;
  87.             }
  88.  
  89.             if( i == 1){
  90.                 b = in.readLong();
  91.                 i++;
  92.             }
  93.  
  94.  
  95.             if( i == 2){
  96.                 c = in.readBoolean();
  97.                 i++;
  98.             }
  99.  
  100.             if(i == 3){
  101.                 input = in.readUTF();
  102.                 i++;
  103.             }
  104.  
  105.  
  106.         }
  107.  
  108.         System.out.println(a + " "+b + " "+c + " "+input + " ");
  109.         s.close();
  110.         socket.close();
  111.         System.out.println("\n\nClient has terminated the connection");
  112.  
  113.     }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement