G
GVK
Hi,
I'm facing another problem with my network program. I'm writing a basic server
that handles multiple clients. Whenever a client is connected, a new thread is
created to handle it.
The socket descriptor of the client and it's sockaddr_in structure are passed
as arguments to the function that handles the client. Now, the following code is
giving me an error. It's the function that handles the client:
--------------------------------------------------------------------------------
void *
handle_client(void *arg)
{
pthread_detach(pthread_self());
int clientfd,temp;
char *mesg;
struct sockaddr_in client;
sock_arg *func_arg = (sock_arg *)arg;
mesg = (char *)malloc(100 * sizeof(mesg));
/*
sock_arg is my data type.
struct
{
int sockfd; //socket descriptor
struct sockaddr_in server; // info. of client connected
}
*/
clientfd = func_arg->sockfd;
client = func_arg->server;
/* printing welcome message to the client - works fine */
mesg = "welcome to network-sort server\n";
write(clientfd,mesg,strlen(mesg));
/* this block of code gives the error */
if( read(clientfd,mesg,1024) < 0 )
{
printf("read() error in %s: %s\n",__FUNCTION__,strerror(errno));
exit(1);
}
temp = atoi(mesg);
if( temp == S_OK )
printf("server said: OK\n");
else if( temp == S_NOK )
printf("server said: NOT OK\n");
else
printf("unrecognized code - call the sys admin\n");
return;
}
Here's the sample output for a session: ( the above code is in monitor.c )
SERVER:
[project@voors implementation]$ ./monitor
server started on 0.0.0.0 - listening on port 9999
connection from 172.16.14.52:2067
creating a new thread to handle the client
read() error in handle_client: Bad address
CLIENT:
[project@voors project]$ telnet 172.16.14.52 9999
Trying 172.16.14.52...
Connected to 172.16.14.52.
Escape character is '^]'.
welcome to network-sort server
1 /* input */
Connection closed by foreign host.
--------------------------------------------------------------------------------
I've no idea what's going wrong. This is the first time I'm using threads. Any
help is appreciated.
regards,
GVK
I'm facing another problem with my network program. I'm writing a basic server
that handles multiple clients. Whenever a client is connected, a new thread is
created to handle it.
The socket descriptor of the client and it's sockaddr_in structure are passed
as arguments to the function that handles the client. Now, the following code is
giving me an error. It's the function that handles the client:
--------------------------------------------------------------------------------
void *
handle_client(void *arg)
{
pthread_detach(pthread_self());
int clientfd,temp;
char *mesg;
struct sockaddr_in client;
sock_arg *func_arg = (sock_arg *)arg;
mesg = (char *)malloc(100 * sizeof(mesg));
/*
sock_arg is my data type.
struct
{
int sockfd; //socket descriptor
struct sockaddr_in server; // info. of client connected
}
*/
clientfd = func_arg->sockfd;
client = func_arg->server;
/* printing welcome message to the client - works fine */
mesg = "welcome to network-sort server\n";
write(clientfd,mesg,strlen(mesg));
/* this block of code gives the error */
if( read(clientfd,mesg,1024) < 0 )
{
printf("read() error in %s: %s\n",__FUNCTION__,strerror(errno));
exit(1);
}
temp = atoi(mesg);
if( temp == S_OK )
printf("server said: OK\n");
else if( temp == S_NOK )
printf("server said: NOT OK\n");
else
printf("unrecognized code - call the sys admin\n");
return;
}
Here's the sample output for a session: ( the above code is in monitor.c )
SERVER:
[project@voors implementation]$ ./monitor
server started on 0.0.0.0 - listening on port 9999
connection from 172.16.14.52:2067
creating a new thread to handle the client
read() error in handle_client: Bad address
CLIENT:
[project@voors project]$ telnet 172.16.14.52 9999
Trying 172.16.14.52...
Connected to 172.16.14.52.
Escape character is '^]'.
welcome to network-sort server
1 /* input */
Connection closed by foreign host.
--------------------------------------------------------------------------------
I've no idea what's going wrong. This is the first time I'm using threads. Any
help is appreciated.
regards,
GVK