gpt4 book ai didi

c - Accept() 函数会中断程序。 C

转载 作者:行者123 更新时间:2023-11-30 15:52:38 25 4
gpt4 key购买 nike

我不知道为什么以下服务器线程中的accept()函数会破坏程序。

头文件

/*
* tcpip_server.h
*
* Created on: 2013-01-10
* Author: Lukasz
*/

#ifndef TCPIP_SERVER_H_
#define TCPIP_SERVER_H_

int init_tcpip_server();

#endif /* TCPIP_SERVER_H_ */

.c文件

*
* tcpip_server.c
*
* Created on: 2013-01-10
* Author: Lukasz
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

#include "tcpip_server.h"


/* Logger thread prototype */
void *tServerThreadFunc(void *);

/* Thread variable */
pthread_t tServerThread;
/* Thread attributes structure */
pthread_attr_t aServerThreadAttr;

/**
* Function crates server thread
*/
int init_tcpip_server() {

int status;
printf("begginning init_tcpip_server()\n");
/* Set server scheduling policy to FIFO */
pthread_attr_init(&aServerThreadAttr);
pthread_attr_setschedpolicy(&aServerThreadAttr, SCHED_FIFO);
printf("before ServerThread createion\n");
/* Create server thread */
if ((status = pthread_create( &tServerThread, NULL, tServerThreadFunc, &aServerThreadAttr))) {
fprintf(stderr, "Cannot create thread.\n");
return 0;
}

return 0;
}

/**
* Server thread function
*/
void *tServerThreadFunc(void *cookie) {

/* Scheduling policy: FIFO or RR */
int policy;
/* Structure of other thread parameters */
struct sched_param param;
/* Socket address structure */
struct sockaddr_in socket_addr;
/* Socket variable */
int srv_socket;
/* Buffer */
char buff[100];
char answer[100];


/* Read modify and set new thread priority */
pthread_getschedparam( pthread_self(), &policy, &param);
param.sched_priority = sched_get_priority_min(policy);
pthread_setschedparam( pthread_self(), policy, &param);
printf("before socket\n");
/* Initialize socket variable */
srv_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

if(srv_socket == -1) {
fprintf(stderr, "Cannot create socket\n");
return 0;
}

/* Initialize socket address to 0*/
memset(&socket_addr, 0, sizeof(socket_addr));
/* Set socket address parameters */
socket_addr.sin_family = AF_INET;
socket_addr.sin_port = htons(1200);
socket_addr.sin_addr.s_addr = INADDR_ANY;

/* Bind socket to socket address struct */
if(bind(srv_socket, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) == -1) {
fprintf(stderr, "Cannot bind socket\n");
close(srv_socket);
return 0;
}

/* Start to listen on created socket */
if(listen(srv_socket, 10) == -1) {
fprintf(stderr, "Cannot start to listen\n");
close(srv_socket);
return 0;
}

printf("Server is waiting.\n");

/* Wait until somebody open connection with you */
int srv_connection = accept(srv_socket, NULL, NULL); /* <= program breaks here. */

printf("test\n");

/* Check if connection is OK */
if(srv_connection < 0) {
fprintf(stderr, "Accept connection failed\n");
close(srv_socket);
return 0;
}

for(;;) {

}
return 0;
}

程序打印“服务器正在等待”,但从未到达“测试”,并且只是停止工作。我使用 init_tcpip_server() 函数从同一项目中的另一个 .c 文件启动它。我在编译过程中没有出现错误。

最佳答案

好的。我发现了我的失败。我对线程很陌生,并且不习惯主线程继续工作并退出,关闭所有内容的事实。在这种情况下,介于“服务器正在等待”和“测试”之间。在主要内容中添加延迟后,一切正常。很抱歉浪费了您的时间。

关于c - Accept() 函数会中断程序。 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14278022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com