我对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()方法。这些函数应该等到read(th_r)和write(th_w)线程完成并放弃它们使用的资源。我遇到的问题是这些方法显然从未被调用过。
当我启动客户端时,我看到它到达&#34; hello1&#34;马上,但当客户端或服务器终止通信流时,我看不到&#34; hello2&#34;或&#34; hello3,&#34;这意味着两者都没有达到。
有人可以对我失踪的内容有所了解吗?
答案 0 :(得分:2)
你不应该在线程中调用exit
,当线程完成时你 应该return NULL
。调用exit
只会终止整个程序。