Click here to Skip to main content
15,887,340 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
Hi,

I have developed a server and client code using tcp/ip protocol, which enables the client to send data to server and the same data is received from server to client.

I would like to know how to keep the client open all the time, same like as server. I should not keep executing the client every time.

I am giving the code for further reference

Server code:
C#
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
void str_echo(int s)
{
	FILE *fp,*fp1;
	char buf[2000];
	//receiving data from client
	recv(s,buf,2000,0);
	puts("Message from Client...");
	fputs(buf,stdout);
	send(s,buf,2000,0);
	}
int main()
{
 	int ls,cs,len;
 	struct sockaddr_in serv,cli;
 	pid_t pid;
 	puts("I am Server...");
	//creating socket
	ls=socket(AF_INET,SOCK_STREAM,0);
 	puts("Socket Created Successfully...");
	//socket address structure
 	serv.sin_family=AF_INET;
 	serv.sin_addr.s_addr=INADDR_ANY;
 	serv.sin_port=htons(6777);
 	bind(ls,(struct sockaddr*)&serv,sizeof(serv));
 	puts("Binding Done...");
 	listen(ls,3);
 	puts("Listening for Client...");
 	for(; ;)
 	{
		len=sizeof(cli);
		//accepting client connection
		cs=accept(ls,(struct sockaddr*)&cli,&len);
		puts("\nConnected to Client...");
		//creating child process
		if((pid=vfork()) == 0)
		{
			puts("Child process created...");
			close(ls);
			str_echo(cs);
			close(cs);
			exit(0);
		}
	close(cs);
 	}
 	return 0;
}



Client Code:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
void str_echo(int s)
{
	char buf[2000],buf1[2000];
	puts("Enter the Message...");
	fgets(buf,2000,stdin);
	send(s,buf,2000,0); //sending data to server
	//receiving data from server
	recv(s,buf1,2000,0);
	puts("Message from Server...");
	//fputs(buf1,stdout);
	puts(buf1);
}
int main()
{
	int ls;
 	struct sockaddr_in cli;
 	puts("I am Client...");
	/*creating socket*/
 	ls=socket(AF_INET,SOCK_STREAM,0);
 	puts("Socket Created Successfully...");
	/*socket address structure*/
 	cli.sin_family=AF_INET;
 	cli.sin_addr.s_addr=inet_addr("127.0.0.1");
 	cli.sin_port=htons(6777);
	/*connecting to server*/
 	connect(ls,(struct sockaddr*)&cli,sizeof(cli));
 	puts("Connected with Server...");  
	str_echo(ls);
 	close(ls);
 	return 0;
}
Posted
Updated 20-Dec-15 21:50pm
v4
Comments
Richard MacCutchan 21-Dec-15 4:08am    
You have edited your question, but have not explained what problem exists now.
venkat28vk 21-Dec-15 4:37am    
I want to send data from client to server and the server has to reply back the same data. This should keep on iterating until i am terminating the process. How to achieve this in the above code

1 solution

surely that depends on YOU - what you you want/need to achieve in the client ? (we dont know your requirements because you havnt stated any)

instead of
str_echo(ls);
you could do

for (; ; )
{
    str_echo(ls);
    sleep(60 * 1000);   // for example to sleep 1 minute between loops 
}


Im not saying its a good idea, just a possible one (I recommend NOT running this as an example, you'll have to kill the process by its PID if you do)
 
Share this answer
 
Comments
venkat28vk 19-Dec-15 5:41am    
I like to send and receive data from client to server, and the server has to receive the data and send it back to client and moreover after this the client and the server has to be in open state(ready to transfer the data again)...this cycle should repeat. This is the scenario i want to achieve, how can i achieve it from my above code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900