SHARE
TWEET

Socket Threads

butbanksy Oct 21st, 2019 (edited) 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package SocketTCP;
  2.  
  3. import java.io.*;
  4. import java.net.Socket;
  5.  
  6. public class SocketClient {
  7.     public static void main(String[] args) throws IOException {
  8.  
  9.         BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
  10.         Socket socketClient = new Socket("localhost", 5056);
  11.  
  12.         DataOutputStream toServer;
  13.         toServer = new DataOutputStream(socketClient.getOutputStream());
  14.  
  15.         DataInputStream fromServer;
  16.         fromServer = new DataInputStream(socketClient.getInputStream());
  17.  
  18.         String phrase;
  19.         phrase = userInput.readLine();
  20.  
  21.         toServer.writeBytes(phrase + '\n');
  22.         String phraseModifiee = fromServer.readLine();
  23.  
  24.         System.out.println("Reçu du serveur : " + phraseModifiee);
  25.         socketClient.close();
  26.     }
  27. }
  28.  
  29.  
  30.  
  31.  
  32.  
  33. package SocketTCP;
  34.  
  35. import java.io.*;
  36. import java.net.ServerSocket;
  37. import java.net.Socket;
  38.  
  39. public class Server {
  40.     public static void main(String[] args) throws IOException {
  41.         // server is listening on port 5056
  42.         ServerSocket ss = new ServerSocket(5056);
  43.  
  44.         // running infinite loop for getting
  45.         // client request
  46.         while (true) {
  47.             Socket s = null;
  48.  
  49.             try {
  50.                 // socket object to receive incoming client requests
  51.                 s = ss.accept();
  52.  
  53.                 System.out.println("A new client is connected : " + s);
  54.  
  55.                 // obtaining input and out streams
  56.                 BufferedReader fromClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
  57.                 DataOutputStream toClient = new DataOutputStream(s.getOutputStream());
  58.  
  59.                 System.out.println("Assigning new thread for this client");
  60.  
  61.                 // create a new thread object
  62.                 Thread t = new ClientHandler(s, fromClient, toClient);
  63.  
  64.                 // Invoking the start() method
  65.                 t.start();
  66.  
  67.             } catch (Exception e) {
  68.                 s.close();
  69.                 e.printStackTrace();
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76. package SocketTCP;
  77.  
  78. import java.io.BufferedReader;
  79. import java.io.DataInputStream;
  80. import java.io.DataOutputStream;
  81. import java.io.IOException;
  82. import java.net.Socket;
  83. import java.util.Date;
  84.  
  85. class ClientHandler extends Thread
  86. {
  87.  
  88.     final BufferedReader dis;
  89.     final DataOutputStream dos;
  90.     final Socket s;
  91.  
  92.  
  93.     // Constructor
  94.     public ClientHandler(Socket s, BufferedReader dis, DataOutputStream dos)
  95.     {
  96.         this.s = s;
  97.         this.dis = dis;
  98.         this.dos = dos;
  99.     }
  100.  
  101.     @Override
  102.     public void run()
  103.     {
  104.         try {
  105.  
  106.             String phrase = dis.readLine();
  107.             System.out.println("Reçu du client : "+phrase);
  108.             String phraseMaj = phrase.toUpperCase();
  109.             dos.writeBytes(phraseMaj);
  110.         }
  111.         catch (IOException e){
  112.             e.printStackTrace();
  113.         }
  114.         try
  115.         {
  116.             this.dis.close();
  117.             this.dos.close();
  118.  
  119.         }catch(IOException e){
  120.             e.printStackTrace();
  121.         }
  122.     }
  123. }
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
 
Top