SlideShare a Scribd company logo
1 of 14
Value-Result Arguments
•Length of socket passed as an argument
•Method by which length is passed depends on which direction the structure is
being passed (from process to kernel, or vice versa)
Value-Result Arguments
•Value-only: bind, connect, sendto (from process to kernel)
•Value-Result: accept, recvfrom, getsockname, getpeername (from kernel to process,
pass a pointer to an integer containing size)
Tells process how much information kernel actually stored
struct sockaddr_in clientaddr ;
socklen_t len;
int listenfd, connectfd;
len = sizeof (clientaddr);
connectfd = accept (listenfd, (SA *) &clientaddr, &len);
Byte Ordering
 Port number and IP Address are represented by
multi byte data type , which are placed in packets for the
purpose of routing and multiplexing.
Port Number are two byte or 16 bit and IPv4 address
are 4 bytes or 32 bits.
Now the problem arrises when transfering multi byte
data types between different architecture.
Byte Ordering
 Let host A uses big endian architecture and sends a
packet across the network to host B which uses a little
endian architecture.
If host B looks at the address to see if the packet is for
him/her(choose a gender !) it will interprete the byte in
the opposite order and will wrongly conclude that it is
not his/ her packet.
The internet uses big endian and we call it the Network
Byte Order.
It is not important to know which method it uses, since
we have function to convert host-byte-order values in
network-byte-order values and reverse also.
Byte Ordering Functions
htonl : convert host to network, long integer
htons : convert host to network, short integer
ntohl : convert network to host, long integer
ntohs : convert network to host, short integer
Byte Ordering Functions 1/4
•Two ways to store 2 bytes (16-bit integer) in memory
Low-order byte at starting address  little-endian byte order
High-order byte at starting address  big-endian byte order
•in a big-endian computer  store 4F52
Stored as 4F52  4F is stored at storage address 1000, 52 will
be at address 1001, for example
•In a little-endian system  store 4F52
it would be stored as 524F (52 at address 1000, 4F at 1001)
•Byte order used by a given system known as host byte order
•Network programmers use network byte order
•Internet protocol uses big-endian byte ordering for integers (port number and IP
address)
Byte Ordering Functions 2/4
High-order byte low-order byte
MSB 16bit value LSB
High-order byte low-order byte
Increasing memory
address
Address A+1 Address A
Little-endian byte order:
big-endian byte order:
Address A+1
Address A
Increasing memory
address
Byte Ordering Functions 3/4
#include "unp.h"
int main(int argc, char **argv)
{
union {
short s;
char c[sizeof(short)];
} un;
un.s = 0x0102;
printf("%s: ", CPU_VENDOR_OS);
if (sizeof(short) == 2) {
if (un.c[0] == 1 && un.c[1] == 2)
printf("big-endiann");
else if (un.c[0] == 2 && un.c[1] == 1)
printf("little-endiann");
else
printf("unknownn");
} else
printf("sizeof(short) = %dn", sizeof(short));
exit(0);
}
•Sample program to figure out little-
endian or big-endian machine
•Source code in byteorder.c
Byte Ordering Functions 4/4
•To convert between byte orders
Return value in network byte order
htons (s for short word 2 bytes)
htonl (l for long word 4 bytes)
Return value in host byte order
ntohs
ntohl
•Must call appropriate function to convert between host and network byte order
•On systems that have the same ordering as the Internet protocols, four functions
usually defined as null macros
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(13);
Byte Manipulation Functions
#include <strings.h>
void bzero (void *dest, size_t nbytes);
// sets specified number of bytes to 0 in the destination
void bcopy (const void *src,void * dest, size_t nbytes);
// moves specified number of bytes from source to destination
void bcmp (const void *ptr1, const void *ptr2,size_t nbytes)
//compares two arbitrary byte strings, return value is zero if two byte strings are
identical, otherwise, nonzero
Address Conversion Functions 1/2
Convert an IPv4 address from a dotted-decimal string “206.168.112.96” to a 32-bit
network byte order binary value
#include <arpa/inet.h>
int inet_aton (const char* strptr, struct in_addr *addrptr);
// return 1 if string was valid, 0 on error. Address stored in *addrptr
in_addr_t inet_addr (const char * strptr);
// returns 32 bit binary network byte order IPv4 address, currently
deprecated
char * inet_nota (struct in_addr inaddr);
//returns pointer to dotted-decimal string
Address Conversion Functions 2/2
To handle both IPv4 and IPv6 addresses
#include <arpa/inet.h>
int inet_pton (int family, const char* strptr, void *addrptr);
// return 1 if OK, 0 on error. 0 if not a valid presentation, -1 on error,
Address stored in *addrptr
Const char * inet_ntop (int family, const void* addrptr, char *strptr, size_t len);
// return pointer to result if OK, NULL on error
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);
ptr = inet_ntop (AF_INET,&addr.sin_addr,str,sizeof(str));
Reading and Writing Functions 1/2
int send (int socket, char *message, int msg_len, int flags) (TCP)
int sendto (int socket, void *msg, int len, int flags, struct sockaddr *
to, int tolen ); (UDP)
int write(int socket, void *msg, int len); /* TCP */
int recv (int socket, char *buffer, int buf_len, int flags) (TCP)
int recvfrom(int socket, void *msg, int len, int flags, struct sockaddr
*from, int *fromlen); (UDP)
int read(int socket, void *msg, int len); (TCP)
Reading and Writing Functions 2/2
•Stream sockets (TCP sockets) exhibit a behavior with read and write that differs
from normal file I/O
•A read or write on a stream socket might input or output fewer bytes than
requested (not an error)
readn function
writen function
readline function

