Cnet lab week5_fall2024 (2)
Cnet lab week5_fall2024 (2)
Cnet lab week5_fall2024 (2)
Socket Programming
Socket
A socket is one endpoint of a two way communication link between two programs running on the network. The
socket mechanism provides a means of inter-process communication (IPC) by establishing named contact points
between which the communication takes place. Like ‘Pipe’ is used to create pipes and sockets are created using
‘socket’ system calls. The socket provides a bidirectional FIFO Communication facility over the network. A
socket connecting to the network is created at each end of the communication. Each socket has a specific address.
This address is composed of an IP address and a port number.
Sockets are generally employed in client server applications. The server creates a socket, attaches it to a network
port address then waits for the client to contact it. The client creates a socket and then attempts to connect to the
server socket. When the connection is established, transfer of data takes place.
Sockets are primarily associated with the Transport layer. The Transport layer is responsible for end-to-end
communication and ensures the reliable delivery of data between applications on different devices. Two key
protocols within the Transport layer are Transmission Control Protocol (TCP) and User Datagram Protocol
(UDP), each serving different communication needs.
Socket Programming
Socket programming is a way of connecting two nodes on a network to communicate with each other. One
socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a
connection. The server forms the listener socket while the client reaches out to the server.
2. Bind
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
After the creation of the socket, the bind function binds the socket to the address and port number specified in
addr(custom data structure). In the example code, we bind the server to the localhost, hence we use
INADDR_ANY to specify the IP address.
3. Listen
int listen(int sockfd, int backlog);
It puts the server socket in a passive mode, where it waits for the client to approach the server to make a
connection. The backlog defines the maximum length to which the queue of pending connections for sockfd may
grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of
ECONNREFUSED.
4. Accept
int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
National University of Computer and Emerging Sciences (NUCES) Islamabad
It extracts the first connection request on the queue of pending connections for the listening socket, sockfd, creates
a new connected socket, and returns a new file descriptor referring to that socket. At this point, the connection is
established between client and server, and they are ready to transfer data.
2. Connect:
The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by
addr. Server’s address and port is specified in addr.
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
Sample Code:
Server Side
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
int main() {
WSADATA wsaData;
National University of Computer and Emerging Sciences (NUCES) Islamabad
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("Failed to initialize Winsock.\n");
return 1;
}
listen(server_socket, 5);
printf("Server is listening on port 3036...\n");
closesocket(client_socket);
closesocket(server_socket);
WSACleanup();
return 0;
}
Client Side
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
int main() {
WSADATA wsaData;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("Failed to initialize Winsock.\n");
return 1;
}
// setup an address
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(3036);
// Use localhost (127.0.0.1) for testing, replace with server's IP for real connection
inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr);
// Cleanup Winsock
WSACleanup();
return 0;
}
Practice tasks
Task 1:
Create a simple client-server interaction using socket programming in C. The server should listen for incoming
connections and display messages received from the client. The client, on the other hand, should allow users to
input messages to be sent to the server. Implement a mechanism for the client to signal the end of the conversation,
and ensure that the server reacts accordingly by displaying the messages and terminating the connection. Provide
both the client and server source code along with instructions for running the programs.
Task 2:
Implement a client-server application where the client sends a string to the server, and the server responds with
the reversed version of the string. The client allows the user to input a string. The server processes the string,
reverses it, and sends it back to the client. Display both the sent and received messages on the client side.
Submission Guidelines:
● Source code for both client and server renamed to your roll number.
● Screenshot of the terminal with message transfer.