Archived
3
0

tcpserver : malloc

This commit is contained in:
Philippe PITTOLI 2016-09-26 14:15:56 +02:00
parent f3f7927d25
commit a35ba9ce45

View File

@ -60,18 +60,7 @@ void write_message(int sock, const char *buffer)
int read_message(int sock, char *buffer)
{
int n = 0;
if((n = recv(sock, buffer, BUF_SIZE - 1, 0)) < 0)
{
perror("recv()");
/* if recv error we disonnect the client */
n = 0;
}
buffer[n] = 0;
return n;
return recv(sock, buffer, BUF_SIZE - 1, 0);
}
void endConnection(int sock) {
@ -105,17 +94,20 @@ void * service_thread(void * c_data) {
*/
//path service
char * servicePath = (char*) calloc((strlen(TMPDIR) + strlen(service) + 1), sizeof(char));
char * servicePath = malloc (strlen(TMPDIR) + strlen(service) + 1);
memset (servicePath, 0, strlen(TMPDIR) + strlen(service) + 1);
if (servicePath == NULL) {
perror("calloc()");
perror("malloc()");
}
strcat(servicePath, TMPDIR);
strcat(servicePath, service);
//pid index version
char * piv = (char*) calloc(PATH_MAX, sizeof(char));
char * piv = (char*) malloc(PATH_MAX);
memset(piv , 0, PATH_MAX);
if (piv == NULL) {
perror("calloc()");
perror("malloc()");
}
makePivMessage(&piv, getpid(), cda->index, version);
printf("piv : %s\n",piv );
@ -131,13 +123,15 @@ void * service_thread(void * c_data) {
// gets the service path, such as /tmp/ipc/pid-index-version-in/out
char *pathname[2];
pathname[0] = (char*) calloc(PATH_MAX, sizeof(char));
pathname[0] = (char*) malloc(PATH_MAX);
memset(pathname[0], 0, PATH_MAX);
if (pathname[0] == NULL) {
perror("calloc()");
perror("malloc()");
}
pathname[1] = (char*) calloc(PATH_MAX, sizeof(char));
pathname[1] = malloc(PATH_MAX);
memset(pathname[1] , 0, PATH_MAX);
if (pathname[1] == NULL) {
perror("calloc()");
perror("malloc()");
}
inOutPathCreate(pathname, cda->index, version);
@ -191,13 +185,15 @@ void * service_thread(void * c_data) {
break;
}else /*if (FD_ISSET(fdin, &rdfs))*/{
if (FD_ISSET(clientSock, &rdfs)) {
if(read_message(clientSock, buffer) > 0) {
printf("message : %s\n",buffer );
int n = 0;
n = read_message(clientSock, buffer);
if(n > 0) {
if(file_write(pathname[1], buffer, strlen(buffer)) < 0) {
perror("file_write");
}
printf("ok\n");
}
printf("message (%d bytes) : %s\n", n, buffer);
}
if (FD_ISSET(fdin, &rdfs)) {
@ -208,6 +204,7 @@ void * service_thread(void * c_data) {
printf("message from file in : %s\n", buffer );
}
}
memset (buffer, 0, BUF_SIZE);
}
//free