F09 Socket
F09 Socket
Dr F. Belqasmi,
Industrial Research Post Doctoral
Fellow, Ericsson Canada
Goals:
Present the basics of socket programming
Show concretely how it works using Java
Agenda:
Basics
Client and server implementation
Sending and receiving data
Basics
• What is a socket?
• Socket
communication
What is a socket
“A socket is an abstraction through which an
application may send and receive data, in
much the same way as an open file handle
allows an application to read and Server
write data to stable storage”
FTP HTTP
Client SNMP
application 21 161 80
TCP/UDP
TCP UDP
IP
IP
192.168.1.3 192.168.1.50
Network
What is a socket
• The ports are used by TCP and UDP protocols to
identify the destination program (application) of
an incoming data
Connection request
port
server
Client
Socket communication
• The server accepts the incoming connection
request.
server
port
Client
port Connection
Socket communication
• Two main communication protocols can be
used for socket programming
– Datagram communication
• datagram sockets (UDP)
– Stream communication
• stream sockets (TCP)
Socket communication
• Datagram communication
• UDP is a connectionless protocol
– For each datagram, we need to send the local socket
descriptor and the receiving socket's address
– There is a size limit of 65,500 bytes on each datagram
– No guarantee that the sent datagrams will be received in
the same order
• Main classes
• Data exchange
• TCP: InputStream and OutputStream
• UDP: DatagramPacket
Client Server
Application Application
socket socket
TCP TCP
IP IP
TCP client/server implementation
Server Client
Open a serverSocket
Display data
close
close clientSocket
serverSocket
TCP server implementation
ServerSocket server;
try {
Open a server server = new ServerSocket(portNumber);
socket } catch (IOException e) { System.out.println(e); }
Create a try {
connection Socket connectSocket = server.accept();
socket object } catch (IOException e) { .. }
try {
Close socket clientSocket.close(); // Close the socket and its streams
} catch (IOException e) {…}
UDP client/server implementation
Server Client
create a serverSocket for create a clientSocket
incoming request.
port=x:
Send a reply
to the client read reply from
using the clientSocket
serverSocket
close
close clientSocket
serverSocket
UDP client implementation
try {
Create the DatagramSocket clientSocket = new DatagramSocket();
clientSocket } catch (IOException e) { System.out.println(e); }
try {
Close socket clientSocket.close(); // Close the socket
} catch (IOException e) {…}
UDP server implementation
DatagramSocket server;
Open a try {
socket serverSocket = new DatagramSocket (portNumber);
} catch (IOException e) { System.out.println(e); }
• Example
Sending and receiving data
• Sender and receiver must agree on the communication
protocol
Voting Request
Candidate = 123
Inquiry Request
Vote Request
Candidate = 123
Client Server
Vote Response
Candidate = 123
Vote count= 456
Vote Response
Candidate = 123
Vote count= 457
Sending and receiving data
1. Message representation:
public class VoteMsg {
private boolean isInquiry; // true if inquiry; false if vote
private boolean isResponse;// true if response from server
private int candidateID; // in [0,1000]
private long voteCount; // nonzero only in response
…..
}