Obsolete
/
libipc-old
Archived
3
0
Fork 0

tcpserver : malloc

more_to_read
Philippe PITTOLI 2016-09-26 14:15:56 +02:00
parent f3f7927d25
commit a35ba9ce45
1 changed files with 259 additions and 262 deletions

View File

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