Archived
3
0

some bugs need to fix : select()

This commit is contained in:
lapupe 2016-09-22 18:38:24 +02:00
parent 3615c3512d
commit 4fda0231ce
2 changed files with 62 additions and 56 deletions

View File

@ -73,9 +73,8 @@ int read_message(int sock, char *buffer)
return n; return n;
} }
void endConnection(int sock, int sfd) { void endConnection(int sock) {
close(sock); close(sock);
close(sfd);
} }
void printClientAddr(struct sockaddr_in *csin) { void printClientAddr(struct sockaddr_in *csin) {
@ -85,46 +84,6 @@ void printClientAddr(struct sockaddr_in *csin) {
} }
int main(int argc, char * argv[], char **env) {
int sock = init_connection();
// char buffer[BUF_SIZE];
struct sockaddr_in csin = { 0 };
socklen_t sinsize = sizeof csin;
int sfd = accept(sock, (struct sockaddr *)&csin, &sinsize);
if(sfd == -1)
{
perror("accept()");
close(sock);
exit(errno);
}
printClientAddr(&csin);
printf("%d \n",getpid());
pthread_t listenPid;
p_data *p_d_listen = malloc (sizeof(p_data));
//p_d_listen.c_sock = NULL;
p_d_listen->sfd = sfd;
p_d_listen->index = 0;
int ret = pthread_create( &listenPid, NULL, &service_thread, (void *) p_d_listen);
if (ret) {
perror("pthread_create()");
endConnection(sock, sfd);
exit(errno);
} else {
printf("Creation of listen thread \n");
}
pthread_join(listenPid, NULL);
endConnection(sock, sfd);
return 0;
}
void * service_thread(void * pdata) { void * service_thread(void * pdata) {
p_data *pda = (p_data*) pdata; p_data *pda = (p_data*) pdata;
char buffer[BUF_SIZE]; char buffer[BUF_SIZE];
@ -181,7 +140,6 @@ void * service_thread(void * pdata) {
return NULL; return NULL;
} }
printf("pathname[0] : %s\n", pathname[0]);
//open -in fifo file //open -in fifo file
int fdin = open (pathname[0], O_RDWR); int fdin = open (pathname[0], O_RDWR);
if (fdin <= 0) { if (fdin <= 0) {
@ -193,8 +151,8 @@ void * service_thread(void * pdata) {
//utilisation du select() pour surveiller la socket du client et fichier in //utilisation du select() pour surveiller la socket du client et fichier in
fd_set rdfs; fd_set rdfs;
int max = clientSock > fdin ? clientSock : fdin; int max = clientSock > fdin ? clientSock : fdin;
max = max > STDIN_FILENO ? max : STDIN_FILENO;
printf("Waitting for new messages :\n" );
while(1) { while(1) {
FD_ZERO(&rdfs); FD_ZERO(&rdfs);
@ -218,24 +176,34 @@ void * service_thread(void * pdata) {
{ {
/* stop process when type on keyboard */ /* stop process when type on keyboard */
break; break;
}else if (FD_ISSET(clientSock, &rdfs)) { }else /*if (FD_ISSET(fdin, &rdfs))*/{
if(read_message(clientSock, buffer) > 0) { if (FD_ISSET(clientSock, &rdfs)) {
printf("message : %s\n",buffer ); if(read_message(clientSock, buffer) > 0) {
printf("message : %s\n",buffer );
}
if(file_write(pathname[1], buffer, strlen(buffer)) < 0) {
perror("file_write");
}
printf("ok\n");
} }
/*if(file_write(pathname[1], buffer, strlen(buffer)) < 0) { if (FD_ISSET(fdin, &rdfs)) {
perror("file_write");
}*/ if(read(fdin, &buffer, BUF_SIZE) < 0) {
}else { perror("read()");
if(read(fdin, &buffer, BUF_SIZE) < 0) { }
perror("read()"); printf("message from file in : %s\n", buffer );
} }
printf("file in : %s\n", buffer );
} }
} }
free(servicePath); free(servicePath);
free(piv); free(piv);
//close the files descriptors
close(fdin);
close(clientSock);
return NULL; return NULL;
} }
@ -346,4 +314,42 @@ void makePivMessage (char ** piv, int pid, int index, int version) {
strcat(*piv, versionStr); strcat(*piv, versionStr);
} }
int main(int argc, char * argv[], char **env) {
int sock = init_connection();
// char buffer[BUF_SIZE];
struct sockaddr_in csin = { 0 };
socklen_t sinsize = sizeof csin;
int sfd = accept(sock, (struct sockaddr *)&csin, &sinsize);
if(sfd == -1)
{
perror("accept()");
close(sock);
exit(errno);
}
printClientAddr(&csin);
printf("%d \n",getpid());
pthread_t listenPid;
p_data *p_d_listen = malloc (sizeof(p_data));
//p_d_listen.c_sock = NULL;
p_d_listen->sfd = sfd;
p_d_listen->index = 0;
int ret = pthread_create( &listenPid, NULL, &service_thread, (void *) p_d_listen);
if (ret) {
perror("pthread_create()");
endConnection(sock);
exit(errno);
} else {
printf("Creation of listen thread \n");
}
pthread_join(listenPid, NULL);
endConnection(sock);
return 0;
}

View File

@ -13,7 +13,7 @@ typedef struct {
} p_data; } p_data;
int initConnection (); int initConnection ();
void endConnection (int sock, int csock); void endConnection (int sock);
void printClientAddr (struct sockaddr_in *csin); void printClientAddr (struct sockaddr_in *csin);