![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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? |
|
||||
|
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 |
|
|||||
|
For simple stream server stub look at http://beej.us/guide/bgnet/output/ht...l#simpleserver
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|