This server can handle 2 types of clients one which request one shot data transmission and another requested periodic data transmission.
It wait for control character from client to start and stop transmission.
For one shot ‘c’ start transmission and for periodic ‘a’ starts and ‘b’ stops it.
But as the execution comes to condition checking for ‘a’ the execution is halted there and child starts receiving unpredictable value like (0,32767).
When i checked with gdb than it also not showing any thing as come to “if(control==’a’)”.
Can u suggest me some thing to fix this problem.
`while(1)
{
sin_size = sizeof(struct sockaddr_storage);
connected = accept(sock1, (struct sockaddr *)&client_addr,&sin_size);
//connecteds[count]=connected;
//clients[count]=client_addr;
if(connected == -1){
perror("accept");
continue;
}
count++;
printf("\n%d %d\n",count,connected);
inet_ntop(client_addr.ss_family,get_in_addr((struct sockaddr *)&client_addr),s, sizeof s);
printf("server: got connection from %s\n", s);
pid=fork();
if(pid<0)perror("error in fork");
if(pid==0)
{
while(1){
int no;
char control;
printf("\n waiting for data request from clients:.......\n");
fflush(stdout);
bzero(&control,1);
while(1){
bytes_recieved = recv(connected,&control,1,0);
if(control=='a'||control=='b'||control=='c')
break;
}
r1.control1=control;
r1.client=client_addr;
send(sock,&r1,sizeof(struct recieved),0);
printf("\nreceived data request is :%c from client (%s)\n", control,s);
fflush(stdout);
if(control=='a')
{printf("..................");
struct argument *argu;
argu->dis=connected;
argu->sock=sock;
argu->cli=client_addr;
//inet_ntop(client_addr.ss_family,get_in_addr((struct sockaddr *)&client),s, sizeof s);
printf("transmission started for client:%s\n",s);
fflush(stdout);
pthread_create(&tid1,NULL,sends,(void*)argu);
pthread_create(&tid2,NULL,recieve,(void*)argu);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
control=value;
printf("comming here\n");
fflush(stdout);
}
if(control=='b')
{
printf("terminating the connection with client (%s).\n",s);
fflush(stdout);
break;
}
if(control=='c')
{
while(1){
recv(sock,&data1,sizeof(struct dataset),0);
if(data1.data==200)
break;
}
send(connected,&data1,sizeof(struct dataset),0);
printf("getting data from server %c\n%d\nterminating the connection with client (%s).\n",data1.a,data1.data,s);
fflush(stdout);
break;
}
}//end of while2
}//end of child
`
You do not allocate memory for the
argupointer.Remember to
freethe pointer after the two calls topthread_join.Edit
Better yet, instead of using a pointer, just use the structure: