0% found this document useful (0 votes)
2 views20 pages

Socket Programming (1)

Socket programming

Uploaded by

Renjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Socket Programming (1)

Socket programming

Uploaded by

Renjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Socket

Programming
Client Server
Socket Programming
Server Client

Socket

Socket
Bind

Listen Connect

Accept
Accept

Send / Receive
Send /
Receive
Client Program
• Headers:

• #include <stdio.h>
• #include <stdlib.h>
• #include <string.h>
• #include <unistd.h>
• #include <arpa/inet.h>
#include <unistd.h>
• This header file provides access to the POSIX operating system
API.
• It includes various function prototypes and constants related to
system calls, file operations, and process management.
• In socket programming, it's often used for functions like
• close() to close sockets or file descriptors,
• read() and write() for reading from and writing to
sockets,
• and fork() for creating child processes, among others.
#include <arpa/inet.h>
• This header file contains definitions for Internet operations, such as
converting IP addresses between binary and text form, and vice versa.

It provides functions like


inet_addr()for converting an IPv4 address from text to binary
form, inet_ntoa() for converting an IPv4 address from binary
to text form.
Main function
int main(){

char *ip = "127.0.0.1";


int port = 5566;

int sock;
struct sockaddr_in addr;
socklen_t addr_size;
char buffer[1024];
int n;
char *ip = "127.0.0.1";
int port = 5566;

ip:
This variable is a pointer to a character array (char *). It holds
the IP address "127.0.0.1", which is the loopback address
commonly used to refer to the local machine.

Port:
This variable holds the port number 5566. Ports are endpoints
for communication in networking, and this specific port
number is chosen for communication through the socket.
• int sock;
sock: This integer variable will hold the socket descriptor, a unique
identifier for the socket created during the program.
• struct sockaddr_in addr;
struct sockaddr_in addr: This structure holds socket address information,
such as IP address and port number. sockaddr_in is specifically used for
IPv4 addresses.
• socklen_t addr_size;
socklen_t addr_size: This variable stores the size of the address structure.
It's often used with functions like accept() and recvfrom() to specify the
size of the buffer for incoming address information.
• char buffer[1024];
This character array (buffer) is allocated to store data received from the
socket. It has a size of 1024 bytes, which is a common buffer size
int main(){

char *ip = "127.0.0.1";


int port = 5566;

int sock;
struct sockaddr_in addr;
socklen_t addr_size;
char buffer[1024];
int n;
sock = socket(AF_INET,
SOCK_STREAM, 0);
• socket
• This is the function used to create a socket.
• It returns a socket descriptor (an integer) that uniquely identifies the
created socket.
• This descriptor will be used in subsequent socket operations.
int main(){

char *ip = "127.0.0.1";


int port = 5566;

int sock;
struct sockaddr_in addr;
socklen_t addr_size;
char buffer[1024];
int n;
memset(&addr, '\0', sizeof(addr));
memset(&addr, '\0', sizeof(addr));

memset: This function is used to fill a block of memory with


a particular value.
In this case, it's being used to set all bytes of the addr
structure to 0.
This ensures that any uninitialized fields are properly set to
0.
int main(){

char *ip = "127.0.0.1";


int port = 5566;

int sock;
struct sockaddr_in addr;
socklen_t addr_size;
char buffer[1024];
int n;
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_family = AF_INET;
addr.sin_family

This field specifies the address


family of the socket, which was set
to AF_INET.

It indicates that the address


int main(){

char *ip = "127.0.0.1";


int port = 5566;

int sock;
struct sockaddr_in addr;
socklen_t addr_size;
char buffer[1024];
int n;
memset(&addr, '\0', sizeof(addr));
this block of code initializes the addr structure
addr.sin_family = AF_INET; with the necessary information (address
addr.sin_port = port; family, port number, and IP address) required
to establish a socket connection.
addr.sin_addr.s_addr = inet_addr(ip);
connect(sock, (struct sockaddr*)&addr,
sizeof(addr));
printf("Connected to the server.\n");
connect: This function establishes a connection to a remote socket specified by
the address (addr) provided.

It takes three parameters:

sock: The socket descriptor created earlier using the socket() function.
(struct sockaddr*)&addr: The address of the remote socket to which the
connection is being established.

It needs to be cast to a struct sockaddr* because connect() expects a generic


pointer to a socket address structure (sockaddr). In this case, it's cast to (struct
sockaddr*) because addr is of type sockaddr_in.

sizeof(addr): The size of the address structure (addr) being passed to connect().
It ensures that connect() knows the size of the address structure.
int main(){

char *ip = "127.0.0.1";


int port = 5566;

int sock;
struct sockaddr_in addr;
socklen_t addr_size;
char buffer[1024];
int n;
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = inet_addr(ip);
bzero(buffer, 1024);
strcpy(buffer, "HELLO, THIS IS CLIENT.");
printf("Client: %s\n", buffer);
send(sock, buffer, strlen(buffer), 0);
Socket
Programming
Server Program
Server Program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main(){

char *ip = "127.0.0.1";


int port = 5566;
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
char buffer[1024];
int n;
memset(&server_addr, '\0', sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);

You might also like