Basis of Socket Programming
Basis of Socket Programming
https://rtpnotes.vercel.app
https://pgmsite.netlify.app
Computer-Networks-TCP-Program-Tips
TCP Client
Include Statements
TCP-Client: Basic Algorithm for remembering
TCP-Client: Steps in more detail
1. Creating the socket
2. Setup the server address structure
3. Loop
3.1 Connecting to the server
3.2 Read input
3.3 Send Message
3.4 Receive Message
3.5 Logic to end the loop
4. Close the socket
TCP Server
TCP-Server: Basic Algorithm for remembering
TCP-Server: Steps in more detail
1. Create the socket
2. Setup the server address struct
3. Bind the socket and listen for connections
Bind
Listen
4. Accept connection from client
5. Loop
5.1 Receive data from client and print the message
5.2 Read input to send to client
5.3 Sending the message
6. Close connection with client and server socket
TCP Client
Include Statements
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys.types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int client_socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
3. Loop
We are using TCP server here, since TCP is connection oriented, we need to connect
Buffer
Pointer to the buffer we want to send to the server
Size of Buffer
Size of content to send
Flags
Set as 0, no flags needed
Here we will be receiving the message from server after we have the our message
We need a variable called rBuf to store the received buffer
char rBuf[100] = ""
recv(client_socket, rBuf, sizeof(rBuf), 0); // Receive data from the server
close(client_socket)
TCP Server
TCP-Server: Basic Algorithm for remembering
1. Create the socket
2. Setup the server address struct
3. Bind the socket and listen for connections
4. Accept connection from client
5. Loop
1. Receive data from client and print the message
2. Read input to send to client
3. Send message
4. Logic to end the loop
6. Close connection with client
7. Close server socket
Bind
The bind function is used to associate a socket with a specific local IP address and port
number.
The parameters used are
sockfd
The file descriptor of the socket to be bound. In your example, this is lfd ,
which is the listening file descriptor.
addr (const struct sockaddr *):
A pointer to a struct sockaddr that contains the address to bind to the socket.
addrlen (socklen_t):
The size of the address structure. In your example, this is sizeof(server) ,
which is the size of the struct sockaddr_in structure.
Listen
The listen function marks a connection-oriented socket (like a TCP socket) as ready to
accept incoming connection requests.
The parameters used are
sockfd
The file descriptor of the socket to be set to listening mode. Here its lfd
backlog
The maximum length of the queue of pending connections. In this example, this
is 1 , meaning the queue can hold at most one pending connection before
additional connections are refused.
A pointer to a struct sockaddr structure that will be filled with the address
information of the connecting client. In this example, this is (struct sockaddr
*)&client .
A pointer to a socklen_t variable that initially contains the size of the addr
structure.
5. Loop
The recv function is used to receive data from a connected socket. It reads data from the
socket into a buffer,
Parameters used
sockfd
The file descriptor of the connected socket from which to receive data. In this
example, this is confd , which represents the new socket file descriptor created
by the accept function for the specific client connection.
Buffer
buffer where the received data will be stored.
Size of buffer
Flags
The send function is used to transmit data from a socket to a connected peer. This
function is typically used in server applications to send data to a connected client, or in
client applications to send data to a server.
Parameters used
sockfd
The file descriptor of the connected socket through which data will be sent. In
this example, this is confd , which represents the new socket file descriptor
created by the accept function for the specific client connection.
Buffer
Size of buffer
Flags