kernel assigned ports

  • Follow


Hi,
I am trying to fork a tcp server process (tcp_server()) function in
code below. This tcp server will listen on a port assigned by the kernel.
Hence I am settin sin_port to 0.

Now in the same funtion, I am forking 10 child processes (child_process)
which will in turn connect with this tcp server.

Now in the code below  first I am filling up the server_addr structure
(which is of the type sockaddr_in). The i am forking  a child process
which will start tcp_server. Then parent process will fork child processes.
These child processes use the same server_addr structure to connect to
parent.

I am having problem with this. The tcp server gets started. But child
proceses cannot connect to that. When I change sin_port to some other port
number (say 5555) then it works. 

I think I am wrong somewhere in making kernel assign the port no. 

I request that experts please comment on my mistake.


#include "myheader.h"
#include "child.c"
#include "server.c"

int main()
{
pid_t server_id;
	/*Assigning server Addresses*/
		server_addr.sin_family=AF_INET;
		server_addr.sin_port =htons(3490);
		server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
		memset(&(server_addr.sin_zero),'\0',8);
	
	
	if((server_id=fork())==0)
	{
		u_int8_t res;
		if((res=tcp_server())!= SUCCESS)
		{
		perror("tcp_server: Error creating TCP server ! bailing out ..");
		exit ETCPSERV;			
		}
		
		
	}
	else
	{
		int count;
		u_int8_t res;	
		for(count=0;count < 10;count++)
		{
			
			if((res =child_process())!=SUCCESS)
			{
			perror("child_process: Error creating CHILD process ! cowardly bailing
			out..");
			}
			}
			
		}			
		
		
kill(server_id,SIGKILL);
return SUCCESS;
}






Regards,

Tejas Kokje
The University of Southern California
0
Reply Tejas 3/7/2004 9:10:27 PM

In article <pan.2004.03.07.21.10.26.867977@usc.edu>,
 Tejas Kokje <kokje@usc.edu> wrote:

> Hi,
> I am trying to fork a tcp server process (tcp_server()) function in
> code below. This tcp server will listen on a port assigned by the kernel.
> Hence I am settin sin_port to 0.

The code you posted sets sin_port to 3490, not 0.

> 
> Now in the same funtion, I am forking 10 child processes (child_process)
> which will in turn connect with this tcp server.
> 
> Now in the code below  first I am filling up the server_addr structure
> (which is of the type sockaddr_in). The i am forking  a child process
> which will start tcp_server. Then parent process will fork child processes.
> These child processes use the same server_addr structure to connect to
> parent.

You need to call socket(), bind(), and getsockname() before forking, so 
that you find out what port was assigned.  Otherwise, how will the 
clients know what port to connect to?

> 
> I am having problem with this. The tcp server gets started. But child
> proceses cannot connect to that. When I change sin_port to some other port
> number (say 5555) then it works. 
> 
> I think I am wrong somewhere in making kernel assign the port no. 
> 
> I request that experts please comment on my mistake.
> 
> 
> #include "myheader.h"
> #include "child.c"
> #include "server.c"
> 
> int main()
> {
> pid_t server_id;
> 	/*Assigning server Addresses*/
> 		server_addr.sin_family=AF_INET;
> 		server_addr.sin_port =htons(3490);
> 		server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
> 		memset(&(server_addr.sin_zero),'\0',8);
> 	
> 	
> 	if((server_id=fork())==0)
> 	{
> 		u_int8_t res;
> 		if((res=tcp_server())!= SUCCESS)
> 		{
> 		perror("tcp_server: Error creating TCP server ! bailing out ..");
> 		exit ETCPSERV;			
> 		}
> 		
> 		
> 	}
> 	else
> 	{
> 		int count;
> 		u_int8_t res;	
> 		for(count=0;count < 10;count++)
> 		{
> 			
> 			if((res =child_process())!=SUCCESS)
> 			{
> 			perror("child_process: Error creating CHILD process ! cowardly bailing
> 			out..");
> 			}
> 			}
> 			
> 		}			
> 		
> 		
> kill(server_id,SIGKILL);
> return SUCCESS;
> }
> 
> 
> 
> 
> 
> 
> Regards,
> 
> Tejas Kokje
> The University of Southern California

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
0
Reply Barry 3/7/2004 10:01:07 PM


1 Replies
153 Views

(page loaded in 0.059 seconds)


Reply: