The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Trouble with tr Mike@Work SUN Solaris 8 08-10-2006 04:04 PM
rc=127 can't fork BG_JrAdmin Shell Programming and Scripting 1 08-01-2006 06:30 PM
trouble awk UNIX Desktop for Dummies Questions & Answers 1 11-22-2002 10:51 AM
fork() and dup() AkumaTay High Level Programming 1 11-13-2002 09:31 AM
Fork Deepali UNIX for Dummies Questions & Answers 5 08-26-2001 09:14 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-23-2006
EltonSky EltonSky is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 11
Trouble with using fork()

Hello guys,

I m working on a chat program with C. I finished the iterative server and it's working well. But once I add fork(), I met problems. I cannot use close or exit, otherwise I cannot accept new connection in next loop. here is my code:



Code:
.
.
.
Listen(simpleSocket, 5);

signal(SIGCHLD, sigchld_handler);

/*****************************************************************/
while (1)
{
int tmpSock = 0;
int pid;
char buf[200] = "";
sendPack sp; /*struct traveling btw 3 programs*/
struct sockaddr_in clientName = { 0 };
int clientNameLength = sizeof(clientName);
int valUsr = 0, i = 0, status = 0;

/* wait here */
/*************************************************************/
simpleChildSocket = Accept(simpleSocket,
(struct sockaddr *)&clientName, &clientNameLength);
/*************************************************************/

if((pid = fork()) == 0)
{
/*if I close listen socket here, parent process cannot accept connection next time*/
close(simpleSocket);

returnStatus = Read(simpleChildSocket, &sp,
sizeof(sp));

if(sp.flag == 1)
{
/* printf("here we go: %s\n", sp.usrName); */
login(sp.usrName, simpleChildSocket, usrHead);

}
else if(sp.flag == 2)
{
if((tmpSock = judgeUser(usrHead, sp.usrName)))
{
status = 1;
Write(simpleChildSocket, &status, sizeof(int));
printf("Pass judgeUser.\n");
returnStatus = read(simpleChildSocket, &sp,
sizeof(sp));

if ( returnStatus > 0 ) {
printf("%d: %s", returnStatus, sp.usrName);
} else {
fprintf(stderr, "Return Status = %d \n", returnStatus);
}
while(sp.func != 4)
{
/*judge val of returnStatus inside func.*/
if(returnStatus > 0)
{
printf("Switch start..\n");
switch(sp.func)
{
case 1:
printf("Single send server.\n");
relaySingle(sp, usrHead, simpleChildSocket);
break;
case 2:
relayGroup(sp, usrHead, simpleChildSocket);
break;
case 3:
queryUsr(usrHead, simpleChildSocket);
break;
default :
break;
}

}
memset(&sp, '\0', sizeof(sp));
returnStatus = Read(simpleChildSocket, &sp,
sizeof(sp));
}
/*Delete quitting usrName.*/
sendQuit(usrHead, sp.usrName);
Write(tmpSock, &sp, sizeof(sp));
}
else
{
printf("You have not logged in yet.\n");
Write(simpleChildSocket, &status, sizeof(int));
}
}

/*If I use exit(0) or close child socket here, parent process cannot accept connection next time.*/
close(simpleChildSocket);
exit(0);
}

}

close(simpleSocket);
return 0;

}


Diring help~~

Cheers,

Elton
  #2 (permalink)  
Old 08-23-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
could you please check once again that in the forking part
(pid_t = fork () ) == 0
/* child */
pid_t > 0
you dont call exit by mistake...

i have not gone through completely ...

if your accept is failing what is the errno?
  #3 (permalink)  
Old 08-23-2006
EltonSky EltonSky is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 11
Thanku matrixmadhan,

It can accept connection before using fork(), but cause error at 2rd time. the error msg is :

In function Accept() :
accept() : returned -1, errno=EINTR (Interrupted system call)
parameters : s = 3
addr = ffbff4b8
addrlen = ffbff4b4
(*addrlen = 16 before call, 16 after call)
From the man page:
accept() will fail if:
EINTR The accept attempt was interrupted by
the delivery of a signal.



Cheers,

Elton
  #4 (permalink)  
Old 08-23-2006
Hitori's Avatar
Hitori Hitori is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2006
Posts: 360
For simple stream server stub look at http://beej.us/guide/bgnet/output/ht...l#simpleserver
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0