0% found this document useful (0 votes)
9 views

Socket Programming

Uploaded by

toklesreeja
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Socket Programming

Uploaded by

toklesreeja
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Socket

Programming
160122733303
Socket Programming

It is a way to enable communication between two devices (typically a client and a server)
over a network. A socket provides an endpoint for sending or receiving data across the
network, using standard network protocols like TCP (Transmission Control Protocol) or UDP
(User Datagram Protocol).

There are two primary types of sockets:


Stream sockets (TCP): These provide reliable, connection-oriented
communication.
Datagram sockets (UDP): These provide connectionless unreliable
communication but are faster for certain use cases.

This mainly involves two machines client and the server.

 The server program listens for client requests and processes them.
 The client program initiates a connection to the server by specifying
the server's IP address and port number.
Steps Involved in Socket Programming

Steps involved in socket programming:

1. Server Side Steps:


Socket Creation:
Create a socket using socket().
Binding:
Bind the socket to an IP address and port using bind().
Listening:
Put the socket in passive mode using listen() to wait for incoming client connections.
Accepting Connections:
Accept an incoming connection using accept() (creates a new socket for the client).

C o m m u n i c a t io n :
U s e re a d ()/re c v () to re c e iv e d a ta f ro m th e c lie n t an d w r ite () / s e n d ( ) to s e n d
d a ta b ac k .
C lo sin g t h e So c ket :
C lo s e th e c o n n e c tio n u s in g c los e ().

2. Client Side Steps:

Socket Creation:
Create a socket using socket().

Connecting:
Connect to the server using connect() by providing the server's IP address and port.

Communication:
Use send() to send data to the server and recv() to receive data.

Closing the Socket:


Close the connection using close().

Key System calls:

Key System Calls:

socket(): Create a socket.


bind(): Bind a socket to an IP/port (for servers).
listen(): Put the socket in a listening state (for servers).
accept(): Accept a connection request from a client (for servers).
connect(): Request a connection to a server (for clients).
send()/recv(): Send and receive data over the socket.
close(): Close the socket connection.

You might also like