Windows 上的 C 套接字编程(服务器,使用 select() 和 fd_set 的多线程)
Socket programming in C on Windows (server, multithreading with select() and fd_set)
我尝试创建一个简单的应用程序,其中服务器同时为局域网中的多个客户端提供服务,客户端向服务器发送数据。问题是服务器不知道如何同时与多个客户端通信,它仍然只与最后连接的客户端通信。
我在应用层部署了Salt channel密码协议,保证了传输数据的完整性和安全性。我用 C 语言编程,我使用 Mingw 编译器,我使用 select()
函数和 fd_set
结构同时处理多个套接字。我写了客户端和服务端的代码,这里握手成功,可以同时和所有客户端交换数据。随后,我尝试在代码中部署盐通道应用协议。但是,当我部署协议时,服务器只为最后登录的客户端提供服务。
我创建了客户端的结构,我想它是否包含所有需要的东西。我无法弄清楚为什么服务器一次不能为多个客户端提供服务的错误。我试图测试是否将多个客户端连接到服务器,但客户端不发送任何消息,因此服务器保留了它们的套接字,当我关闭服务器时,所有客户端-服务器连接都关闭了,但是一旦客户端发送消息,当前只有一个客户端-服务器连接。我有一个 while
循环通信,我在 fd_set
结构中添加一个服务器套接字并调用 select()
函数,当套接字可用于握手时,我调用 accept()
并且 return 值是一个特定的客户端套接字,然后我将其添加到 fd_set
结构中,循环继续并查找可用的套接字,当它们检查它是否适合握手时,如果不是,调用一个函数从客户端接收消息并解密它。
//Libraries for working with network tools in Windows
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
//pragma comment nie je potrebny, lebo vyuzivam v Makefile subore flag -lws2_32
//#pragma comment(lib, "ws2_32.lib")
//Constants for working with sockets in Windows
#define ISVALIDSOCKET(s) ((s) != INVALID_SOCKET)
#define CLOSESOCKET(s) closesocket(s)
#define GETSOCKETERRNO() (WSAGetLastError())
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
//Libraries of Salt channelv2
#include "salt.h"
#include "salt_io.h"
#include "salti_util.h"
#include <time.h>
//Function for reads encrypted message
salt_ret_t salt_read_begin_pom(salt_channel_t *p_channel,
uint8_t *p_buffer,
uint32_t buffer_size,
salt_msg_t *p_msg,
uint8_t *p_pom,
uint32_t *p_size);
//Ready sk_sec key for server
static uint8_t host_sk_sec[64] = {
0x7a, 0x77, 0x2f, 0xa9, 0x01, 0x4b, 0x42, 0x33,
0x00, 0x07, 0x6a, 0x2f, 0xf6, 0x46, 0x46, 0x39,
0x52, 0xf1, 0x41, 0xe2, 0xaa, 0x8d, 0x98, 0x26,
0x3c, 0x69, 0x0c, 0x0d, 0x72, 0xee, 0xd5, 0x2d,
0x07, 0xe2, 0x8d, 0x4e, 0xe3, 0x2b, 0xfd, 0xc4,
0xb0, 0x7d, 0x41, 0xc9, 0x21, 0x93, 0xc0, 0xc2,
0x5e, 0xe6, 0xb3, 0x09, 0x4c, 0x62, 0x96, 0xf3,
0x73, 0x41, 0x3b, 0x37, 0x3d, 0x36, 0x16, 0x8b
};
typedef struct{
SOCKET sock_fd;
salt_channel_t channel;
struct sockaddr_storage client_address;
socklen_t client_len;
} CLIENT;
void connection_and_servicing(CLIENT *p_client, SOCKET p_socket);
int main() {
#if defined(_WIN32)
//Variables
SOCKET socket_listen;;
CLIENT *client_info;
uint8_t rx_buffer[UINT16_MAX * 4];
uint8_t hndsk_buffer[SALT_HNDSHK_BUFFER_SIZE];
uint8_t pom_buffer[SALT_HNDSHK_BUFFER_SIZE];
salt_msg_t msg_in;
salt_protocols_t protocols;
salt_msg_t msg_out;
salt_ret_t ret_msg;
uint32_t verify = 0, decrypt_size;
//The MAKEWORD macro allows us to request Winsock version 2.2
WSADATA d;
if (WSAStartup(MAKEWORD(2, 2), &d)) { //inicializacia Winscok-u
fprintf(stderr, "Failed to initialize.\n");
return 1;
}
printf("Configuring local address...\n");
//Struct addrinfo hints
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
//Looking address IPv4
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; //TCP connection
//We ask getaddrinfo () to set the address, for the availability of any network device
hints.ai_flags = AI_PASSIVE;
//Setting a pointer to a structure that contains return information from the getaddrinfo () function
struct addrinfo *bind_address;
getaddrinfo("192.168.100.8", "8080", &hints, &bind_address); //port 8080, generate an address suitable for the bind () function
//Creating socket
printf("Creating socket...\n");
socket_listen = socket(bind_address->ai_family,
bind_address->ai_socktype, bind_address->ai_protocol);
if (!ISVALIDSOCKET(socket_listen)) {
fprintf(stderr, "socket() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
//Binding socket to local address
printf("Binding socket to local address...\n");
if (bind(socket_listen,
bind_address->ai_addr, bind_address->ai_addrlen)) {
fprintf(stderr, "bind() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
//After we've called bind(), we use the freeaddrinfo() function to free the memory for bind_address
puts("Bind done");
freeaddrinfo(bind_address);
printf("Listening...\n");
if (listen(socket_listen, 5) < 0) {
fprintf(stderr, "listen() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
//Define fd_set structure master that stores all of the active sockets
fd_set master;
FD_ZERO(&master);
FD_SET(socket_listen, &master);
SOCKET max_socket = socket_listen;
printf("Waiting for connections...\n");
while(1) {
fd_set reads;
reads = master;
//The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O
if (select(max_socket+1, &reads, 0, 0, 0) < 0) {
fprintf(stderr, "select() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
SOCKET i;
//Loop through each possible socket
for(i = 1; i <= max_socket; ++i) {
if (FD_ISSET(i, &reads)) {
//If socket_listen, create TCP connection of accept() function
if (i == socket_listen) {
client_info = (CLIENT *) malloc(sizeof(CLIENT));
client_info->client_len = sizeof(client_info->client_address);
client_info->sock_fd = accept(socket_listen,
(struct sockaddr*) &client_info->client_address,
&client_info->client_len);
if (!ISVALIDSOCKET(client_info->sock_fd)) {
fprintf(stderr, "accept() failed. (%d)\n",
GETSOCKETERRNO());
return 1;
}
FD_SET(client_info->sock_fd, &master);
if (client_info->sock_fd > max_socket)
max_socket = client_info->sock_fd;
//Prints the client address using the getnameinfo() function
char address_buffer[100];
getnameinfo((struct sockaddr*)&client_info->client_address,
&client_info->client_len,
address_buffer, sizeof(address_buffer), 0, 0,
NI_NUMERICHOST);
printf("New connection %s\n", address_buffer);
printf("\nWaiting for succeses Salt handshake...\n");
connection_and_servicing(client_info, socket_listen);
printf("handshake\n");
} else {
ret_msg = SALT_ERROR;
memset(rx_buffer, 0, sizeof(hndsk_buffer));
ret_msg = salt_read_begin_pom(&client_info->channel, rx_buffer, sizeof(rx_buffer), &msg_in, pom_buffer, &decrypt_size);
continue;
}
} //if FD_ISSET
} //for i to max_socket
} //while(1)
printf("Closing listening socket...\n");
free(client_info);
CLOSESOCKET(socket_listen);
WSACleanup();
#endif
printf("Finished.\n");
return 0;
}
void connection_and_servicing(CLIENT *p_client, SOCKET p_socket)
{
//CLIENT *p_client = (context *);
//SOCKET sock = p_client->sock_fd;
uint8_t hndsk_buffer[SALT_HNDSHK_BUFFER_SIZE];
uint8_t rx_buffer[UINT16_MAX * 4];
uint8_t pom_buffer[SALT_HNDSHK_BUFFER_SIZE];
uint8_t tx_buffer[UINT16_MAX * 4];
uint8_t protocol_buffer[128];
uint32_t verify = 0, decrypt_size;
salt_msg_t msg_out;
salt_ret_t ret;
salt_ret_t ret_msg;
salt_msg_t msg_in;
salt_protocols_t protocols;
clock_t start_t, end_t;
ret = salt_create(&p_client->channel, SALT_SERVER, my_write, my_read, &my_time);
assert(ret == SALT_SUCCESS);
//Initiates to add information about supported protocols to host
ret = salt_protocols_init(&p_client->channel, &protocols, protocol_buffer, sizeof(protocol_buffer));
assert(ret == SALT_SUCCESS);
//Add a protocol to supported protocols
ret = salt_protocols_append(&protocols, "ECHO", 4);
assert(ret == SALT_SUCCESS);
//Sets the signature used for the salt channel
ret = salt_set_signature(&p_client->channel, host_sk_sec);
assert(ret == SALT_SUCCESS);
//New ephemeral key pair is generated and the read and write nonce is reseted
ret = salt_init_session(&p_client->channel, hndsk_buffer, sizeof(hndsk_buffer));
assert(ret == SALT_SUCCESS);
//Sets the context passed to the user injected read implementation
ret = salt_set_context(&p_client->channel, &p_client->sock_fd, &p_client->sock_fd);
assert(ret == SALT_SUCCESS);
//Set threshold for delay protection
salt_set_delay_threshold(&p_client->channel, 20000);
start_t = clock();
//Salt handshake
ret = salt_handshake(&p_client->channel, NULL);
end_t = clock();
printf("\n");
printf("\t\n***** SERVER:Salt channelv2 handshake lasted: %6.6f sec. *****\n", ((double) (end_t -
start_t) / (CLOCKS_PER_SEC)));
printf("\n");
//Testing success for Salt handshake
while (ret != SALT_SUCCESS) {
if (ret == SALT_ERROR) {
printf("Error during handshake:\r\n");
printf("Salt error: 0x%02x\r\n", p_client->channel.err_code);
printf("Salt error read: 0x%02x\r\n", p_client->channel.read_channel.err_code);
printf("Salt error write: 0x%02x\r\n", p_client->channel.write_channel.err_code);
printf("Connection closed.\r\n");
CLOSESOCKET(p_client->sock_fd);
free(p_client);
break;
}
ret = salt_handshake(&p_client->channel, NULL);
}
if (ret == SALT_SUCCESS) {
printf("\nSalt handshake successful\r\n");
printf("\n");
verify = 1;
}
这是服务器代码,我创建了一个CLIENT
结构,其中包含一个套接字(表示accept()
函数在握手时的值,salt_channel_T
结构需要在连接和 service()
函数中创建握手)。
salt_read_begin_pom()
函数接收来自客户端的加密消息,对其进行验证、解密,并将其打印到屏幕上。
问题在于您传递给 salt_read_begin_pom()
的 channel
。
您没有将分配的 CLIENT
实例存储在任何有用的地方。您只有一个 client_info
变量,每次 accept()
编辑新客户端时都会更新该变量。因此,读取循环中不是服务器套接字的每个 i
套接字最终调用 salt_read_begin_pom()
使用最后一个 accept()
客户端的相同 CLIENT
实例。
您需要将 CLIENT
实例存储在某处,例如在数组或查找 table 中,然后对于具有以下内容的读取循环中的每个非服务器 i
套接字等待读取的数据,找到sock_fd
对应i
对应的CLIENT
,然后用找到的CLIENT
调用salt_read_begin_pom()
.
我尝试创建一个简单的应用程序,其中服务器同时为局域网中的多个客户端提供服务,客户端向服务器发送数据。问题是服务器不知道如何同时与多个客户端通信,它仍然只与最后连接的客户端通信。
我在应用层部署了Salt channel密码协议,保证了传输数据的完整性和安全性。我用 C 语言编程,我使用 Mingw 编译器,我使用 select()
函数和 fd_set
结构同时处理多个套接字。我写了客户端和服务端的代码,这里握手成功,可以同时和所有客户端交换数据。随后,我尝试在代码中部署盐通道应用协议。但是,当我部署协议时,服务器只为最后登录的客户端提供服务。
我创建了客户端的结构,我想它是否包含所有需要的东西。我无法弄清楚为什么服务器一次不能为多个客户端提供服务的错误。我试图测试是否将多个客户端连接到服务器,但客户端不发送任何消息,因此服务器保留了它们的套接字,当我关闭服务器时,所有客户端-服务器连接都关闭了,但是一旦客户端发送消息,当前只有一个客户端-服务器连接。我有一个 while
循环通信,我在 fd_set
结构中添加一个服务器套接字并调用 select()
函数,当套接字可用于握手时,我调用 accept()
并且 return 值是一个特定的客户端套接字,然后我将其添加到 fd_set
结构中,循环继续并查找可用的套接字,当它们检查它是否适合握手时,如果不是,调用一个函数从客户端接收消息并解密它。
//Libraries for working with network tools in Windows
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
//pragma comment nie je potrebny, lebo vyuzivam v Makefile subore flag -lws2_32
//#pragma comment(lib, "ws2_32.lib")
//Constants for working with sockets in Windows
#define ISVALIDSOCKET(s) ((s) != INVALID_SOCKET)
#define CLOSESOCKET(s) closesocket(s)
#define GETSOCKETERRNO() (WSAGetLastError())
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
//Libraries of Salt channelv2
#include "salt.h"
#include "salt_io.h"
#include "salti_util.h"
#include <time.h>
//Function for reads encrypted message
salt_ret_t salt_read_begin_pom(salt_channel_t *p_channel,
uint8_t *p_buffer,
uint32_t buffer_size,
salt_msg_t *p_msg,
uint8_t *p_pom,
uint32_t *p_size);
//Ready sk_sec key for server
static uint8_t host_sk_sec[64] = {
0x7a, 0x77, 0x2f, 0xa9, 0x01, 0x4b, 0x42, 0x33,
0x00, 0x07, 0x6a, 0x2f, 0xf6, 0x46, 0x46, 0x39,
0x52, 0xf1, 0x41, 0xe2, 0xaa, 0x8d, 0x98, 0x26,
0x3c, 0x69, 0x0c, 0x0d, 0x72, 0xee, 0xd5, 0x2d,
0x07, 0xe2, 0x8d, 0x4e, 0xe3, 0x2b, 0xfd, 0xc4,
0xb0, 0x7d, 0x41, 0xc9, 0x21, 0x93, 0xc0, 0xc2,
0x5e, 0xe6, 0xb3, 0x09, 0x4c, 0x62, 0x96, 0xf3,
0x73, 0x41, 0x3b, 0x37, 0x3d, 0x36, 0x16, 0x8b
};
typedef struct{
SOCKET sock_fd;
salt_channel_t channel;
struct sockaddr_storage client_address;
socklen_t client_len;
} CLIENT;
void connection_and_servicing(CLIENT *p_client, SOCKET p_socket);
int main() {
#if defined(_WIN32)
//Variables
SOCKET socket_listen;;
CLIENT *client_info;
uint8_t rx_buffer[UINT16_MAX * 4];
uint8_t hndsk_buffer[SALT_HNDSHK_BUFFER_SIZE];
uint8_t pom_buffer[SALT_HNDSHK_BUFFER_SIZE];
salt_msg_t msg_in;
salt_protocols_t protocols;
salt_msg_t msg_out;
salt_ret_t ret_msg;
uint32_t verify = 0, decrypt_size;
//The MAKEWORD macro allows us to request Winsock version 2.2
WSADATA d;
if (WSAStartup(MAKEWORD(2, 2), &d)) { //inicializacia Winscok-u
fprintf(stderr, "Failed to initialize.\n");
return 1;
}
printf("Configuring local address...\n");
//Struct addrinfo hints
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
//Looking address IPv4
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; //TCP connection
//We ask getaddrinfo () to set the address, for the availability of any network device
hints.ai_flags = AI_PASSIVE;
//Setting a pointer to a structure that contains return information from the getaddrinfo () function
struct addrinfo *bind_address;
getaddrinfo("192.168.100.8", "8080", &hints, &bind_address); //port 8080, generate an address suitable for the bind () function
//Creating socket
printf("Creating socket...\n");
socket_listen = socket(bind_address->ai_family,
bind_address->ai_socktype, bind_address->ai_protocol);
if (!ISVALIDSOCKET(socket_listen)) {
fprintf(stderr, "socket() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
//Binding socket to local address
printf("Binding socket to local address...\n");
if (bind(socket_listen,
bind_address->ai_addr, bind_address->ai_addrlen)) {
fprintf(stderr, "bind() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
//After we've called bind(), we use the freeaddrinfo() function to free the memory for bind_address
puts("Bind done");
freeaddrinfo(bind_address);
printf("Listening...\n");
if (listen(socket_listen, 5) < 0) {
fprintf(stderr, "listen() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
//Define fd_set structure master that stores all of the active sockets
fd_set master;
FD_ZERO(&master);
FD_SET(socket_listen, &master);
SOCKET max_socket = socket_listen;
printf("Waiting for connections...\n");
while(1) {
fd_set reads;
reads = master;
//The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O
if (select(max_socket+1, &reads, 0, 0, 0) < 0) {
fprintf(stderr, "select() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
SOCKET i;
//Loop through each possible socket
for(i = 1; i <= max_socket; ++i) {
if (FD_ISSET(i, &reads)) {
//If socket_listen, create TCP connection of accept() function
if (i == socket_listen) {
client_info = (CLIENT *) malloc(sizeof(CLIENT));
client_info->client_len = sizeof(client_info->client_address);
client_info->sock_fd = accept(socket_listen,
(struct sockaddr*) &client_info->client_address,
&client_info->client_len);
if (!ISVALIDSOCKET(client_info->sock_fd)) {
fprintf(stderr, "accept() failed. (%d)\n",
GETSOCKETERRNO());
return 1;
}
FD_SET(client_info->sock_fd, &master);
if (client_info->sock_fd > max_socket)
max_socket = client_info->sock_fd;
//Prints the client address using the getnameinfo() function
char address_buffer[100];
getnameinfo((struct sockaddr*)&client_info->client_address,
&client_info->client_len,
address_buffer, sizeof(address_buffer), 0, 0,
NI_NUMERICHOST);
printf("New connection %s\n", address_buffer);
printf("\nWaiting for succeses Salt handshake...\n");
connection_and_servicing(client_info, socket_listen);
printf("handshake\n");
} else {
ret_msg = SALT_ERROR;
memset(rx_buffer, 0, sizeof(hndsk_buffer));
ret_msg = salt_read_begin_pom(&client_info->channel, rx_buffer, sizeof(rx_buffer), &msg_in, pom_buffer, &decrypt_size);
continue;
}
} //if FD_ISSET
} //for i to max_socket
} //while(1)
printf("Closing listening socket...\n");
free(client_info);
CLOSESOCKET(socket_listen);
WSACleanup();
#endif
printf("Finished.\n");
return 0;
}
void connection_and_servicing(CLIENT *p_client, SOCKET p_socket)
{
//CLIENT *p_client = (context *);
//SOCKET sock = p_client->sock_fd;
uint8_t hndsk_buffer[SALT_HNDSHK_BUFFER_SIZE];
uint8_t rx_buffer[UINT16_MAX * 4];
uint8_t pom_buffer[SALT_HNDSHK_BUFFER_SIZE];
uint8_t tx_buffer[UINT16_MAX * 4];
uint8_t protocol_buffer[128];
uint32_t verify = 0, decrypt_size;
salt_msg_t msg_out;
salt_ret_t ret;
salt_ret_t ret_msg;
salt_msg_t msg_in;
salt_protocols_t protocols;
clock_t start_t, end_t;
ret = salt_create(&p_client->channel, SALT_SERVER, my_write, my_read, &my_time);
assert(ret == SALT_SUCCESS);
//Initiates to add information about supported protocols to host
ret = salt_protocols_init(&p_client->channel, &protocols, protocol_buffer, sizeof(protocol_buffer));
assert(ret == SALT_SUCCESS);
//Add a protocol to supported protocols
ret = salt_protocols_append(&protocols, "ECHO", 4);
assert(ret == SALT_SUCCESS);
//Sets the signature used for the salt channel
ret = salt_set_signature(&p_client->channel, host_sk_sec);
assert(ret == SALT_SUCCESS);
//New ephemeral key pair is generated and the read and write nonce is reseted
ret = salt_init_session(&p_client->channel, hndsk_buffer, sizeof(hndsk_buffer));
assert(ret == SALT_SUCCESS);
//Sets the context passed to the user injected read implementation
ret = salt_set_context(&p_client->channel, &p_client->sock_fd, &p_client->sock_fd);
assert(ret == SALT_SUCCESS);
//Set threshold for delay protection
salt_set_delay_threshold(&p_client->channel, 20000);
start_t = clock();
//Salt handshake
ret = salt_handshake(&p_client->channel, NULL);
end_t = clock();
printf("\n");
printf("\t\n***** SERVER:Salt channelv2 handshake lasted: %6.6f sec. *****\n", ((double) (end_t -
start_t) / (CLOCKS_PER_SEC)));
printf("\n");
//Testing success for Salt handshake
while (ret != SALT_SUCCESS) {
if (ret == SALT_ERROR) {
printf("Error during handshake:\r\n");
printf("Salt error: 0x%02x\r\n", p_client->channel.err_code);
printf("Salt error read: 0x%02x\r\n", p_client->channel.read_channel.err_code);
printf("Salt error write: 0x%02x\r\n", p_client->channel.write_channel.err_code);
printf("Connection closed.\r\n");
CLOSESOCKET(p_client->sock_fd);
free(p_client);
break;
}
ret = salt_handshake(&p_client->channel, NULL);
}
if (ret == SALT_SUCCESS) {
printf("\nSalt handshake successful\r\n");
printf("\n");
verify = 1;
}
这是服务器代码,我创建了一个CLIENT
结构,其中包含一个套接字(表示accept()
函数在握手时的值,salt_channel_T
结构需要在连接和 service()
函数中创建握手)。
salt_read_begin_pom()
函数接收来自客户端的加密消息,对其进行验证、解密,并将其打印到屏幕上。
问题在于您传递给 salt_read_begin_pom()
的 channel
。
您没有将分配的 CLIENT
实例存储在任何有用的地方。您只有一个 client_info
变量,每次 accept()
编辑新客户端时都会更新该变量。因此,读取循环中不是服务器套接字的每个 i
套接字最终调用 salt_read_begin_pom()
使用最后一个 accept()
客户端的相同 CLIENT
实例。
您需要将 CLIENT
实例存储在某处,例如在数组或查找 table 中,然后对于具有以下内容的读取循环中的每个非服务器 i
套接字等待读取的数据,找到sock_fd
对应i
对应的CLIENT
,然后用找到的CLIENT
调用salt_read_begin_pom()
.