Socket Programming- Multiple Threads
Socket is used as communication Mechanism between two computers using TCP
Architecture
Server Socket Server Application Client socket Application
Socket…Socket… Socket…
Client Socket Communications with Server Socket
1. Establish a TCP Connection
Server ------ Initiate--- ServerSocket object [Port No] (6666)
Server --- -------- accept() method of ServerSocket Class…… (Waits for Client)
Client ------------ Socket Object Server Name(localhost) Port No(6666)
Attempt Client to Connect
1
SocketServer code
Import…..
Class Xserver
ServerSocket ss = newServerSocket(6666); Create Object
Socket S =ss.accept()
Multiple Threads
Server.java – Client.java
Server File contains two classes – Server public Class for Server & ClientHandler for handling (n)
many Clients………..
One public Class Client—for creating a client
Server Class
1. Establish a Connection
2. Obtaining the streams
3. Creating the handler object
4. Invoking the Start() method
ClientHandler Class
1. extends thread---- objects created – invoked- threads properties
2. DataInputStream…………. DataOutputStream……
3. run()
Client Class
Establish Connection
Communication