More Related Content

Similar to File 10 - CSX 334 _VRA NBO.ppsx

Socket Programming
Socket ProgrammingSocket Programming
Socket ProgrammingCEC Landran
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecturehugo lu
 
mobile_network_layer.pptx
mobile_network_layer.pptxmobile_network_layer.pptx
mobile_network_layer.pptxsinghram281982
 
2 logical addressing
2 logical addressing2 logical addressing
2 logical addressinggafurov_x
 
Custom_IP_Network_Protocol_and_Router
Custom_IP_Network_Protocol_and_RouterCustom_IP_Network_Protocol_and_Router
Custom_IP_Network_Protocol_and_RouterVishal Vasudev
 
Ex 1 chapter06-i-pv4-tony_chen
Ex 1 chapter06-i-pv4-tony_chenEx 1 chapter06-i-pv4-tony_chen
Ex 1 chapter06-i-pv4-tony_chenĐô GiẢn
 
Ppt fnr arbitrary length small domain block cipher proposal
Ppt fnr  arbitrary length small domain block cipher proposalPpt fnr  arbitrary length small domain block cipher proposal
Ppt fnr arbitrary length small domain block cipher proposalKarunakar Saroj
 
RHSA_1_Chapter(11)_Resume_chaptre_11.pptx
RHSA_1_Chapter(11)_Resume_chaptre_11.pptxRHSA_1_Chapter(11)_Resume_chaptre_11.pptx
RHSA_1_Chapter(11)_Resume_chaptre_11.pptxAbdellahELMAMOUN
 
11 coms 525 tcpip - internet protocol - forward
11   coms 525 tcpip - internet protocol - forward11   coms 525 tcpip - internet protocol - forward
11 coms 525 tcpip - internet protocol - forwardPalanivel Kuppusamy
 
10 coms 525 tcpip - internet protocol - ip
10   coms 525 tcpip -  internet protocol - ip10   coms 525 tcpip -  internet protocol - ip
10 coms 525 tcpip - internet protocol - ipPalanivel Kuppusamy
 
Networking presentation 9 march 2009
Networking presentation   9 march 2009Networking presentation   9 march 2009
Networking presentation 9 march 2009Kinshook Chaturvedi
 

Similar to File 10 - CSX 334 _VRA NBO.ppsx (20)

03 sockets
03 sockets03 sockets
03 sockets
 
Sockets
SocketsSockets
Sockets
 
Internet Protocol
Internet ProtocolInternet Protocol
Internet Protocol
 
