SlideShare a Scribd company logo
1 of 29
www.techvilla.org.in
TECHVILLA
www.techvilla.org.in
www.techvilla.org.in
Linux networking
Socket programming concepts.
Writing socket program code
www.techvilla.org.in
Socket Programming
Table of Contents
1. Network Application Programming
Interface: Sockets and Internet Sockets
2. Network Programming Tips
3. Client-Server Architecture
4. Example: Client Programming
5. Example: Server Programming
6. Network Programmer’s Mistakes
3
www.techvilla.org.in
Layers of the IP Protocol Suite 4
Link Layer
Transport Layer
Network Layer
Application Layer
Link Layer
Transport Layer
Network Layer
Application Layer
Ethernet
e.g. ftp
e.g. TCP, UDP
e.g. IP
www.techvilla.org.in
Protocol Suite Location
Internet Protocol Layer
5
Link Layer
Transport Layer (TCP, UDP)
Network Layer (IP)
Application Layer
Network Card &
Device Driver
(e.g. Ethernet card)
Operating System
(e.g. Unix)
Applications
(e.g. browser, game, ftp)
Application Programming
Interface (API)
(e.g. network API)
Interface to the Network Card
Location
www.techvilla.org.in
Network API
Operating system provides Application
Programming Interface (API) for network application
API is defined by a set of function types, data
structures, and constants
Desirable characteristics of the network interface
 Simple to use
 Flexible
 independent from any application
 allows program to use all functionality of the network
 Standardized
 allows programmer to learn once, write anywhere
Application Programming Interface for networks is
called socket
6
www.techvilla.org.in
Sockets
Sockets provide mechanisms to communicate
between computers across a network
There are different kind of sockets
DARPA Internet addresses (Internet Sockets)
Unix interprocess communication (Unix Sockets)
CCITT X.25 addresses
and many others
Berkeley sockets is the most popular Internet Socket
 runs on Linux, FreeBSD, OS X, Windows
 fed by the popularity of TCP/IP
7
www.techvilla.org.in
Internet Sockets
Support stream and datagram packets (e.g. TCP,
UDP, IP)
Is Similar to UNIX file I/O API (provides a file
descriptor)
Based on C, single thread model
 does not require multiple threads
8
www.techvilla.org.in
Types of Internet Sockets
Different types of sockets implement different
communication types (stream vs. datagram)
Type of socket: stream socket
 connection-oriented
 two way communication
 reliable (error free), in order delivery
 can use the Transmission Control Protocol (TCP)
 e.g. telnet, ssh, http
Type of socket: datagram socket
 connectionless, does not maintain an open
connection, each packet is independent
 can use the User Datagram Protocol (UDP)
9
www.techvilla.org.in
Network Programming Tips
Byte Ordering
Naming
Addressing
10
www.techvilla.org.in
Byte Ordering of Integers
Different CPU architectures have different
byte ordering
11
D3
high-order byte low-order byte
memory
address A
memory
address A +1
Stored at little-endian computer
Stored at big-endian computer low-order byte high-order byte
F2Integer representation (2 byte)
www.techvilla.org.in
Byte Ordering Problem
Question: What would happen if two
computers with different integer byte ordering
communicate?
12
Message in Memory of
of big-endian Computer
Message is sent
across Network 48 45 4C 4C 6F 01 00
Message is:
[Hello,1]
Message is: [Hello,256]
48 45 4C 4C 6F 01 00
Message in Memory of
little-endian Computer
Answer:
Nothing if they do not exchange integers!
But: If they exchange integers, they would get the
wrong order of bytes, therefore, the wrong value!
Example:
www.techvilla.org.in
Byte Ordering Solution
There are two solutions if computers with different byte
ordering system want to communicate
 They must know the kind of architecture of the sending
computer
(bad solution, it has not been implemented)
 Introduction of a network byte order. The functions are:
