accept()函数中断程序。 C

时间:2013-01-11 12:16:21

标签: c sockets

我不知道为什么以下服务器线程中的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文件启动它。编译时我没有错误。

1 个答案:

答案 0 :(得分:1)

确定。我发现了我的失败。我是线程的新手,并且不习惯主要只是继续工作和退出,关闭一切。在这种情况下,“服务器正在等待”和“测试”。在主要加入延迟之后一切顺利。我很抱歉浪费你的时间。