Client Server
Applications
The Web Service is provided by the server
and the clients use these services
Client u e st
Re q
Server
Network
t
Resul
Client machine
Server machine
AThis is where
client, socketsand
A server, come in !!
network
Sockets
Sockets are the endpoints of any
communication over the internet.
Sockets are identified by socket
addresses.
Socket Address = IP Address + Port
Number
Why IP Address + Port
number?
• IP Address is of the form 10.0.0.1
• Port Number can be anything from 0
to 65,535.
Destination Socket = 10.0.0.2 : 80
IP Address – Choose
network
20.0.0.0
10.0.0.0 30.0.0.0
40.0.0.0
Destination Socket =
10.0.0.2 : 80
IP Address -> MAC Address – Locate
the specific
system
10.0.0.1
10.0.0.2
Port Number – Process
Port
10000 specific
Port
11000
Port
120000
Understanding Ports
OutLook AVG
Gmail YM
Express Update
Port 1 Port 2 Port 3 Port 4
Transport Layer
Packet
Port num data
Network layer
Thus virtually, sockets are a
connection between the two
processes in different systems.
Eg : Let the socket pairs be
10.0.0.1 : 80 and 20.0.0.1 : 2000
192.168.21.10 : 3000 and 192.168.100.1
: 6000
Networking Basics – the larger
picture
Applications Layer
Standard apps
HTTP TCP STACK
FTP
Telnet Application
User apps (http,ftp,telnet,…)
Transport Layer Transport
TCP (TCP, UDP,..)
UDP
Network
Network Layer (IP,..)
IP
Link Layer Link
(device driver,..)
Device drivers
Network Basics - Where are these
sockets?
Applications Layer
Standard apps
HTTP TCP STACK
FTP
Telnet Application
User apps (http,ftp,telnet,…)
Programming Socke
Interface: ts
SOCKETS Transport
(TCP, UDP,..)
Transport Layer
TCP Network
UDP (IP,..)
Link
Network Layer (device driver,..)
IP
Link Layer
Now into Socket
programming..
Socket Programming with
TCP
Server starts first..
Server Process must be running first
Server must have created a socket which
welcomes client’s connection. (Welcoming socket)
Client contacts server by..
Creating Client local TCP socket
Specify IP Address and port number of server
process.
When Client socket is created, the connection is
established.
When connection is established, server creates a
new socket (Connection Socket) to communicate
with that client and the Welcoming socket is once
again waiting for connections for other clients.
Client/Server Socket
Interaction in TCP
Server
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket() Client
TCP create socket,
wait for incoming
connection request connection setup connect to hostid, port=x
connectionSocket = clientSocket =
welcomeSocket.accept() Socket()
send request using
read request from clientSocket
connectionSocket
write reply to
connectionSocket read reply from
connectionSocket
close
connectionSocket close
clientSocket
Step 1 :
Connection request
port
Server
Client
Step 2 :
port
Server
port
Client
port Connection
Types of Sockets in TCP
ServerSocket – the socket used by
servers
Socket – Socket used by clients
Create a ServerSocket in the server
and make it to wait for connections
from Sockets from other clients
The concept of Streams
Client Server
output input
stream stream
Client Server
socket socket
Client Server
input output
stream stream
Socket Programming with
UDP
No Connection between client and
server.
No handshaking
The sender has to explicitly mention the
IP address and the port of the
destination.
The server should extract the IP Address
of the datagram everytime.
Uses DatagramSocket.
Client/server socket
interaction: UDP
Server Client
create socket,
port=x, for create socket,
clientSocket =
incoming request: DatagramSocket()
serverSocket =
DatagramSocket()
Create, address (hostid, port=x),
send datagram request
using clientSocket
read request from
serverSocket
write reply to
serverSocket
read reply from
specifying client
clientSocket
host address,
port number close
clientSocket
Coding
time..
Examples
-Client-
When programming a client, you must follow these four
steps:
1.Open a socket.
2.Open an input and output stream to
the Socket.
3.Read from and write to the socket
according to the server's protocol.
4.Clean up.
These steps are pretty much the same for all clients.
The only step that varies is step three, since it depends
on the server you are talking to.
Echo Client Example
Examples
-Server-
In following example, Basically, the
echo server receives text from the
client and then sends that exact text
back to the client.
Echo Server Example
TCP/IP SOCKET
PROGRAMMING
The two key classes from the java.net package used in
creation of server and client programs are:
ServerSocket
Socket
A server program creates a specific type of socket that
is used to listen for client requests (server socket), In
the case of a connection request, the program creates a
new socket through which it will exchange data with the
client using input and output streams.
The socket abstraction is very similar to the file
concept: developers have to open a socket, perform
I/O, and close it.
A simple Server Program in Java The steps for creating a
simple server program are:
1. Open the Server Socket:
ServerSocket server = new ServerSocket( PORT );
2. Wait for the Client Request:
Socket client = server.accept();
3. Create I/O streams for communicating to the client
DataInputStream is = new DataInputStream(client.getInputStream());
DataOutputStream os = new
DataOutputStream(client.getOutputStream());
4. Perform communication with client
Receive from client: String line = is.readLine();
Send to client: os.writeBytes(“Hello\n”);
5. Close socket:
client.close();
// SimpleServer.java: A simple server program.
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {
// Register service on port 1254
ServerSocket s = new ServerSocket(1254);
Socket s1=s.accept(); // Wait and accept a connection
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
// Send a string!
dos.writeUTF(“Hi there”);
// Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
}
}
A simple Client Program in Java The steps for
creating a simple client program are:
1. Create a Socket Object:
Socket client = new Socket(server, port_id);
2. Create I/O streams for communicating with the server.
is = new DataInputStream(client.getInputStream());
os = new DataOutputStream(client.getOutputStream());
3. Perform I/O or communication with the server:
Receive data from the server: String line = is.readLine();
Send data to the server: os.writeBytes(“Hello\n”);
4. Close the socket when done:
client.close();
// SimpleClient.java: A simple client program.
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException {
// Open your connection to a server, at port 1254
Socket s1 = new Socket(“localhost”,1254);
// Get an input file handle from the socket and read the input
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}}
Conclusion
Socket Programming is very easy in
Java.
Usually each and every socket is
handled by a separate thread in real-
time client/server environments.