Reputation: 2317
I have no idea why the accept() function in the following server thread breaks the program.
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
*
* 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, ¶m);
param.sched_priority = sched_get_priority_min(policy);
pthread_setschedparam( pthread_self(), policy, ¶m);
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. I had no errors during compilation.
Upvotes: 0
Views: 271
Reputation: 2317
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.
Upvotes: 1