Np unit2
Np unit2Np unit2
Np unit2
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
mobile_network_layer.pptx
mobile_network_layer.pptxmobile_network_layer.pptx
mobile_network_layer.pptx
 
2 logical addressing
2 logical addressing2 logical addressing
2 logical addressing
 
Custom_IP_Network_Protocol_and_Router
Custom_IP_Network_Protocol_and_RouterCustom_IP_Network_Protocol_and_Router
Custom_IP_Network_Protocol_and_Router
 
Ex 1 chapter06-i-pv4-tony_chen
Ex 1 chapter06-i-pv4-tony_chenEx 1 chapter06-i-pv4-tony_chen
Ex 1 chapter06-i-pv4-tony_chen
 
Ppt fnr arbitrary length small domain block cipher proposal
Ppt fnr  arbitrary length small domain block cipher proposalPpt fnr  arbitrary length small domain block cipher proposal
Ppt fnr arbitrary length small domain block cipher proposal
 
I pv4 and ipv6
I pv4 and ipv6I pv4 and ipv6
I pv4 and ipv6
 
C08-Network_Protocols (1).ppt
C08-Network_Protocols (1).pptC08-Network_Protocols (1).ppt
C08-Network_Protocols (1).ppt
 
Ipv4 and Ipv6
Ipv4 and Ipv6Ipv4 and Ipv6
Ipv4 and Ipv6
 
RHSA_1_Chapter(11)_Resume_chaptre_11.pptx
RHSA_1_Chapter(11)_Resume_chaptre_11.pptxRHSA_1_Chapter(11)_Resume_chaptre_11.pptx
RHSA_1_Chapter(11)_Resume_chaptre_11.pptx
 
11 coms 525 tcpip - internet protocol - forward
11   coms 525 tcpip - internet protocol - forward11   coms 525 tcpip - internet protocol - forward
11 coms 525 tcpip - internet protocol - forward
 
10 coms 525 tcpip - internet protocol - ip
10   coms 525 tcpip -  internet protocol - ip10   coms 525 tcpip -  internet protocol - ip
10 coms 525 tcpip - internet protocol - ip
 
Network Programming Assignment Help
Network Programming Assignment HelpNetwork Programming Assignment Help
Network Programming Assignment Help
 
Internetworking - IP
Internetworking - IPInternetworking - IP
Internetworking - IP
 
Networking presentation 9 march 2009
Networking presentation   9 march 2009Networking presentation   9 march 2009
Networking presentation 9 march 2009
 

Recently uploaded

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Recently uploaded (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

File 10 - CSX 334 _VRA NBO.ppsx

  • 1. Value-Result Arguments •Length of socket passed as an argument •Method by which length is passed depends on which direction the structure is being passed (from process to kernel, or vice versa)
  • 2. Value-Result Arguments •Value-only: bind, connect, sendto (from process to kernel) •Value-Result: accept, recvfrom, getsockname, getpeername (from kernel to process, pass a pointer to an integer containing size) Tells process how much information kernel actually stored struct sockaddr_in clientaddr ; socklen_t len; int listenfd, connectfd; len = sizeof (clientaddr); connectfd = accept (listenfd, (SA *) &clientaddr, &len);
  • 3. Byte Ordering  Port number and IP Address are represented by multi byte data type , which are placed in packets for the purpose of routing and multiplexing. Port Number are two byte or 16 bit and IPv4 address are 4 bytes or 32 bits. Now the problem arrises when transfering multi byte data types between different architecture.
  • 4. Byte Ordering  Let host A uses big endian architecture and sends a packet across the network to host B which uses a little endian architecture. If host B looks at the address to see if the packet is for him/her(choose a gender !) it will interprete the byte in the opposite order and will wrongly conclude that it is not his/ her packet. The internet uses big endian and we call it the Network Byte Order. It is not important to know which method it uses, since we have function to convert host-byte-order values in network-byte-order values and reverse also.
  • 5. Byte Ordering Functions htonl : convert host to network, long integer htons : convert host to network, short integer ntohl : convert network to host, long integer ntohs : convert network to host, short integer
  • 6. Byte Ordering Functions 1/4 •Two ways to store 2 bytes (16-bit integer) in memory Low-order byte at starting address  little-endian byte order High-order byte at starting address  big-endian byte order •in a big-endian computer  store 4F52 Stored as 4F52  4F is stored at storage address 1000, 52 will be at address 1001, for example •In a little-endian system  store 4F52 it would be stored as 524F (52 at address 1000, 4F at 1001) •Byte order used by a given system known as host byte order •Network programmers use network byte order •Internet protocol uses big-endian byte ordering for integers (port number and IP address)
  • 7. Byte Ordering Functions 2/4 High-order byte low-order byte MSB 16bit value LSB High-order byte low-order byte Increasing memory address Address A+1 Address A Little-endian byte order: big-endian byte order: Address A+1 Address A Increasing memory address
  • 8. Byte Ordering Functions 3/4 #include "unp.h" int main(int argc, char **argv) { union { short s; char c[sizeof(short)]; } un; un.s = 0x0102; printf("%s: ", CPU_VENDOR_OS); if (sizeof(short) == 2) { if (un.c[0] == 1 && un.c[1] == 2) printf("big-endiann"); else if (un.c[0] == 2 && un.c[1] == 1) printf("little-endiann"); else printf("unknownn"); } else printf("sizeof(short) = %dn", sizeof(short)); exit(0); } •Sample program to figure out little- endian or big-endian machine •Source code in byteorder.c
  • 9. Byte Ordering Functions 4/4 •To convert between byte orders Return value in network byte order htons (s for short word 2 bytes) htonl (l for long word 4 bytes) Return value in host byte order ntohs ntohl •Must call appropriate function to convert between host and network byte order •On systems that have the same ordering as the Internet protocols, four functions usually defined as null macros servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13);
  • 10. Byte Manipulation Functions #include <strings.h> void bzero (void *dest, size_t nbytes); // sets specified number of bytes to 0 in the destination void bcopy (const void *src,void * dest, size_t nbytes); // moves specified number of bytes from source to destination void bcmp (const void *ptr1, const void *ptr2,size_t nbytes) //compares two arbitrary byte strings, return value is zero if two byte strings are identical, otherwise, nonzero
  • 11. Address Conversion Functions 1/2 Convert an IPv4 address from a dotted-decimal string “206.168.112.96” to a 32-bit network byte order binary value #include <arpa/inet.h> int inet_aton (const char* strptr, struct in_addr *addrptr); // return 1 if string was valid, 0 on error. Address stored in *addrptr in_addr_t inet_addr (const char * strptr); // returns 32 bit binary network byte order IPv4 address, currently deprecated char * inet_nota (struct in_addr inaddr); //returns pointer to dotted-decimal string
  • 12. Address Conversion Functions 2/2 To handle both IPv4 and IPv6 addresses #include <arpa/inet.h> int inet_pton (int family, const char* strptr, void *addrptr); // return 1 if OK, 0 on error. 0 if not a valid presentation, -1 on error, Address stored in *addrptr Const char * inet_ntop (int family, const void* addrptr, char *strptr, size_t len); // return pointer to result if OK, NULL on error if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_quit("inet_pton error for %s", argv[1]); ptr = inet_ntop (AF_INET,&addr.sin_addr,str,sizeof(str));
  • 13. Reading and Writing Functions 1/2 int send (int socket, char *message, int msg_len, int flags) (TCP) int sendto (int socket, void *msg, int len, int flags, struct sockaddr * to, int tolen ); (UDP) int write(int socket, void *msg, int len); /* TCP */ int recv (int socket, char *buffer, int buf_len, int flags) (TCP) int recvfrom(int socket, void *msg, int len, int flags, struct sockaddr *from, int *fromlen); (UDP) int read(int socket, void *msg, int len); (TCP)
  • 14. Reading and Writing Functions 2/2 •Stream sockets (TCP sockets) exhibit a behavior with read and write that differs from normal file I/O •A read or write on a stream socket might input or output fewer bytes than requested (not an error) readn function writen function readline function