creating listen socket on port 21... creating listen socket on port 990... s
时间: 2023-10-22 15:01:20 浏览: 106
创建监听套接字在端口21上... 在端口990上创建监听套接字... s。
这段话描述了在计算机网络中创建监听套接字的过程。套接字是计算机网络通信中的一种机制,它提供了一种在不同主机之间进行数据传输的方法。在这种情况下,使用端口21和端口990作为监听套接字的传输通道。
创建监听套接字时,计算机首先需要选择一个可用的端口号。端口号是用于标识应用程序的特定套接字的数字标识,它可以是0到65535之间的任何数字。然后,计算机会在指定的端口上创建一个监听套接字,以等待其他主机的连接请求。
创建监听套接字是一种服务器端的操作,它使得其他主机可以通过指定的端口与该服务器进行通信。在这个例子中,创建了两个监听套接字,分别在端口21和990上。这意味着服务器可以接受在这两个端口上的连接请求,并与发送这些请求的主机进行数据交换。
总而言之,以上300字的描述简要说明了在端口21和990上创建监听套接字的过程,使服务器能够接受来自其他主机的连接请求,并进行数据传输。
相关问题
将代码inet_ntoa(client_addr.sin_addr)改为inet_ntop()函数
inet_ntop() 函数能够将网络字节序的二进制IP地址转换为点分十进制格式的字符串IP地址。如果要将代码中的inet_ntoa()函数改为inet_ntop()函数,需要修改以下几点:
1. 在头文件中包含<arpa/inet.h>以使用inet_ntop()函数。
2. 将inet_ntoa(client_addr.sin_addr)替换为inet_ntop(AF_INET, &(client_addr.sin_addr), client_ip, INET_ADDRSTRLEN),其中client_ip是存放字符串IP地址的缓冲区,INET_ADDRSTRLEN是缓冲区大小。
3. 在调用inet_ntop()函数之前,需要将client_ip缓冲区清空,以防止出现未知的字符。
下面是修改后的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
int main(int argc, char const *argv[]) {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
char client_ip[INET_ADDRSTRLEN];
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Convert binary IP address to string IP address
memset(client_ip, '\0', INET_ADDRSTRLEN);
inet_ntop(AF_INET, &(address.sin_addr), client_ip, INET_ADDRSTRLEN);
printf("Connection accepted from %s:%d\n", client_ip, ntohs(address.sin_port));
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
```
C++ server socket
In C programming language, a server socket is a type of socket that is used to listen for incoming connections from clients. The server socket is created using the socket() system call with the AF_INET (IPv4) or AF_INET6 (IPv6) address family and the SOCK_STREAM protocol type.
Once the server socket is created, it must be bound to a specific port number on the local machine using the bind() system call. This tells the operating system to route incoming client connections to this specific port on the server.
The server socket then enters a loop where it listens for incoming client connections using the listen() system call. When a client connects to the server, the server socket accepts the connection using the accept() system call, which creates a new socket dedicated to communication with that specific client.
The server socket can then handle multiple client connections simultaneously by creating a new thread or process for each accepted connection. The server can send and receive data over each client socket using the read() and write() system calls.
Overall, the server socket provides the foundation for building networked applications that require communication between multiple clients and a central server.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)