0% found this document useful (0 votes)
16 views30 pages

3.socket APIs Slide

Chapter 3 covers the basics of socket programming, including socket pairs, TCP and UDP port numbers, and buffer sizes. It details the Sockets API, including socket address structures, value-result arguments, and address conversion functions. Additionally, it explains the read() and write() functions for handling data transmission over sockets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views30 pages

3.socket APIs Slide

Chapter 3 covers the basics of socket programming, including socket pairs, TCP and UDP port numbers, and buffer sizes. It details the Sockets API, including socket address structures, value-result arguments, and address conversion functions. Additionally, it explains the read() and write() functions for handling data transmission over sockets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Chương 3: Socket API

Nội dung
• Basics of socket
• Sockets API
BASICS OF SOCKET
Socket Pair
• four-tuple that defines the two endpoints of
the connection
– the local IP address
– local port
– foreign IP address
– foreign port
• The 2 values that identify each endpoint, an IP
address and a port number, are often called a
socket.
TCP Port Numbers and Concurrent
Servers (1)
TCP Port Numbers and Concurrent
Servers (2)
TCP Port Numbers and Concurrent
Servers (3)
TCP Port Numbers and Concurrent
Servers (4)
Buffer Sizes and Limitations
• Maximum size of an IPv4 datagram: 65,538 bytes
• MTU (Maximum transmission unit)
• Fragmentation when the size of the datagrram
exceeds the link MTU.
– DF bit (don’t fragment)
• MSS (maximum segment size): that announces to
the peer TCP the maximum amount of TCP data
that the peer can send per segment.
• MSS = MTU – fixed size of headers of IP and TCP
TCP output
UDP output
Protocol Usage by Common
Internet Applications
SOCKETS API
Socket Address Structures
• IPv4 Socket Address Structure: sockaddr_in
(including <netinet/in.h>)

Chương trình ví dụ: init_sockaddr_in.c


Datatypes required by the POSIX
specification
Address families in sys/socket.h
Value-Result Arguments
• when a socket address structure is passed to
any socket function, it is always passed by
reference.
• The length of the structure is also passed as
an argument.
• 2 directions: process à kernel or kernel à
process
Value-Result Arguments
• 3 functions: bind, connect & sendto: process à kernel

Chương trình ví dụ: ex_connect.c


Value-Result Arguments
• 4 functions: accept, recvfrom, getsockname,
and getpeername: kernel à process

Chương trình ví dụ: ex_getpeername.c


Address Conversion Functions
• They convert Internet addresses between ASCII strings (what
humans prefer to use) and network byte ordered binary values
(values that are stored in socket address structures).

#include <arpa/inet.h>
int inet_aton(const char *strptr, struct
in_addr *addrptr);
Returns: 1 if string was valid, 0 on error

in_addr_t inet_addr(const char *strptr);


Returns: 32-bit binary network byte ordered IPv4 address;
INADDR_NONE if error

char *inet_ntoa(struct in_addr inaddr);


Returns: pointer to dotted-decimal string

Chương trình ví dụ: convert_IP.c


Address Conversion Functions (2)
#include <arpa/inet.h>
int inet_pton(int family, const char
*strptr, void *addrptr);
Returns: 1 if OK, 0 if input not a valid presentation format, -1 on
error

const char *inet_ntop(int family, const void


*addrptr, char *strptr, size_t len);
Returns: pointer to result if OK, NULL on error

Chương trình ví dụ: convert_IP_v4-6.c


Summary of address conversion
functions
sock_ntop function
• Problem of inet_ntop: it requires the caller
to pass a pointer to a binary address.
struct sockaddr_in addr;
inet_ntop(AF_INET,&addr.sin_addr,
str, sizeof(str));
• function sock_ntop
#include "unp.h"
char *sock_ntop(const struct sockaddr
*sockaddr, socklen_t addrlen);
Chương trình ví dụ: example_of_sock_ntop.c
Compare inet_ntop and sock_ntop
read()
• read() Function:
• The read() function is used to read data from a file descriptor, which
can include socket file descriptors. For socket streams, read() reads
data sent by a peer over the network.
• ssize_t read(int sockfd, void *buf, size_t count);
• Parameters:
• sockfd: The file descriptor of the socket (created
using socket()).
• buf: A pointer to a buffer where the data read from
the socket will be stored.
• count: The maximum number of bytes to read (size of
the buffer).
read()
• read() function returns:
– On success, the number of bytes actually read is
returned (this can be less than count if fewer
bytes are available).
– On error, -1 is returned, and errno is set
appropriately.
– If the peer has closed the connection, 0 is
returned (end-of-file).
read()
• Example:
char buffer[1024];
int n = read(sockfd, buffer, sizeof(buffer));
if (n > 0) {
printf("Received message: %s\n", buffer);
} else if (n == 0) {
printf("Connection closed by peer\n");
} else {
perror("read error");
}
write()
• write() Function:
• The write() function is used to send data to a
file descriptor, which can include socket file
descriptors. For socket streams, write() sends
data to the connected peer.

• ssize_t write(int sockfd, const void *buf,


size_t count);
write()
• Parameters:
• sockfd: The file descriptor of the socket (created
using socket()).
• buf: A pointer to the buffer containing the data to be
sent.
• count: The number of bytes to write (send) from the
buffer.
• Return Value:
• On success, the number of bytes actually written is
returned.
• On error, -1 is returned, and errno is set appropriately.
write()
• Example:
char *message = "Hello, server!";
int n = write(sockfd, message,
strlen(message));
if (n > 0) {
printf("Message sent: %s\n",
message);
} else {
perror("write error");
}
Chương trình ví dụ: read-write-server.c
read-write-client.c

You might also like