0% found this document useful (0 votes)
35 views18 pages

Sockets

A socket is an endpoint for communication between two machines that uses a combination of IP address and port number. It provides an API for applications to connect over networks using protocols like TCP and UDP. Key system calls for sockets include socket(), bind(), listen(), accept(), connect(), send(), recv() which are used to establish and manage connections between client and server applications.

Uploaded by

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

Sockets

A socket is an endpoint for communication between two machines that uses a combination of IP address and port number. It provides an API for applications to connect over networks using protocols like TCP and UDP. Key system calls for sockets include socket(), bind(), listen(), accept(), connect(), send(), recv() which are used to establish and manage connections between client and server applications.

Uploaded by

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

Socket Programming

A Socket is a :-
• It is an API

• It is an end-end connection point


between two machines

• Socket address is a combination of IP


address and the port number
The Socket Interface
socket socket
Application Application
interface interface
1 2

user user

kern kern
el el
Socket Socket

Underlying Underlying
communicati communicati
on Protocols on Protocols

Communication
s network
Port numbers
Well known ports:-
• 0-1024

Registered ports
• 1024-49151

Dynamic ports
• 49152-65535
System calls for connection
oriented protocol [TCP/IP]
Server side system calls :-
• Socket
• Bind
• Listen
• Accept
• Read
• Write
System calls for connection
oriented protocol [TCP/IP]
Client side system calls :-

• Socket
• Connect
• Write
• Read
Server
TCP client-server
socket()

bind()

Client
listen()

accept()
socket()

blocks until server receives


a connect request from client connect negotiation connect()

data write()
read()

write() data
read()

close()
close()

Networks: TCP/IP 6
Socket Calls
System calls for connection less
protocol [UDP/IP]
Server side system calls :-

• Socket
• Bind
• Recvfrom
• Sendto
System calls for connection less
protocol [UDP/IP]
Client side system calls :-

• Socket
• Bind
• Sendto
• recvfrom
UDP Socket Calls
Server
socket()
Client
socket()
bind()

bind() Not needed


recvfrom()

blocks until server


receives data from client data sendto()

sendto() data
recvfrom()

close()
close()

Networks: UDP/IP
Socket Calls
Socket programming
# include <sys/socket.h>

struct in_addr {
in_addr_t s_addr ;
};

4 bytes in size/32 bit IP address


Socket programming
struct sockaddr_in
{
uin8_t sin_len; /* structure length ,16 */

sa_family_t sin_family; /* AF/PF */

in_port_t sin_port; /* TCP,UDP port,16 bit TCP/UDP


port number */

struct in_addr sin_addr; /* 32 bit IPv4 addr */

char sin_zero[8]; /* unused */


};
Socket programming
#include <sys/types.h>
#include <sys/socket.h>

int socket(int family,int type,int


protocol );
Descriptor if OK,-1 on error.
Family : AF/PF
Type : SOCK_STREAM,SOCK_DGRAM,SOCK_RAW
Protocol :TCP/IP or UDP/IP
Socket programming
#include <sys/types.h>
#include <sys/socket.h>

int bind (int sockfd,struct sockaddr


*myaddr,int addrlen);
0:OK and -1:error
Assigns local protocol address to a
socket,(IP address + port number).
Socket programming
#include <sys/types.h>
#include <sys/socket.h>
int connect (int sockfd,struct sockaddr
*servaddr,socklen_t addrlen);

• For connecting to the server.


Socket programming
#include <sys/types.h>
#include <sys/socket.h>

int listen (int sockfd,int backlog);


0:OK,-1:error
Listen converts an unconnected
socket to a passive socket…
Socket programming
#include <sys/types.h>
#include <sys/socket.h>

int accept (int sockfd,struct sockaddr


*peer,int *addrlen);
It creates a new socket,to which the
properties of old socket are passed.
Socket programming
#include <sys/types.h>
#include <sys/socket.h>

int close (int fd);


Socket programming
Threads

You might also like