简体   繁体   English

accept()函数中断程序。 C

[英]accept() function breaks program. C

I have no idea why the accept() function in the following server thread breaks the program. 我不知道为什么以下服务器线程中的accept()函数会破坏程序。

header file 头文件

/*
 * 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 file .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;
}

The program prints "Server is waiting", but never reaches "test", and just stops working. 该程序将显示“服务器正在等待”,但永远不会到达“测试”,并且只会停止工作。 I launch it from another .c file in the same project using the init_tcpip_server() function. 我使用init_tcpip_server()函数从同一项目中的另一个.c文件启动它。 I had no errors during compilation. 编译期间没有错误。

Ok. 好。 I found my failure. 我发现自己失败了。 I'm new to threads, and didn't got used to the fact that the main just continued working and exited, closing everything. 我是线程的新手,并且不习惯于主线程继续工作并退出,关闭所有内容。 In this case between the "server is waiting" and "test". 在这种情况下,“服务器正在等待”和“测试”之间。 After adding a delay in the main everything worked. 在延长主时间后,一切正常。 I am sorry for wasting your time. 很抱歉浪费您的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM