我对 C 中的多线程非常缺乏经验,因此我非常感谢您对这段客户端代码的一些输入(为简单起见,已删除了无关部分)。
// client_read(): read from server
void *client_read(int sockfd) {
char text[MAX_TEXT_SIZE + 2];
while (1) {
// clean out buffer
bzero(text, sizeof(text));
// read from server
if (read(sockfd, text, sizeof(text)) <= 0) {
fprintf(stderr, "failed to read\n");
exit(1);
}
// print message from server to stdout
fprintf(stdout, "%s", text);
// quit conversation
if (strncmp(text, "bye\n", 4) == 0) {
fprintf(stdout, "end to conversation\n");
close(sockfd);
exit(0);
}
}
}
// client_write(): write to server
void *client_write(int sockfd) {
char text[MAX_TEXT_SIZE + 2];
int c, d, i;
size_t text_len;
while (1) {
// clean out buffer
bzero(text, sizeof(text));
i = 0;
// read from stdin
while ((c = getchar()) != '\n' && c != EOF && i < MAX_TEXT_SIZE) {
text[i++] = c;
}
// clean out stdin if MAX_TEXT_SIZE exceeded
if (i == MAX_TEXT_SIZE && c != '\n') {
while ((d = getchar()) != EOF && d != '\n') {
}
}
text[i++] = '\n';
text[i] = '\0';
text_len = strlen(text);
// write to server
if (write(sockfd, text, text_len) != text_len) {
fprintf(stderr, "sent wrong number of bytes\n");
exit(1);
}
// quit conversation
if (strncmp(text, "bye\n", 4) == 0) {
fprintf(stdout, "end to conversation\n");
close(sockfd);
exit(0);
}
}
}
int main(int argc, char **argv) {
char keyword[MAX_KEY_SIZE + 1], *server_IP;
in_port_t server_port = 6000;
int r_val, rtn_val, sockfd, w_val;
pthread_t th_r, th_w;
struct sockaddr_in server_addr;
// checking number of command line arguments
if (argc != 3 && argc != 5) {
fprintf(stderr, "Usage: ./client server_IP [-p server_port] key\n");
exit(1);
}
// Usage: ./client server_IP key
else if (argc == 3) {
if (strlen(argv[2]) > MAX_KEY_SIZE) {
fprintf(stderr, "maximum key length exceeded\n");
exit(1);
}
else {
strcpy(keyword, argv[2]);
}
}
// Usage: ./client server_IP -p server_port key
else {
if (strcmp("-p", argv[2]) != 0) {
fprintf(stderr, "client: illegal option %s\n", argv[2]);
exit(1);
}
else {
server_port = atoi(argv[3]);
}
if (strlen(argv[4]) > MAX_KEY_SIZE) {
fprintf(stderr, "maximum key length exceeded\n");
exit(1);
}
else {
strcpy(keyword, argv[4]);
}
}
server_IP = argv[1];
// creating reliable stream socket using TCP
if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fprintf(stderr, "unable to create TCP socket\n");
exit(1);
}
// Constructing server address structure
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
// Converting address into proper format
if ((rtn_val = inet_pton(AF_INET, server_IP, &server_addr.sin_addr.s_addr)) == 0) {
fprintf(stderr, "invalid IP address\n");
exit(1);
}
else if (rtn_val < 0) {
fprintf(stderr, "failed to convert IP string\n");
exit(1);
}
// server port
server_addr.sin_port = htons(server_port);
// connecting to server
if (connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
fprintf(stderr, "failed to connect to server\n");
exit(1);
}
// beginning chat
else {
fprintf(stdout, "successfully connected to server\n");
// starting separate thread for reading from server
if ((r_val = pthread_create(&th_r, NULL, (void *) client_read, (void *) sockfd)) != 0) {
fprintf(stderr, "failed to create read thread\n");
exit(1);
}
// starting separate thread for writing to server
if ((w_val = pthread_create(&th_w, NULL, (void *) client_write, (void *) sockfd)) != 0) {
fprintf(stderr, "failed to create write thread\n");
exit(1);
}
// Waiting until threads close to return resources
printf("hello1\n");
pthread_join(th_r, NULL);
printf("hello2\n");
pthread_join(th_w, NULL);
printf("hello3\n");
}
return 0;
}
对于那些熟悉客户端-服务器编程的人来说,你应该看到除了使用 POSIX 线程之外,客户端代码在处理用户输入和读取和写入服务器方面并没有什么特别之处。我决定使用单独的线程进行读写;否则,客户端将不得不写一些东西,等待服务器回复,然后再写一些东西。
我在 main() 的最后使用 pthread_join() 方法。这些函数应该等到读取 (th_r) 和写入 (th_w) 线程完成并放弃它们使用的资源。我遇到的问题是这些方法显然从未被调用过。
当我启动客户端时,我看到它立即到达“hello1”,但是当客户端或服务器终止通信流时,我看不到“hello2”或“hello3”,这意味着两者都没有到达。
有人可以阐明我所缺少的吗?