uint16_t htons(uint16_t host16bitvalue)
uint32_t htonl(uint32_t host32bitvalue)
uint16_t ntohs(uint16_t net16bitvalue)
uint32_t ntohs(uint32_t net32bitvalue)
Note: use for all integers (short and long), which
are sent across the network
Including port numbers and IP addresses
13
www.techvilla.org.in
Network Programming Tips
Byte Ordering
Naming
Addressing
14
www.techvilla.org.in
Naming and Addressing
Host name
identifies a single host (see Domain Name System
slides)
variable length string (e.g. www.berkeley.edu)
is mapped to one or more IP addresses
IP Address
written as dotted octets (e.g. 10.0.0.1)
32 bits. Not a number! But often needs to be
converted to a 32-bit to use.
Port number
identifies a process on a host
16 bit number
15
www.techvilla.org.in
Client-Server Architecture
Client requests service from server
Server responds with sending service or error message to client
16
Client Server
request
response
www.techvilla.org.in
Simple Client-Server Example 17
Client Server
request
response
socket()
connect()
send()
recv()
close()
socket()
bind()
listen()
accept()
recv()
send()
recv()
close()
Connection
establishment
Data response
Data request
End-of-file notification
www.techvilla.org.in
Example: Client Programming
Create stream socket (socket() )
Connect to server (connect() )
While still connected:
 send message to server (send() )
 receive (recv() ) data from server and process it
