SlideShare a Scribd company logo
UNIT 2
SOCKET PROGRAMMING
INTRODUCTION:
Programming with TCP/IP sockets:
Step 1. Create a socket:
Cont…
DEMO TO CREATE SOCKET:
#include <stdio.h> /* defines printf */
#include <stdlib.h> /* defines exit and other sys calls */
#include <sys/socket.h>
main(int argc, char **argv)
{
int fd;
/* create a tcp/ip socket */
/* request the Internet address protocol */
/* and a reliable 2-way byte stream (TCP/IP) */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("cannot create socket");
return 0;
}
printf("created socket: descriptor=%dn", fd);
exit(0);
}
Step 2. Identify (name) a socket:
Cont…
Cont..
Cont…
Cont…
Cont…
Code:
#include <stdlib.h> /* defines system calls */
#include <stdio.h> /* needed for printf */
#include <string.h> /* needed for memset */
#include <sys/socket.h>
#include <netinet/in.h> /* needed for sockaddr_in */
main(int argc, char **argv)
{
struct sockaddr_in myaddr; /* our address */
int fd; /* our socket */
unsigned int alen; /* length of address (for getsockname) */
/* create a tcp/ip socket */
/* request the Internet address protocol */
/* and a reliable 2-way byte stream (TCP/IP) */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("cannot create socket");
return 0;
}
printf("created socket: descriptor = %dn", fd);
Cont…
/* bind to an arbitrary return address */
/* in this case, assume it's the client side, so we won't
/* care about the port number as no application will connect here */
/* INADDR_ANY is the IP address and 0 is the socket */
/* htonl converts a long integer (e.g. address) to a network */
/* representation (IP-standard byte ordering) */
memset((void *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(0);
if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
perror("bind failed");
return 0;
}
alen = sizeof(myaddr);
if (getsockname(fd, (struct sockaddr *)&myaddr, &alen) < 0) {
perror("getsockname failed");
return 0;
}
printf("bind complete. Port number = %dn", ntohs(myaddr.sin_port));
exit(0);
}
Number Conversion:
Cont..
Step 3a. Connect to a server from a client
Cont…
Cont…
Cont…
Cont…
Cont…
Step 3b. Accept connections on the server
Cont…
Step 4. Communicate
Cont…
Cont…
Step 5. Close the connection
Synchronous or Asynchronous?
POSIX DATATYPES:
Posix Datatypes:
Cont…
Socket Addresses:
Cont…
Cont…
Cont…
Assigning Address To Socket:
Cont…
JAVA Socket Programming:
Socket Class:
Server Socket Class:
Example Of JAVA Server Programming:
Socket  Programming Intro.pptx
Socket  Programming Intro.pptx
Thread Programming:
Lifecycle Of Thread:
Following are the stages of the life cycle βˆ’
β€’ New βˆ’ A new thread begins its life cycle in the new state. It remains in this state until the program starts the
thread. It is also referred to as aborn thread.
β€’ Runnable βˆ’ After a newly born thread is started, the thread becomes runnable. A thread in this state is
considered to be executing its task.
β€’ Waiting βˆ’ Sometimes, a thread transitions to the waiting state while the thread waits for another thread to
perform a task. A thread transitions back to the runnable state only when another thread signals the waiting
thread to continue executing.
β€’ Timed Waiting βˆ’ A runnable thread can enter the timed waiting state for a specified interval of time. A thread in
this state transitions back to the runnable state when that time interval expires or when the event it is waiting
for occurs.
β€’ Terminated (Dead) βˆ’ A runnable thread enters the terminated state when it completes its task or otherwise
terminates.
Thread Priorities:
Create a Thread by Implementing a Runnable
Interface:
β€’ Step 3
Once a Thread object is created, you can start it by calling start() method, which executes a call to run( )
method. Following is a simple syntax of start() method βˆ’
void start();
Create a Thread by Extending a Thread Class:
Berkeley Sockets:
Socket API Functions:
Cont…
Cont…
Berkeley sockets can operate in one of two
modes: blocking or non-blocking.
Socket Address Structure:
IPv4 Socket Address Structure:
Datatypes:
Cont..
Generic Socket Address Structure
IPv6 Socket Address Structure:
Cont…
Comparison of socket address structure:
Value Result Arguments:
Cont…
Cont…
Cont…
With variable length socket address structure, the value returned can be less than the maximum size of
the structure.
Byte Order functions:
Cont…
Cont…
Byte Manipulation Functions:
Cont…
Cont…
Cont..
Address Conversion Functions:
Cont…
Cont…
Cont…
Elementary socket system calls:
Cont…
Bind:
Connect:
Listen:
Accept:
send, sendto, recv and recvfrom:
Close :
TCP ports (ephemeral, reserved):
Asynchronous I/O
Asynchronous I/O advantages:
Advantages :
Asynchronous I/O API’s:
Cont…
How asynchronous I/O works?
Cont…
Asynchronous I/O structure
Socket  Programming Intro.pptx
I/O Multiplexing Models:
Cont…
Select Function :
Cont…
Poll Function:
Cont…
Figure Shows Input events and returned revents
for poll.
fcntl Function:
Cont…
Cont…
Cont…
Unix Domain Protocols:
Cont…
Cont…
Unix Domain Socket Address Structure:
Cont…

