Socket Programming
EE4204 Socket Programming Assignment
and Example Programs
Prepared by: Sivaraman Vignesh (Graduate Assistant)
Module Lecturer: Assoc. Prof. Mohan Gurusamy
Electrical and Computer Engineering Department
National University of Singapore
Sockets
Application Programming Interface (API)
Interface between an application and network
Application creates a socket
The interface defines the following operations
Create a socket
Attach a socket to the network
Send/receive messages through a socket
Close a socket
EE4204 (Part 1) Lab
G. Mohan/NUS
2
Socket – Family and Type
Socket Family
PF_INET : Internet Family
PF_UNIX: Unix Pipe facility
Socket Type
SOCK_STREAM: byte stream such as TCP
SOCK_DGRAM: message oriented service such as UDP
EE4204 (Part 1) Lab
G. Mohan/NUS
3
Example 1
Develop a socket program in UNIX/Linux that uses (i) TCP as the transport protocol and (ii) UDP as the transport protocol for transferring a short message between a client and server. The client sends a string (input by the user) to the server and the server prints the string on the screen after receiving it.
EE4204 (Part 1) Lab
G. Mohan/NUS
4
Socket structure
EE4204 (Part 1) Lab
G. Mohan/NUS
5
The McGraw-Hill Companies, Inc., 2000
UDP Client
Create a socket
EE4204 (Part 1) Lab
G. Mohan/NUS
6
Chosen by the system
The McGraw-Hill Companies, Inc., 2000
UDP Client
EE4204 (Part 1) Lab
G. Mohan/NUS
7
Has information like name, aliases, address type etc
Get the server address
Server address(remote socket)
The McGraw-Hill Companies, Inc., 2000
Send data to the server
UDP Server
EE4204 (Part 1) Lab
G. Mohan/NUS
8
Creating socket is same as the client
Values of the other tuples
In case the system has multiple IPs
The McGraw-Hill Companies, Inc., 2000
UDP Server
EE4204 (Part 1) Lab
G. Mohan/NUS
9
Bind function: Associates the socket with the given local address
Receive from client
Client address (remote socket)
Close the socket (both at server and client)
headsock.h (Ex 1)
EE4204 (Part 1) Lab
G. Mohan/NUS
10
Example 1 (TCP): Client
EE4204 (Part 1) Lab
G. Mohan/NUS
11
Server address (from command line)
Creating a socket and connecting to it
Example 1 (TCP): Client
EE4204 (Part 1) Lab
G. Mohan/NUS
12
Client application to send a string to the server
Example 1 (TCP): Server
EE4204 (Part 1) Lab
G. Mohan/NUS
13
Create a socket at the server
Example 1 (TCP): Server
EE4204 (Part 1) Lab
G. Mohan/NUS
14
Informs the OS, that socket is ready for requests
Connect to an incoming request
Server application to receive a string from a client
Example 2
Develop a TCP-based client-server socket program for transferring a large message. Here, the message transmitted from the client to server is read from a large file. The entire message is sent by the client as a single data-unit. After receiving the file, the server sends an ACK message to the receiver. Verify if the file has been sent completely and correctly by comparing the received file with the original file (“diff” command could be used). Measure the message transfer time and throughput.
EE4204 (Part 1) Lab
G. Mohan/NUS
15
Example 2: Client
The TCP connection process is same as Example 1
EE4204 (Part 1) Lab
G. Mohan/NUS
16
Open the file in read mode
Determine the length of the file
Read the file into the buffer
Send the file to the server
Example 2: Client
EE4204 (Part 1) Lab
G. Mohan/NUS
17
Wait for the ack
Check if it is an Ack and calculate the time taken
Example 2: Server
EE4204 (Part 1) Lab
G. Mohan/NUS
18
Receive the whole file from client
Example 2: Server
EE4204 (Part 1) Lab
G. Mohan/NUS
19
Send ack to client after receiving the whole file
Save the file from buffer to a file
headsock.h (Ex 2)
EE4204 (Part 1) Lab
G. Mohan/NUS
20
Example 3
Develop a TCP-based client-server socket program for transferring a large message. Here, the message transmitted from the client to server is read from a large file. The message is split into short data-units which are sent one by one without waiting for any acknowledgement between transmissions of two successive data-units. Verify if the file has been sent completely and correctly by comparing the received file with the original file. Measure the message transfer time and throughput for various sizes of data-units.
EE4204 (Part 1) Lab
G. Mohan/NUS
21
Example 3: Client sending file in data units
EE4204 (Part 1) Lab
G. Mohan/NUS
22
Sending file in data units of size DATALEN
Client waits for Ack
EE4204 (Part 1) Lab
G. Mohan/NUS
23
Example 3: Server receiving file in data units
EE4204 (Part 1) Lab
G. Mohan/NUS
24
Receiving file in data units of size DATALEN
Server sending Ack and saving the file
EE4204 (Part 1) Lab
G. Mohan/NUS
25
Send ack to client
Save the file from buffer to a file
headsock.h (Ex 3)
EE4204 (Part 1) Lab
G. Mohan/NUS
26
/docProps/thumbnail.jpeg