To make a socket listen for incoming connections, you need to perform a few steps:
Create a socket using the
socket()
function from the<sys/socket.h>
header. You specify the address family asAF_INET
, the socket type asSOCK_STREAM
for TCP, and set the protocol as0
.TEXT/X-C++SRC1int sockfd; 2sockfd = socket(AF_INET, SOCK_STREAM, 0);
Set up the server address using the
sockaddr_in
structure. This structure contains fields such assin_family
for the address family,sin_addr.s_addr
for the IP address, andsin_port
for the port number. In this example, we set thesin_addr.s_addr
tohtonl(INADDR_ANY)
to bind the socket to any available network interface, and we set thesin_port
to the desired port number.TEXT/X-C++SRC1struct sockaddr_in serverAddr; 2serverAddr.sin_family = AF_INET; 3serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); 4serverAddr.sin_port = htons(8080);
Bind the socket to the server address using the
bind()
function. This associates the socket with the specified address and port.TEXT/X-C++SRC1int bindResult = bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
Finally, you can make the socket listen for incoming connections using the
listen()
function. You pass the socket descriptor and specify the maximum number of connections that can be queued using the second parameter. In this example, we set the maximum queue size to10
.TEXT/X-C++SRC1int listenResult = listen(sockfd, 10);
If any errors occur during these steps, you can check the return values of the relevant functions to handle them accordingly.
Once the socket is listening for connections, you can then accept incoming connections using the accept()
function. This allows you to establish a connection with a client and start sending and receiving data.
Remember to close the socket when you're done with it by calling the close()
function!
Here's an example code that demonstrates how to make a socket listen for connections:
1#include <iostream>
2#include <sys/socket.h>
3#include <netinet/in.h>
4
5int main() {
6 int sockfd;
7 struct sockaddr_in serverAddr;
8
9 // Create a socket
10 sockfd = socket(AF_INET, SOCK_STREAM, 0);
11
12 if (sockfd == -1) {
13 std::cout << "Failed to create socket" << std::endl;
14 return 1;
15 }
16
17 // Set up the server address
18 serverAddr.sin_family = AF_INET;
19 serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
20 serverAddr.sin_port = htons(8080);
21
22 // Bind the socket
23 int bindResult = bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
24
25 if (bindResult == -1) {
26 std::cout << "Failed to bind socket" << std::endl;
27 return 1;
28 }
29
30 // Listen for connections
31 int listenResult = listen(sockfd, 10);
32
33 if (listenResult == -1) {
34 std::cout << "Failed to listen for connections" << std::endl;
35 return 1;
36 }
37
38 std::cout << "Socket is now listening for connections" << std::endl;
39
40 return 0;
41}
xxxxxxxxxx
}
int main() {
int sockfd;
struct sockaddr_in serverAddr;
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
std::cout << "Failed to create socket" << std::endl;
return 1;
}
// Set up the server address
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(8080);
// Bind the socket
int bindResult = bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
if (bindResult == -1) {
std::cout << "Failed to bind socket" << std::endl;
return 1;
}