Advertisement
Guest User

Socket Threading Example

a guest
Oct 30th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.net.Socket;
  5. import java.net.ServerSocket;
  6. import java.util.ArrayList;
  7.  
  8. public class Main {
  9.    
  10.     private ArrayList<SocketManager> managers = new ArrayList<SocketManager>();
  11.    
  12.     public void startReceivingConnections() {
  13.         final ServerSocket server;
  14.         try {
  15.             server = new ServerSocket(1234);
  16.            
  17.             /* create a thread that will constantly wait for new connections
  18.              * then add them to the list, alerting all other connected hosts
  19.              */
  20.             new Thread(new Runnable() {
  21.                 public void run() {
  22.                     while (true) {
  23.                         Socket s;
  24.                         try {
  25.                             s = server.accept();
  26.                             writeToAll("New connection!");
  27.                             managers.add(new SocketManager(s));
  28.                         } catch (IOException e) {
  29.                             //handle
  30.                         }
  31.                     }
  32.                 }
  33.             }).start();
  34.         } catch (IOException e) {
  35.             //handle
  36.         }
  37.     }
  38.    
  39.     public void writeToAll(String message) throws IOException {
  40.         for (SocketManager m : managers) {
  41.             m.write(message);
  42.         }
  43.     }
  44.    
  45.     public static void main(String[] args) {
  46.         Main m = new Main();
  47.         m.startReceivingConnections();
  48.     }
  49. }
  50.  
  51. class SocketManager {
  52.     private Socket socket;
  53.     private DataInputStream inputStream;
  54.     private DataOutputStream outputStream;
  55.     private Thread readThread;
  56.     private boolean shouldCancel = false;
  57.    
  58.     public SocketManager(Socket s) throws IOException {
  59.         socket = s;
  60.         inputStream = new DataInputStream(s.getInputStream());
  61.         outputStream = new DataOutputStream(s.getOutputStream());
  62.        
  63.         //putting the read on a separate thread allows us to write at any time
  64.         readThread = new Thread(new Runnable() {
  65.             public void run() {
  66.                 while (!shouldCancel) {
  67.                     try {
  68.                         String line = inputStream.readUTF();
  69.                         System.out.println(line);
  70.                     } catch (IOException e) {
  71.                         //handle exception
  72.                     }                  
  73.                 }
  74.             }
  75.         });
  76.         readThread.start();
  77.     }
  78.    
  79.     public void write(String s) {
  80.         try {
  81.             outputStream.writeUTF(s);
  82.         } catch (IOException e) {
  83.             //handle
  84.         }
  85.     }
  86.    
  87.     public void cancel() {
  88.         try {
  89.             shouldCancel = true; //do this instead of calling the deprecated readThread.stop()
  90.             socket.close();
  91.         } catch (IOException e) {
  92.             //handle
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement