SHARE
TWEET

Untitled

a guest Sep 17th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.net.*;
  2. import java.io.*;
  3. public class TCPServer {
  4.     public static void main (String args[]) {
  5.         try{
  6.             int serverPort = 7896; // the server port
  7.             ServerSocket listenSocket = new ServerSocket(serverPort);
  8.             while(true) {
  9.                 Socket clientSocket = listenSocket.accept();
  10.                 Connection c = new Connection(clientSocket);
  11.             }
  12.         } catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());}
  13.     }
  14. }
  15. class Connection extends Thread {
  16.     DataInputStream in;
  17.     DataOutputStream out;
  18.     Socket clientSocket;
  19.     public Connection (Socket aClientSocket) {
  20.         try {
  21.             clientSocket = aClientSocket;
  22.             in = new DataInputStream( clientSocket.getInputStream());
  23.             out =new DataOutputStream( clientSocket.getOutputStream());
  24.             this.start();
  25.         } catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
  26.     }
  27.     public void run(){
  28.         try {                            // an echo server
  29.  
  30.             String data = in.readUTF();                   // read a line of data from the stream
  31.             out.writeUTF(data);
  32.         }catch (EOFException e){System.out.println("EOF:"+e.getMessage());
  33.         } catch(IOException e) {System.out.println("readline:"+e.getMessage());
  34.         } finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}
  35.        
  36.  
  37.     }
  38. }
  39. //Code by Coulouris et al
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