Podcast
Questions and Answers
Which layer of the OSI model do sockets primarily operate within to facilitate data transmission?
Which layer of the OSI model do sockets primarily operate within to facilitate data transmission?
- Network Layer
- Application Layer
- Data Link Layer
- Transport Layer (correct)
What benefit does layering provide to application programmers when using sockets?
What benefit does layering provide to application programmers when using sockets?
- Requires programmers to implement their own routing protocols.
- Automatically handles WiFi encryption.
- Abstracts away the complexities of routing, Ethernet frames, and WiFi encryption. (correct)
- Exposes low-level details of the network.
Which of the following best describes what lower layers need to know for data transmission?
Which of the following best describes what lower layers need to know for data transmission?
- Destination IP address and port number. (correct)
- The implementation details of the application.
- The application's user interface preferences.
- The programming language used by the application.
What is a socket primarily used for?
What is a socket primarily used for?
In the context of socket programming, what does the term 'de facto standard' imply about sockets?
In the context of socket programming, what does the term 'de facto standard' imply about sockets?
Which of the following is NOT a typical application of sockets?
Which of the following is NOT a typical application of sockets?
What is a key characteristic of stream sockets?
What is a key characteristic of stream sockets?
Which protocol is typically used with datagram sockets?
Which protocol is typically used with datagram sockets?
What is a primary reason for using sockets over higher-level libraries?
What is a primary reason for using sockets over higher-level libraries?
Which operating systems provide compatibility with sockets?
Which operating systems provide compatibility with sockets?
Why might a developer choose NOT to use sockets and instead opt for higher-level libraries?
Why might a developer choose NOT to use sockets and instead opt for higher-level libraries?
In socket communication, what information is contained within the 'connection socket pair'?
In socket communication, what information is contained within the 'connection socket pair'?
What is the fundamental difference between a 'passive' and an 'active' socket?
What is the fundamental difference between a 'passive' and an 'active' socket?
Which sequence of actions accurately reflects the steps a server typically performs when using sockets?
Which sequence of actions accurately reflects the steps a server typically performs when using sockets?
In the context of setting up a socket, what is the purpose of the bind()
function?
In the context of setting up a socket, what is the purpose of the bind()
function?
What does the listen()
function do in socket programming?
What does the listen()
function do in socket programming?
What is the significance of the accept()
function in server-side socket programming?
What is the significance of the accept()
function in server-side socket programming?
In the sockaddr_in
structure, what is the purpose of the sin_family
field?
In the sockaddr_in
structure, what is the purpose of the sin_family
field?
What is the role of the htons()
function in socket programming?
What is the role of the htons()
function in socket programming?
In the context of the socket()
function, what is the significance of the protocol
argument being set to 0
?
In the context of the socket()
function, what is the significance of the protocol
argument being set to 0
?
What is the primary purpose of the INADDR_ANY
constant when binding a socket?
What is the primary purpose of the INADDR_ANY
constant when binding a socket?
If the listen()
function is called with a backlog
parameter of 5
, what does this signify?
If the listen()
function is called with a backlog
parameter of 5
, what does this signify?
What does the return value of the accept()
function represent?
What does the return value of the accept()
function represent?
In a client-server model using sockets, which action does the client perform to initiate a connection after setting up its socket?
In a client-server model using sockets, which action does the client perform to initiate a connection after setting up its socket?
When a client calls the gethostbyname()
function, what type of information is it trying to retrieve?
When a client calls the gethostbyname()
function, what type of information is it trying to retrieve?
What is the purpose of the send()
and recv()
functions in socket programming?
What is the purpose of the send()
and recv()
functions in socket programming?
What is one key difference between using send()/recv()
versus read()/write()
for data transfer over sockets?
What is one key difference between using send()/recv()
versus read()/write()
for data transfer over sockets?
In a typical client-server interaction using sockets, which side usually calls connect()
?
In a typical client-server interaction using sockets, which side usually calls connect()
?
Which of the following is a valid reason to use stream sockets over datagram sockets?
Which of the following is a valid reason to use stream sockets over datagram sockets?
Which function is responsible for accepting a connection request in a server application?
Which function is responsible for accepting a connection request in a server application?
If a server needs to handle multiple client connections concurrently, what approach is typically used in conjunction with sockets?
If a server needs to handle multiple client connections concurrently, what approach is typically used in conjunction with sockets?
When using TCP sockets, what is the correct sequence of operations to gracefully close a connection?
When using TCP sockets, what is the correct sequence of operations to gracefully close a connection?
What could be a potential drawback of using connectionless datagram sockets?
What could be a potential drawback of using connectionless datagram sockets?
Consider the following code snippet where an error is occurring: sockfd = socket(AF_INET, SOCK_STREAM, 0);
and sockfd
returns -1
; what does this generally indicate?
Consider the following code snippet where an error is occurring: sockfd = socket(AF_INET, SOCK_STREAM, 0);
and sockfd
returns -1
; what does this generally indicate?
Why is it often necessary to convert the IP address from presentation (human-readable) format to network format using inet_pton
(or similar) before storing it in sockaddr_in
?
Why is it often necessary to convert the IP address from presentation (human-readable) format to network format using inet_pton
(or similar) before storing it in sockaddr_in
?
Imagine a scenario where a server is designed to handle a high volume of simultaneous connections and uses a non-blocking socket approach with select()
or epoll()
. If the server suddenly stops responding to new connection attempts while still actively serving existing connections, what is the most likely cause?
Imagine a scenario where a server is designed to handle a high volume of simultaneous connections and uses a non-blocking socket approach with select()
or epoll()
. If the server suddenly stops responding to new connection attempts while still actively serving existing connections, what is the most likely cause?
A networked service suffers intermittent connection drops. Wireshark captures reveal TCP RST
packets being sent from the server immediately after receiving data from clients. Increasing the SO_RCVBUF
socket option on the server has no effect. What is the most likely cause?
A networked service suffers intermittent connection drops. Wireshark captures reveal TCP RST
packets being sent from the server immediately after receiving data from clients. Increasing the SO_RCVBUF
socket option on the server has no effect. What is the most likely cause?
Flashcards
What is a Socket?
What is a Socket?
An endpoint of a two-way communication link between two networked programs.
Why Sockets?
Why Sockets?
The standard API for connecting processes, locally or over a network.
What's in a socket?
What's in a socket?
It is the connection between client and server contains client address, server address and port number.
Stream Sockets
Stream Sockets
Signup and view all the flashcards
Datagram Sockets
Datagram Sockets
Signup and view all the flashcards
socket() function
socket() function
Signup and view all the flashcards
bind() function
bind() function
Signup and view all the flashcards
listen() function
listen() function
Signup and view all the flashcards
accept() function
accept() function
Signup and view all the flashcards
Socket Domain
Socket Domain
Signup and view all the flashcards
Socket Type
Socket Type
Signup and view all the flashcards
connect() function
connect() function
Signup and view all the flashcards
send() / recv()
send() / recv()
Signup and view all the flashcards
read() / write()
read() / write()
Signup and view all the flashcards
Study Notes
- Socket Programming is presented by Tran Giang Son, [email protected] of the ICT Department, USTH
Contents
- What is a socket?
- Why use sockets?
- What is in a socket?
- How to use sockets?
Why Layering?
- It's similar to application layering.
- Application programmer responsibilities:
- Not concerned with routing.
- Not concerned with Ethernet frames.
- Not concerned with WiFi WPA2 encryption.
- Not concerned with reliability implementations.
- Application programmer tasks:
- Passes data down.
- Focuses on the application logic.
- Lower layers need to know:
- Destination (where to), specified by hostname (resolved with
gethostbyname()
) or IP address. - Service, indicated by port number.
- Destination (where to), specified by hostname (resolved with
Socket: What?
- A socket is an endpoint of a two-way communication link between two networked programs.
- Sockets are represented by a file descriptor after creation, based on the Unix philosophy that "everything is a file."
- Sockets are the
de facto
standard for TCP/UDP but they replaced the following:- NetBIOS / NetBEUI
- IPX / SPX
Socket: Applications
- Most network applications use sockets.
- Sockets are used to:
- Send messages.
- Share data like images, music, videos.
- Facilitate Interprocess Communication (IPC)
Socket: Which Types?
- Stream sockets: connection-oriented with TCP
- Make a connection.
- Transfer data.
- Close connection.
- Ensure sequence, error checking.
- Example: YouTube video.
- Datagram sockets: connectionless with UDP
- Transfer data without explicitly making a connection.
- Example: DNS.
Socket: Why?
- Sockets are the standard API for connecting processes locally or over a network.
- Compatibility: widely supported on Linux / UNIX, Windows, and macOS.
- They are low-level, minimizing data transfer and providing speed with very little overhead.
- Sockets are Customizable, flexible, self-defined protocol
Why NOT Socket Over Higher-Level Libraries?
- Sockets are low-level and require more effort.
- They require the definition of protocol, message boundaries, data representation, and security.
- Session control including authentication must be implemented.
What's In A Socket?
- Client socket address: 128.2.194.242:3479
- Client host address: 128.2.194.242
- Server socket address: 208.216.181.15:80
- Server host address: 208.216.181.15
- FTP Server (port 21)
- HTTP Server (port 80)
- Connection socket pair (128.2.194.242:3479, 208.216.181.15:80)
Overview
- Server:
- Passively waits.
- Uses a passive socket.
- Client:
- Initiates the connection.
- Uses an active socket.
Steps
- Setup to find remote host and the service
- Transfer Data
- Send/Receive using write() / read()
- Close
Server Overview
- The server process involves:
- socket() call
- bind() associate a socket with a port number
- Buy cable
- Connect cable
- Power on machine
- listen() to incoming requests
- accept() connections
- This equates to a landline phone.
Socket: Important Struct
struct sockaddr_in {
short sin_family; // e.g., AF_INET
unsigned short sin_port; // e.g., htons(3490)
struct in_addr sin_addr; // see struct in_addr, below
char sin_zero[8]; // zero this if you want to
};
struct in_addr {
unsigned long s_addr; // load with inet_aton()
};
Server: Setup Socket
int socket(int domain, int type, int protocol);
domain
: AF_INET (IPv4) or AF_INET6 (IPv6)type
: SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)protocol
: 0
Example:
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
Server: Binding bind()
int bind(int sockfd,
const struct sockaddr *bind_addr,
socklen_t addrlen);
sockfd
: file descriptor returned bysocket()
bind_addr
: astruct sockaddr_in
for IPv4addrlen
: size of the struct pointed bybind_addr
Server: Example for socket() and bind()
struct sockaddr_in saddr;
int sockfd;
unsigned short port = 80;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Error creating socket\n");
...
}
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(port);
if (bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
printf("Error binding\n");
...
}
Server: Listen To Incoming Connections listen()
int listen(int sockfd, int backlog);
sockfd
: file descriptor thatsocket()
returnedbacklog
: number of pending connections to queue- Example:
listen(sockfd, 10);
Server: Accept an Incoming Connection accept()
int accept(int sockfd,
struct sockaddr *addr,
socklen_t *addrlen);
- The server must explicitly accept incoming connections.
sockfd
: file descriptor thatsocket()
returnedaddr
: pointer to store client addressaddrlen
: size of addr- Returns a file descriptor for the connected socket
- Example:
int client = accept(sockfd, (struct sockaddr_in *) &caddr, &clen);
Server: Example for listen() and accept()
if (listen(sockfd, 5) < 0) {
printf("Error listening\n");
...
}
clen = sizeof(caddr);
if ((clientfd = accept(sockfd,
(struct sockaddr *) &caddr, &clen)) < 0) {
printf("Error accepting connection\n");
...
}
Server: Complete Example
int sockfd, clen, clientfd;
struct sockaddr_in saddr, caddr;
unsigned short port = 80;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Error creating socket\n");
...
}
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(port);
if ((bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
printf("Error binding\n");
...
}
if (listen(sockfd, 5) < 0) {
printf("Error listening\n");
...
}
clen = sizeof(caddr);
if ((clientfd = accept(sockfd, (struct sockaddr *) &caddr, &clen)) < 0) {
printf("Error accepting connection\n");
...
}
Practical Work 3: Server Setup
- Write a new program in C named "03.practical.work.server.setup.c".
- The server should:
- Listen to TCP port 8784 (USTH in a T9 dial pad!).
- Bind to all possible interfaces.
- Print a message when a client connects to it.
- Deploy to VPS (Virtual Private Server).
- Test the connection using
telnet
ornc
- Push C program to corresponding forked GitHub repository.
Client: Setup Socket
- Similar to server's
int socket(int domain, int type, int protocol);
domain
: AF_INET (IPv4) or AF_INET6 (IPv6)type
: SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)protocol
: 0- Example:
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
Client: Connect to Server connect()
int connect(int sockfd,
const struct sockaddr *saddr,
socklen_t addrlen);
sockfd
: file descriptor that socket() returned.addr
: pointer to store server address.addrlen
: size of addr.- Example:
connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr))
Client: Complete Example
struct sockaddr_in saddr;
struct hostent *h;
int sockfd;
unsigned short port = 80;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Error creating socket\n");
...
}
if ((h = gethostbyname("ict.usth.edu.vn")) == NULL) {
printf("Unknown host\n");
...
}
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
memcpy((char *) &saddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
saddr.sin_port = htons(port);
if (connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
printf("Cannot connect\n");
...
}
Practical Work 4: Client Setup
- Write a new program in C named "04.practical.work.client.setup.c".
- The client should:
- Get the server hostname from program arguments.
- Ask for hostname from STDIN if no argument is provided.
- Resolve IP address and print to STDOUT.
- Connect to that server, TCP port 8784
- Print a message if it connects to the server successfully.
- Test the connection from the client to the server on VPS.
- Push C program to corresponding forked GitHub repository.
Data Transfer: Overview
- Two common ways for data transfer:
send()
/recv()
- Original socket functions.
- Specific to sockets with "flags".
read()
/write()
- Consider socket as a file.
- Generic functions.
- Use either pair, don't mix.
Data Transfer: recv() and send()
ssize_t recv(int socket, void *buffer, size_t length, int flags);
ssize_t send(int socket, const void *buffer, size_t length, int flags);
socket
: use socket file descriptor returned bysocket()
oraccept()
buffer
: buffer to read from / write tolength
: size of the allocated buffer (recv()
) and length of the content (send()
)flags
: specific "settings" for the request.- Example:
recv(sockfd, buffer, sizeof(buffer), 0);
send(sockfd, "hello world!\n", 13, 0);
Data Transfer: read() and write()
ssize_t read(int fd, void *buf, size_t len);
ssize_t write(int fd, const void *buf, size_t len);
fd
: file descriptor, use socket file descriptor returned bysocket()
oraccept()
buf
: buffer to read from / write tolen
: size of the allocated buffer (read()
) and length of the content (write()
)- Example:
read(sockfd, buffer, sizeof(buffer));
write(sockfd, "hello world!\n", 13);
Data Transfer: Taking Turn
- Client:
while (condition) {
scanf() from STDIN;
send() to server;
recv() from server;
printf() to STDOUT;
}
- Server:
while (condition) {
recv() from client;
printf() to STDOUT;
scanf() from STDIN;
send() to client;
}
Practical Work 5: Data Transfer, Taking Turn
- Copy your client and server code from the 4th practical work to:
- "05.practical.work.server.turn.c"
- "05.practical.work.client.turn.c"
- Improve the client and server to build a chat system:
- Input from STDIN.
- Send to the other side.
- Output received data to STDOUT.
- The client and server take turns.
- Test the system between your laptop and VPS.
- Push your C programs to the corresponding forked GitHub repository.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.