if i open multiple clients then i m getting previous msgs also at the server end,below is the code. connect 3 clients and send msgs to the server and check the flaw and let me know the mistakes.
And may be using threads is not required at client end but I am using it for my convenience, is that creating a problem?
server.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
int clients = 0;
int sockfd = 0,newsockfd = 0;
int sock_buf[100];
void error(char *msg)
{
perror(msg);
exit(1);
}
void *read_function(void* param)
{
int n = 0;
int newsockfd = (int)param;
char buffer[256];
while(1)
{
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0)
{
error("ERROR reading from socket");
}
else
{
printf(" Here is the message: %s",buffer);
fflush(stdout);
}
buffer[0] = '\0';
}
return NULL;
}
void* write_function(void* param)
{
int n = 0;
char *temp =(char*) malloc(10);
int newsockfd = (int) param;
char buffer[256];
char tbuf[257];
while(1)
{
// printf("I m server for client %d\n",clients);
// printf("Please enter the message for client: ");
bzero(buffer,256);
//bzero(tbuf,256);
fgets(buffer,255,stdin);
void* write_function()
{
int n = 0;
char buffer[256];
while(1)
{
printf("Please enter the message for server: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
{
error("ERROR writing to socket");
}
bzero(buffer,256);
sleep(1);
}
return NULL;
}
void* read_function()
{
int n = 0;
char buffer[256];
while(1)
{
bzero(buffer,256);
n = read(sockfd,buffer,255);
// printf("i m client %c\n",buffer[0]);
mycon_num = strtol(buffer,NULL,10);
printf("i m client %d\n",mycon_num);
if (n < 0)
{
error("ERROR reading from socket");
}
else
{
printf("\t\t\t\t\t\t\t Here is the message: %s",buffer);
}
sleep(1);
}
return NULL;
}
int main(int argc, char *argv[])
{
int newsockfd, portno, n, m, opt;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
pid_t child_pid;
Can you please use the code format tag on the right to format your code. You don't need to send a new post, you can just edit what you've already posted.
Why is the server using separate threads to read and write? If you're going to use threads, then each thread should handle a connection to a client. It reads the request from the client, does some work, then replies. Asynchronously reading and writing to the client doesn't make much sense.
The same goes for the client. In fact, there's no reason for the client to use threads at all.
i dont have this issue with your code. when i send a message from your client to your server the server prints the message. i can't even imagine why it should not print the message. you could try this, but i don't think that it makes any difference
void *read_function()
{
int n = 0;
char buffer[256];
while(1)
{
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0)
{
error("ERROR reading from socket");
}
else
{
printf(" Here is the message: %s",buffer);
fflush(stdout); // to flush the output buffer, means print it now
}
}
return NULL;
}