Application is formed by several threads. This is fundamental code of thread
(tcpip.c) where the problem happens.
Application opens COM1, COM2 and other files too (in others threads).
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <errno.h>
#include “constants.h”
#include “extern.h”
#include “protocol.h”
#include “tcpip.h”
// Note: All variables start with ‘g_’ are application´s global
variables
// ********************** thread´s global variables
struct sockaddr_in their_addr;
// ******************** thread´s functions ******************
int InitSocket(void);
void CommandWait(void);
void *thread_socket(void *pArgs) {
int sin_size;
int nbytes;
if (InitSocket() != 0) {
close(g_listener);
return (void*)1;
}
while(g_bRunSocket) {
sin_size = sizeof(struct sockaddr_in);
if (g_cliente = accept(g_listener, (struct sockaddr
*)&their_addr, &sin_size) == -1) {
printf(“Error en accept\n”);
close(g_listener);
break;
}
printf(“connection server %s:%d\n”,
inet_ntoa(their_addr.sin_addr), ntohs (their_addr.sin_port));
// g_client = 0
g_client = g_listener + 1; // if I del
this line, “OK\n” is sent to console, and not to client. why???
nbytes = send(g_client, “OK\n”, 3, 0);
if (nbytes == -1) printf(“Error en send\n”);
do {
WaitCommand();
}while(g_client != NULL);
}
return 0;
}
int InitSocket(void)
{
struct sockaddr_in my_addr;
int yes = 1;
if ((g_listener = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror(“Error en socket\n”);
return 1;
}
// g_listener = 3
if (setsockopt(g_listener, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
perror(“Error en setsockopt\n”);
return 1;
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
//communication port (7000)
my_addr.sin_addr.s_addr = htonl(INADDR_ANY); //my IP
memset(&(my_addr.sin_zero), ‘\0’, ;
if (bind(g_listener, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr)) == -1) {
perror(“Error en bind\n”);
return 1;
}
if (listen(g_listener, BACKLOG) == -1) {
perror(“Error en listen\n”);
return 1;
}
return 0;
}
void CommandWait(void) {
int nbytes;
unsigned char buf[256];
BYTE i, chk;
nbytes = recv(g_client, buf, 256, 0);
if (nbytes <= 0) {
close(g_client);
g_client = NULL;
}
else {
if ((nbytes == 5) || (nbytes == 22) || (nbytes == 25)) {
if(buf[0] == ‘$’ && buf[nbytes-1] == ‘#’) {
chk = 0;
for (i=1;i<nbytes-2;i++) chk = chk + buf_;
if (buf[nbytes-2] == chk) {
if (buf[1] == ‘C’ && buf[2] == ‘F’) Config(1);
else if (buf[1] == ‘G’ && buf[2] == ‘O’)
Run(1);
else if (buf[1] == ‘W’ && buf[2] == ‘P’) {
if (CONFIG) ModifyParam(buf, 1);
}
else if (buf[1] == ‘R’ && buf[2] == ‘P’)
ReadParam(1);
else if (buf[1] == ‘L’ && buf[2] == ‘F’)
ListFiles(1);
else if (buf[1] == ‘D’ && buf[2] == ‘F’)
DelFile(buf);
else if (buf[1] == ‘D’ && buf[2] == ‘W’)
DownloadFile(buf, 1);
else if (buf[1] == ‘S’ && buf[2] == ‘T’)
Stop(1);
}
else {
printf(“checksum´s error!\n”);
}
}
else {
printf(“packet´s error!!!\n”);
}
}
}
// This function belongs to protocol.c
int DownloadFile(char* c, BOOL bTransmition) {
char szFile[20];
int fdw;
int size_read;
char buffer[512];
char szInit[] = “$SF”;
char szEnd[] = “OK#”;
char szError[] = “ER#”;
strcpy(szFile, “/tmp/”);
strncat(szFile, &c[3], CHARSFILE);
fdw = open(szFile, O_RDONLY); //here SIGSEGV signal is
produced!!!
if (fdw != NULL) {
//Send data init
if (bTransmition == 0) write(g_Com1, szInicio, 3);
else send(g_client, szInit, 3, 0);
//Send data file
do {
size_read = read(fdw, buffer, sizeof(buffer));
if (size_read == -1) {
//send data error
if (bTransmition == 0) write(g_Com1, szError, 3);
else send(g_client, szError, 3, 0);
return 1;
}
if (bTransmition == 0) write(g_Com1, buffer, size_read);
else send(g_client, buffer, size_read, 0);
}while (size_read == sizeof(buffer));
//send data end
if (bTransmition == 0) write(g_Com1, szFin, 3);
else send(g_client, szEnd, 3, 0);
close(fdw);
}
return 0;
}_