LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 05-19-2010, 09:16 AM   #1
ninja123
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Rep: Reputation: 0
Socket programming: sending and receiving msgs back and forth


Hi all,

I have a server which is used by number of client Apps. Now when a client initiates a sendto request to a server it has to respond(after recvfrom) by sending a message using sendto back to client and this should be done back and forth.

What I dont understand is, does the client have to open a new port to receive packets from client? If yes, then how will server know to what port of the client(assuming server ports are well-known to clients)?

I had given up on sending messages back and forth using UDP and switched to TCP. I could send a read and write message back and forth. But the trouble comes when I have more than one client wanting to talk to server using SOCK_STREAM. The first one gets through but the second client seems to get blocked forever(but I got no error upon socket creation or upon connect with the server on the same port as the previous client)..

Can somebody throw some light??

Thanks for the help.
 
Old 05-19-2010, 09:30 AM   #2
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
Show us the code!

If you're doing something along the lines of:

Code:
client_sock = accept(listen_sock, ...);
recvfrom(client_sock, ...);
sendto(client_sock, ...);
.. you're doing it wrong.
 
Old 05-20-2010, 07:16 AM   #3
ninja123
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Here is the client code:

Code:
//this is from header file
typedef struct{
	unsigned int permitFlag;
	char rmsg [32];
}ctrlMsg;

//client code

             int dataRead;
                ctrlMsg rmessage;
                int fd_client;
                struct sockaddr_in serv_addr;
                char buf[32];
                int check_ret;

		buf[0] = '\0';

		fd_client = socket(AF_INET, SOCK_STREAM, 0);
		if(fd_client < 0) {
			printf("error creating socket. errno is %d\n", errno);
			exit(1);
		}

		serv_addr.sin_family = AF_INET;
		serv_addr.sin_port = htons(PORT);
		serv_addr.sin_addr.s_addr = inet_addr(SERVER_IP);

		bzero(&(serv_addr.sin_zero), 8);

		check_ret = connect(fd_client,
				(struct sockaddr *) &serv_addr, ADDR_LEN);
		if(check_ret < 0) {
			printf("Connect error. Errno is %d\n", errno);
			exit(1);
		}

		while(1) {
			strcpy(buf, "this is client");
			buf[strlen(buf)] = '\0';
			check_ret = write(fd_client, buf, 32);
			if(check_ret < 0) {
				printf("errno is %d\n", errno);
			}
			bzero(buf, 32);
			check_ret = read(fd_client, &rmessage, sizeof(rmessage));
			if(check_ret < 0) {
				printf("errno is %d\n", errno);
			}
			if (strlen (rmessage.rmsg) > 0) {

				dataRead = atoi(rmessage.rmsg);
				printf ("Msg from server: \"%d\".\n", dataRead);
				putring(dataRead);
			}
			delay(1000);
		}
 
Old 05-20-2010, 07:27 AM   #4
ninja123
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
And here is the server code(UDP)

Code:
          
                char buf[32];
		ctrlMsg smessage;
		smessage.permitFlag = 1;
		uint16_t port_no = 2024;
		int v, sock, addr_len;
		struct sockaddr_in addr;
		if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
		exit(EXIT_FAILURE);
		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_addr.s_addr = INADDR_ANY;
		addr.sin_port = htons(port_no);

		if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
		exit(EXIT_FAILURE);
		printf("server : bind over\n");

		for (;;) {
		         /* do recvfrom 
                        if ((v = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &addr_len)) > 0)
			printf ("Message got from ODR: %s\n", buf);
			bzero(buf,32);

			strcpy(smessage.rmsg, “hello”);
			if (sendto(sock, &smessage, sizeof(smessage), 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
			perror("sending datagram message");
			memset(&smessage, '0', sizeof(smessage));
		}
 
Old 05-20-2010, 07:30 AM   #5
ninja123
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
I was actually reading about select(). Is select to allow multiple sockets between server and client? But then, to have it working, should we know the number of clients talking to a server? That somehow sounds so wrong
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
qmail not sending or receiving zibusiso Linux - Newbie 1 10-28-2009 05:20 AM
sending request but not receiving gaurav pant Linux - Networking 2 11-05-2008 07:17 AM
Help...sending email but not receiving fiona333 Linux - Newbie 9 09-07-2007 09:28 AM
sending msgs thru command line citrus Linux - Software 3 04-13-2004 03:39 AM
Receiving all data from socket Ohmu Programming 6 03-08-2004 02:58 PM


All times are GMT -5. The time now is 12:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration