independent client server

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);

printf("\n............debug...................\n");
sprintf(temp,"%d",clients);
// printf("clients %d\n",clients);

strncpy(tbuf,temp,1);
strcat(tbuf,buffer);

// printf("-----%s\n",tbuf);

int i = 0;

for(i = 0;i <= clients;i++)
{

// printf("\n............debug...................\n");

n = write(sock_buf[i],tbuf,strlen(tbuf));

//n = write(newsockfd,tbuf,strlen(tbuf));
if (n < 0)
{
error("ERROR writing to socket");
}

}

tbuf[0] = '\0';
sleep(1);
}

return NULL;

}

void* service_client(void* param)
{

int newsockfd = 0;

int n = 0;
char buffer[256];
newsockfd = (int)param;

printf("new sockfd is %d\n",newsockfd);
/* while(1)
{

bzero(buffer,256);
n = read(newsockfd,buffer,255);

if(n < 0)
{
error("ERROR reading froom socket\n");
}
else
{
printf("client sent %s\n",buffer);
}

printf("Enter msg for client : ");
bzero(buffer,255);
fgets(buffer,255,stdin);

n = write(newsockfd,buffer,strlen(buffer));
if (n < 0)
{
error("ERROR writing to socket");
}

}
*/



pthread_t read_thread,write_thread;

pthread_create(&read_thread,NULL,read_function,(void*)param);
pthread_create(&write_thread,NULL,write_function,(void*)param);

pthread_join(write_thread,NULL);
pthread_join(read_thread,NULL);
}

int main(int argc, char *argv[])
{

int sockfd1, command, portno, clilen, n,newsockfd1;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
pid_t child_pid;

pthread_t read_thread,write_thread,new_client;

if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)
error("ERROR opening socket");

bzero((char *) &serv_addr, sizeof(serv_addr));

portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = INADDR_ANY;


if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)

error("ERROR on binding");



while(1)
{



listen(sockfd,5);
//printf("wating in listen\n");
clilen = sizeof(cli_addr);

//printf("accept\n");
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

if (newsockfd < 0)
error("ERROR on accept");


sock_buf[clients] = newsockfd;
clients++;


// child_pid = fork();
// if(child_pid == 0)
// {

pthread_create(&new_client,NULL,service_client,(void*)newsockfd);

// } //pthread_create(&read_thread,NULL,read_function,NULL);

//pthread_create(&write_thread,NULL,write_function,NULL);


//}
//pthread_join(write_thread,NULL);

//pthread_join(read_thread,NULL);

}
return 0;



}

client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>

void error(char *msg)
{
perror(msg);
exit(0);
}

int sockfd = 0;
int mycon_num = 0;

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;

pthread_t read_thread,write_thread;

if (argc < 3)
{
fprintf(stderr,"usage %s hostname port", argv[0]);
exit(0);
}

portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);

if (server == NULL)
{
fprintf(stderr,"ERROR, no such host");
exit(0);
}

//struct hostent *gethostbyname(char *name)



bzero((char *) &serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
//void bcopy(char *s1, char *s2, int length)

if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)

error("ERROR connecting");
printf("seok fd is %d\n",sockfd);
//{

pthread_create(&read_thread,NULL,read_function,NULL);

pthread_create(&write_thread,NULL,write_function,NULL);

pthread_join(read_thread,NULL);
pthread_join(write_thread,NULL);

return 0;

}
Last edited on
Welcome to the forum.

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
}
i can't even imagine why it should not print the message
... because the code is non-deterministic. It's using unsynchronised async calls.
Topic archived. No new replies allowed.