More Related Content

PPT
Sockets
PPT
03-socketprogramming for college students.ppt
PPT
03-socketprogrsamming forcoleeger students.ppt
PPTX
L5-Sockets.pptx
PPT
sockets_intro.ppt
PPT
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
PDF
lab04.pdf
PPTX
Socket programming
Sockets
03-socketprogramming for college students.ppt
03-socketprogrsamming forcoleeger students.ppt
L5-Sockets.pptx
sockets_intro.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
lab04.pdf
Socket programming

Similar to Socket Programming Intro.pptx (20)

PDF
Socket programming
PDF
Sockets
PPTX
Basics of sockets
PPT
Sockets intro
PDF
Socket programming using C
PPT
Socket Programming
PPT
Socket System Calls
PPT
Sockets in unix
PDF
PPT
Introduction to sockets tcp ip protocol.ppt
PPT
Multiplayer Game Programming Berkeley Socket API Chapter 3.ppt
PPTX
IPC SOCKET
PPT
Socket programming-tutorial-sk
PPT
Socket programming in C
PDF
Network Programming Assignment Help
PPTX
Lecture 1 Socket programming elementary tcp sockets.pptx
DOCX
Udp socket programming(Florian)
PPT
Basic socket programming
PPTX
Byte Ordering - Unit 2.pptx
PPT
Socket Programming
Socket programming
Sockets
Basics of sockets
Sockets intro
Socket programming using C
Socket Programming
Socket System Calls
Sockets in unix
Introduction to sockets tcp ip protocol.ppt
Multiplayer Game Programming Berkeley Socket API Chapter 3.ppt
IPC SOCKET
Socket programming-tutorial-sk
Socket programming in C
Network Programming Assignment Help
Lecture 1 Socket programming elementary tcp sockets.pptx
Udp socket programming(Florian)
Basic socket programming
Byte Ordering - Unit 2.pptx
Socket Programming
Ad

Recently uploaded (20)

PDF
Introduction to the IoT system, how the IoT system works
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PDF
Testing WebRTC applications at scale.pdf
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPTX
Internet___Basics___Styled_ presentation
PDF
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
Sims 4 Historia para lo sims 4 para jugar
PPTX
E -tech empowerment technologies PowerPoint
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Β 
PDF
The Internet -By the Numbers, Sri Lanka Edition
Β 
PDF
Paper PDF World Game (s) Great Redesign.pdf
Introduction to the IoT system, how the IoT system works
Job_Card_System_Styled_lorem_ipsum_.pptx
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
Testing WebRTC applications at scale.pdf
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
Design_with_Watersergyerge45hrbgre4top (1).ppt
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
SAP Ariba Sourcing PPT for learning material
An introduction to the IFRS (ISSB) Stndards.pdf
SASE Traffic Flow - ZTNA Connector-1.pdf
Internet___Basics___Styled_ presentation
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
introduction about ICD -10 & ICD-11 ppt.pptx
Sims 4 Historia para lo sims 4 para jugar
E -tech empowerment technologies PowerPoint
PptxGenJS_Demo_Chart_20250317130215833.pptx
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Β 
The Internet -By the Numbers, Sri Lanka Edition
Β 
Paper PDF World Game (s) Great Redesign.pdf
Ad

Socket Programming Intro.pptx