Close TCP connection and Socket (close())
18
www.techvilla.org.in
socket(): Initializing Socket
Getting the file descriptor
int chat_sock;
if ((chat_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
printf("Failed to create socketn");
abort ();
}
1.parameter specifies protocol/address family
2.parameter specifies the socket type
Other possibilities: SOCK_DGRAM
3.parameter specifies the protocol.
0 means protocol is chosen by the OS.
19
www.techvilla.org.in
IP Address Data Structure
struct sockaddr_in {
short int sin_family; // Address family
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8];
};
struct in_addr {
unsigned long s_addr; // 4 bytes
};
Padding of sin_zeros: struct sockaddr_in has same size as struct
sockaddr
20
www.techvilla.org.in
connect(): Making TCP Connection
to Serverstruct sockaddr_in sin;
struct hostent *host = gethostbyname (argv[1]);
unsigned int server_address = *(unsigned long *) host->h_addr_list[0];
unsigned short server_port = atoi (argv[2]);
memset (&sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = server_address;
sin.sin_port = htons (server_port);
if (connect(chat_sock, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror("connect");
printf("Cannot connect to servern");
abort();
}
21
www.techvilla.org.in
send(): Sending Packets
int send_packets(char *buffer, int buffer_len) {
sent_bytes = send(chat_sock, buffer, buffer_len, 0);
if (send_bytes < 0) {
perror (“send");
}
return 0;
}
Needs socket descriptor,
Buffer containing the message, and
Length of the message
Can also use write()
22
www.techvilla.org.in
Receiving Packets:
Separating Data in a Stream
Use records (data structures) to partition the data
stream
23
B
Fixed length
record
Fixed length
record
0 1 3
C
2 94 6 87
D
5
A
receive
buffer
slide through
www.techvilla.org.in
Receiving Packets
int receive_packets(char *buffer, int buffer_len, int *bytes_read)
{
int left = buffer_len - *bytes_read;
received = recv(chat_sock, buffer + *bytes_read, left, 0);
if (received < 0) {
perror (“recv");
}
if (received <= 0) {
return close_connection();
}
*bytes_read += received;
while (*bytes_read > RECORD_LEN) {
process_packet(buffer, RECORD_LEN);
*bytes_read -= RECORD_LEN;
memmove(buffer, buffer + RECORD_LEN, *bytes_read);
}
return 0;
}
24
Can also use read()
buffer
*bytes_read
buffer_len
www.techvilla.org.in
Server Programming: Simple
Create stream socket (socket() )
Bind port to socket (bind() )
Listen for new client (listen() )
While
 accept user connection and create a new socket (accept()
)
 data arrives from client (recv() )
 data has to be send to client (send() )
25
www.techvilla.org.in
bind(): Assign IP and Port
struct sockaddr_in sin;
struct hostent *host = gethostbyname (argv[1]);
unsigned int server_address = *(unsigned long *) host->h_addr_list[0];
unsigned short server_port = atoi (argv[2]);
memset (&sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = server_address;
sin.sin_port = htons (server_port);
if (bind(chat_sock, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror("bind");
printf("Cannot bind server application to networkn");
abort();
26
www.techvilla.org.in
bind():
bind() tells the OS to assign a local IP address and local port
number to the socket.
Many applications let the OS choose an IP address.
Use wildcard INADDR_ANY as local address in this case.
At server, user process must call bind() to assign a port
At client, bind() is not required since OS may assign available port
and IP address
 The server will get the port number of the client
through the UDP/TCP packet header
Note: Each application is represented by a server port number
27
www.techvilla.org.in
listen(): Wait for Connections
int listen(int sockfd, int backlog);
Puts socket in a listening state, willing to
handle incoming TCP connection request.
Backlog: number of TCP connections that
can be queued at the socket.
28
www.techvilla.org.in

More Related Content

What's hot (20)

Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Elementary TCP Sockets
Elementary TCP SocketsElementary TCP Sockets
Elementary TCP Sockets
 
Tcp sockets
Tcp socketsTcp sockets
Tcp sockets
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Sockets
SocketsSockets
Sockets
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programming
 
Introduction to tcp ip linux networking
Introduction to tcp ip   linux networkingIntroduction to tcp ip   linux networking
Introduction to tcp ip linux networking
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Java sockets
Java socketsJava sockets
Java sockets
 
Os 2
Os 2Os 2
Os 2
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 

Similar to Raspberry pi Part 23

Similar to Raspberry pi Part 23 (20)

Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Sockets
Sockets Sockets
Sockets
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Java 1
Java 1Java 1
Java 1
 
Sockets
SocketsSockets
Sockets
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
#1 (TCPvs. UDP)
#1 (TCPvs. UDP)#1 (TCPvs. UDP)
#1 (TCPvs. UDP)
 
Python networking
Python networkingPython networking
Python networking
 
Socket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdfSocket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdf
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
 
socket programming
 socket programming  socket programming
socket programming
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
sockets_intro.ppt
sockets_intro.pptsockets_intro.ppt
sockets_intro.ppt
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming Clients
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming Clients
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 

More from Techvilla

Raspberry pi Part 26
Raspberry pi Part 26Raspberry pi Part 26
Raspberry pi Part 26Techvilla
 
Raspberry pi Part 25
Raspberry pi Part 25Raspberry pi Part 25
Raspberry pi Part 25Techvilla
 
Raspberry pi Part 24
Raspberry pi Part 24Raspberry pi Part 24
Raspberry pi Part 24Techvilla
 
Raspberry pi Part 22
Raspberry pi Part 22Raspberry pi Part 22
Raspberry pi Part 22Techvilla
 
Raspberry pi Part 21
Raspberry pi Part 21Raspberry pi Part 21
Raspberry pi Part 21Techvilla
 
Raspberry pi Part 20
Raspberry pi Part 20Raspberry pi Part 20
Raspberry pi Part 20Techvilla
 
Raspberry pi Part 19
Raspberry pi Part 19Raspberry pi Part 19
Raspberry pi Part 19Techvilla
 
Raspberry pi Part 18
Raspberry pi Part 18Raspberry pi Part 18
Raspberry pi Part 18Techvilla
 
Raspberry pi Part 17
Raspberry pi Part 17Raspberry pi Part 17
Raspberry pi Part 17Techvilla
 
Raspberry pi Part 16
Raspberry pi Part 16Raspberry pi Part 16
Raspberry pi Part 16Techvilla
 
Rasperry pi Part 15
Rasperry pi Part 15Rasperry pi Part 15
Rasperry pi Part 15Techvilla
 
Rasperry pi Part 13
Rasperry pi Part 13Rasperry pi Part 13
Rasperry pi Part 13Techvilla
 
Rasperry pi Part 12
Rasperry pi Part 12Rasperry pi Part 12
Rasperry pi Part 12Techvilla
 
Rasperry pi Part 10
Rasperry pi Part 10Rasperry pi Part 10
Rasperry pi Part 10Techvilla
 
Rasperry pi Part 9
Rasperry pi Part 9Rasperry pi Part 9
Rasperry pi Part 9Techvilla
 
Rasperry pi Part 8
Rasperry pi Part 8Rasperry pi Part 8
Rasperry pi Part 8Techvilla
 
Rasperry pi Part 7
Rasperry pi Part 7Rasperry pi Part 7
Rasperry pi Part 7Techvilla
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6Techvilla
 
Raspberry pi Part 5
Raspberry pi Part 5Raspberry pi Part 5
Raspberry pi Part 5Techvilla
 
Raspberry pi Part 4
Raspberry pi Part 4Raspberry pi Part 4
Raspberry pi Part 4Techvilla
 

More from Techvilla (20)

Raspberry pi Part 26
Raspberry pi Part 26Raspberry pi Part 26
Raspberry pi Part 26
 
Raspberry pi Part 25
Raspberry pi Part 25Raspberry pi Part 25
Raspberry pi Part 25
 
Raspberry pi Part 24
Raspberry pi Part 24Raspberry pi Part 24
Raspberry pi Part 24
 
Raspberry pi Part 22
Raspberry pi Part 22Raspberry pi Part 22
Raspberry pi Part 22
 
Raspberry pi Part 21
Raspberry pi Part 21Raspberry pi Part 21
Raspberry pi Part 21
 
Raspberry pi Part 20
Raspberry pi Part 20Raspberry pi Part 20
Raspberry pi Part 20
 
Raspberry pi Part 19
Raspberry pi Part 19Raspberry pi Part 19
Raspberry pi Part 19
 
Raspberry pi Part 18
Raspberry pi Part 18Raspberry pi Part 18
Raspberry pi Part 18
 
Raspberry pi Part 17
Raspberry pi Part 17Raspberry pi Part 17
Raspberry pi Part 17
 
Raspberry pi Part 16
Raspberry pi Part 16Raspberry pi Part 16
Raspberry pi Part 16
 
Rasperry pi Part 15
Rasperry pi Part 15Rasperry pi Part 15
Rasperry pi Part 15
 
Rasperry pi Part 13
Rasperry pi Part 13Rasperry pi Part 13
Rasperry pi Part 13
 
Rasperry pi Part 12
Rasperry pi Part 12Rasperry pi Part 12
Rasperry pi Part 12
 
Rasperry pi Part 10
Rasperry pi Part 10Rasperry pi Part 10
Rasperry pi Part 10
 
Rasperry pi Part 9
Rasperry pi Part 9Rasperry pi Part 9
Rasperry pi Part 9
 
Rasperry pi Part 8
Rasperry pi Part 8Rasperry pi Part 8
Rasperry pi Part 8
 
Rasperry pi Part 7
Rasperry pi Part 7Rasperry pi Part 7
Rasperry pi Part 7
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6
 
Raspberry pi Part 5
Raspberry pi Part 5Raspberry pi Part 5
Raspberry pi Part 5
 
Raspberry pi Part 4
Raspberry pi Part 4Raspberry pi Part 4
Raspberry pi Part 4
 

Recently uploaded

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...Call girls in Ahmedabad High profile
 

Recently uploaded (20)

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
 

Raspberry pi Part 23

  • 2. www.techvilla.org.in Linux networking Socket programming concepts. Writing socket program code
  • 3. www.techvilla.org.in Socket Programming Table of Contents 1. Network Application Programming Interface: Sockets and Internet Sockets 2. Network Programming Tips 3. Client-Server Architecture 4. Example: Client Programming 5. Example: Server Programming 6. Network Programmer’s Mistakes 3
  • 4. www.techvilla.org.in Layers of the IP Protocol Suite 4 Link Layer Transport Layer Network Layer Application Layer Link Layer Transport Layer Network Layer Application Layer Ethernet e.g. ftp e.g. TCP, UDP e.g. IP
  • 5. www.techvilla.org.in Protocol Suite Location Internet Protocol Layer 5 Link Layer Transport Layer (TCP, UDP) Network Layer (IP) Application Layer Network Card & Device Driver (e.g. Ethernet card) Operating System (e.g. Unix) Applications (e.g. browser, game, ftp) Application Programming Interface (API) (e.g. network API) Interface to the Network Card Location
  • 6. www.techvilla.org.in Network API Operating system provides Application Programming Interface (API) for network application API is defined by a set of function types, data structures, and constants Desirable characteristics of the network interface  Simple to use  Flexible  independent from any application  allows program to use all functionality of the network  Standardized  allows programmer to learn once, write anywhere Application Programming Interface for networks is called socket 6
  • 7. www.techvilla.org.in Sockets Sockets provide mechanisms to communicate between computers across a network There are different kind of sockets DARPA Internet addresses (Internet Sockets) Unix interprocess communication (Unix Sockets) CCITT X.25 addresses and many others Berkeley sockets is the most popular Internet Socket  runs on Linux, FreeBSD, OS X, Windows  fed by the popularity of TCP/IP 7
  • 8. www.techvilla.org.in Internet Sockets Support stream and datagram packets (e.g. TCP, UDP, IP) Is Similar to UNIX file I/O API (provides a file descriptor) Based on C, single thread model  does not require multiple threads 8
  • 9. www.techvilla.org.in Types of Internet Sockets Different types of sockets implement different communication types (stream vs. datagram) Type of socket: stream socket  connection-oriented  two way communication  reliable (error free), in order delivery  can use the Transmission Control Protocol (TCP)  e.g. telnet, ssh, http Type of socket: datagram socket  connectionless, does not maintain an open connection, each packet is independent  can use the User Datagram Protocol (UDP) 9
  • 10. www.techvilla.org.in Network Programming Tips Byte Ordering Naming Addressing 10
  • 11. www.techvilla.org.in Byte Ordering of Integers Different CPU architectures have different byte ordering 11 D3 high-order byte low-order byte memory address A memory address A +1 Stored at little-endian computer Stored at big-endian computer low-order byte high-order byte F2Integer representation (2 byte)
  • 12. www.techvilla.org.in Byte Ordering Problem Question: What would happen if two computers with different integer byte ordering communicate? 12 Message in Memory of of big-endian Computer Message is sent across Network 48 45 4C 4C 6F 01 00 Message is: [Hello,1] Message is: [Hello,256] 48 45 4C 4C 6F 01 00 Message in Memory of little-endian Computer Answer: Nothing if they do not exchange integers! But: If they exchange integers, they would get the wrong order of bytes, therefore, the wrong value! Example:
  • 13. www.techvilla.org.in Byte Ordering Solution There are two solutions if computers with different byte ordering system want to communicate  They must know the kind of architecture of the sending computer (bad solution, it has not been implemented)  Introduction of a network byte order. The functions are: uint16_t htons(uint16_t host16bitvalue) uint32_t htonl(uint32_t host32bitvalue) uint16_t ntohs(uint16_t net16bitvalue) uint32_t ntohs(uint32_t net32bitvalue) Note: use for all integers (short and long), which are sent across the network Including port numbers and IP addresses 13
  • 14. www.techvilla.org.in Network Programming Tips Byte Ordering Naming Addressing 14
  • 15. www.techvilla.org.in Naming and Addressing Host name identifies a single host (see Domain Name System slides) variable length string (e.g. www.berkeley.edu) is mapped to one or more IP addresses IP Address written as dotted octets (e.g. 10.0.0.1) 32 bits. Not a number! But often needs to be converted to a 32-bit to use. Port number identifies a process on a host 16 bit number 15
  • 16. www.techvilla.org.in Client-Server Architecture Client requests service from server Server responds with sending service or error message to client 16 Client Server request response
  • 17. www.techvilla.org.in Simple Client-Server Example 17 Client Server request response socket() connect() send() recv() close() socket() bind() listen() accept() recv() send() recv() close() Connection establishment Data response Data request End-of-file notification
  • 18. www.techvilla.org.in Example: Client Programming Create stream socket (socket() ) Connect to server (connect() ) While still connected:  send message to server (send() )  receive (recv() ) data from server and process it Close TCP connection and Socket (close()) 18
  • 19. www.techvilla.org.in socket(): Initializing Socket Getting the file descriptor int chat_sock; if ((chat_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); printf("Failed to create socketn"); abort (); } 1.parameter specifies protocol/address family 2.parameter specifies the socket type Other possibilities: SOCK_DGRAM 3.parameter specifies the protocol. 0 means protocol is chosen by the OS. 19
  • 20. www.techvilla.org.in IP Address Data Structure struct sockaddr_in { short int sin_family; // Address family unsigned short int sin_port; // Port number struct in_addr sin_addr; // Internet address unsigned char sin_zero[8]; }; struct in_addr { unsigned long s_addr; // 4 bytes }; Padding of sin_zeros: struct sockaddr_in has same size as struct sockaddr 20
  • 21. www.techvilla.org.in connect(): Making TCP Connection to Serverstruct sockaddr_in sin; struct hostent *host = gethostbyname (argv[1]); unsigned int server_address = *(unsigned long *) host->h_addr_list[0]; unsigned short server_port = atoi (argv[2]); memset (&sin, 0, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = server_address; sin.sin_port = htons (server_port); if (connect(chat_sock, (struct sockaddr *) &sin, sizeof (sin)) < 0) { perror("connect"); printf("Cannot connect to servern"); abort(); } 21
  • 22. www.techvilla.org.in send(): Sending Packets int send_packets(char *buffer, int buffer_len) { sent_bytes = send(chat_sock, buffer, buffer_len, 0); if (send_bytes < 0) { perror (“send"); } return 0; } Needs socket descriptor, Buffer containing the message, and Length of the message Can also use write() 22
  • 23. www.techvilla.org.in Receiving Packets: Separating Data in a Stream Use records (data structures) to partition the data stream 23 B Fixed length record Fixed length record 0 1 3 C 2 94 6 87 D 5 A receive buffer slide through
  • 24. www.techvilla.org.in Receiving Packets int receive_packets(char *buffer, int buffer_len, int *bytes_read) { int left = buffer_len - *bytes_read; received = recv(chat_sock, buffer + *bytes_read, left, 0); if (received < 0) { perror (“recv"); } if (received <= 0) { return close_connection(); } *bytes_read += received; while (*bytes_read > RECORD_LEN) { process_packet(buffer, RECORD_LEN); *bytes_read -= RECORD_LEN; memmove(buffer, buffer + RECORD_LEN, *bytes_read); } return 0; } 24 Can also use read() buffer *bytes_read buffer_len
  • 25. www.techvilla.org.in Server Programming: Simple Create stream socket (socket() ) Bind port to socket (bind() ) Listen for new client (listen() ) While  accept user connection and create a new socket (accept() )  data arrives from client (recv() )  data has to be send to client (send() ) 25
  • 26. www.techvilla.org.in bind(): Assign IP and Port struct sockaddr_in sin; struct hostent *host = gethostbyname (argv[1]); unsigned int server_address = *(unsigned long *) host->h_addr_list[0]; unsigned short server_port = atoi (argv[2]); memset (&sin, 0, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = server_address; sin.sin_port = htons (server_port); if (bind(chat_sock, (struct sockaddr *) &sin, sizeof (sin)) < 0) { perror("bind"); printf("Cannot bind server application to networkn"); abort(); 26
  • 27. www.techvilla.org.in bind(): bind() tells the OS to assign a local IP address and local port number to the socket. Many applications let the OS choose an IP address. Use wildcard INADDR_ANY as local address in this case. At server, user process must call bind() to assign a port At client, bind() is not required since OS may assign available port and IP address  The server will get the port number of the client through the UDP/TCP packet header Note: Each application is represented by a server port number 27
  • 28. www.techvilla.org.in listen(): Wait for Connections int listen(int sockfd, int backlog); Puts socket in a listening state, willing to handle incoming TCP connection request. Backlog: number of TCP connections that can be queued at the socket. 28

Editor's Notes

  1. Lecture 1