socket accept() keeps looping


 
Thread Tools Search this Thread
Top Forums Programming socket accept() keeps looping
# 1  
Old 01-12-2011
socket accept() keeps looping

I'm using C/ C++ with gcc on Linux. I've a server socket where accept() is called on the socket inside a while() loop. The problem I am facing is that the first call to accept is blocking (i.e., the program waits for the first connection) but as soon as I fork afterwards (so that the child process can handle the communication of the new socket), and come back to the accept() call in the parent, accept() starts returning -1 as if it was a non-blocking call.

Can anyone explain the scenario? What could be the possible reasons for accept() to return -1?
# 2  
Old 01-12-2011
A list of possible reasons is given in the man page for accept. What does perror output?
This User Gave Thanks to pludi For This Post:
# 3  
Old 01-12-2011
Quote:
Originally Posted by pludi
What does perror output?
Thanks! I am new to *nix programming and didn't know the useful perror. It was giving me "Bad file descriptor", which hinted me that something was wrong with the socket I was passing to accept(). And then it dawned upon me that I was closing the socket in the child process. Problem solved.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Looping connect call for a non blocking socket

will there be any unexpected results on looping connect call for a non blocking socket to determine the connection based on error code. I am getting connection unsuccessful intermittently and so wondering whether is the timeout 500 millisec not sufficient or looping connect cause any unexpected. ... (7 Replies)
Discussion started by: satish@123
7 Replies

2. Programming

Looping problems socket programming in C

Sorry if I posted 2 separate questions. I'm currently doing socket programming on my current task. As you can see below in the client side. I've tried to do a loop so I will be able to get prompt for input over and over again. This is the code. do{ printf("Please your name > ");... (10 Replies)
Discussion started by: aLHaNz
10 Replies

3. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

4. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

5. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

6. IP Networking

Port number of socket returned by accept()

Hi, I typed a few tcp/ip client/server examples from a book and it works - sort of - but I noticed something strange. When I run my server I set it to use port 3001 and the client uses the same port to connect to server. They succeed, but the server prints something that doesn't really make much... (0 Replies)
Discussion started by: idelovski
0 Replies

7. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

8. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

9. Programming

Please help! accept function problems in Socket programming

Hi, I have a client-server socket program. It has been working fine for over a year, but recently it started to show strange behavior.:confused: After the server program runs for a while, it will show in the top command saying it is using lots of CPU, MEM. I assume it means the server code is... (1 Reply)
Discussion started by: natxie
1 Replies

10. UNIX for Dummies Questions & Answers

Socket programming : Accept return 0.

I have a server and client code. My server says accept and my client says a connect. After accept when i print inet_ntoa(cin.sin_addr) by running the client on same machine i get 127.0.01 but accept returns a zero. Now when i try to do a recv from the client the recv never waits for a send. It... (7 Replies)
Discussion started by: abc.working
7 Replies
Login or Register to Ask a Question