accept() never returns, just hangs after calling it
- From: vc-programmer <vc-programmer@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 27 Apr 2007 08:56:01 -0700
hi all,
i originally posted this at -
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1526495&SiteID=1
and have pasted the same as under. Briefly, my systems is running winxp sp1
+ vs.net 2003. The details are - =
i am running into a strange error condition - my program never 'comes back'
after making the accept call! (i.e. while debugging, when the line with the
accept() code is executed, the control never comes back into the program). On
debugging, the only error i get is that the lpVendorInfo in wsadata is a bad
ptr (0xcccccccc). i am not sure whether this is the error or something else...
accept should atleast give me an error, but even that is not happening...
any help will be much appreciated, thanks for taking out time to read this
issue....
the code is -
----------------
#define WIN32_LEAN_AND_MEAN
#include<winsock2.h> // For socket()
#include<stdio.h> // For print functions
#include<stdlib.h> // For memory alloc and de-alloc
#include<string.h> // String
#define RCVBUFSIZE 32 // Size of input buffer
#define PORT 33333 // Listen at this port
#define MAXPENDING 3 // Maximum pending connection requests
void main(){
SOCKET listeningSocket, clientSocket; // Socket descriptor
WORD wVersionRequested; // Winsock version string
WSADATA wsadata; // Contains info about the
windows socket implementation. Used for winsock setup communication
struct sockaddr_in echoServerAddr; // SOCKADDR structure for server
struct sockaddr_in echoClientAddr; // SOCKADDR structure for server
// The in_addr structure represents a host by its internet address
unsigned int clientDSlength; // Length of client address data
structure
printf("Starting echoserver\n");
wVersionRequested = MAKEWORD(2,2); // Request for
version 2.0
if (WSAStartup(wVersionRequested,&wsadata) != 0) // Initialize
winsock (WS2_32.dll) & retrieve details of the winsock implementation
{
fprintf(stderr,"Error @ WSAStartup\n");
exit(1);
}
// Create a stream socket using TCP
if( (listeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0 ) {
fprintf(stderr, "socket() failed, error # %d\n",WSAGetLastError());
WSACleanup();
exit(1);
}
// Bind to the local address
// int bind( SOCKET s, const struct sockaddr* name, int namelen);
// sockaddr structure -> sockaddr_in { short sin_family, u_short
sin_port, struct in_addr sin_addr, char sin_zero)
memset(&echoServerAddr,0,sizeof(echoServerAddr));
echoServerAddr.sin_family = AF_INET; // Address family
echoServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);// Any(all?)
incoming interface(s)
echoServerAddr.sin_port = PORT; // Port
if ( bind(listeningSocket, (struct sockaddr*) &echoServerAddr,
sizeof(echoServerAddr)) != 0){
fprintf(stderr,"Bind failed\n");
fprintf(stderr,"Error - %d\n",WSAGetLastError());
WSACleanup();
exit(1);
}
// Make the socket listen for incoming connections
if ( listen(listeningSocket, MAXPENDING) != 0) {
fprintf(stderr,"Listen failed\n");
fprintf(stderr,"Error - %d\n",WSAGetLastError());
WSACleanup();
exit(1);
}
memset(&echoClientAddr,0,sizeof(echoClientAddr));
clientDSlength = sizeof(echoClientAddr);
fprintf(stdout,"Entering while loop to accept connections\n");
// Do something with the data coming in at PORT
while(1) {
// Accept the connection -> SOCKET accept(SOCKET s,struct sockaddr*
addr,int* addrlen)
fprintf(stdout,"In while loop\n");
// if( accept(listeningSocket, (struct sockaddr*)
&echoClientAddr, sizeof(echoClientAddr)) != 0 ) {
// clientSocket = accept(listeningSocket, 0, 0);
clientSocket = accept(listeningSocket, (struct sockaddr*)
&echoClientAddr, &clientDSlength);
fprintf(stdout,"Client socket = %d\n",clientSocket);
//fprintf(stderr,"Error - %d\n",WSAGetLastError());
if( clientSocket != 0 ) {
fprintf(stderr,"Socket is invalid\n");
fprintf(stderr,"Error - %d\n",WSAGetLastError());
WSACleanup();
break;
}
// Client connected, print it out
fprintf(stdout,"Connected to %s",echoClientAddr.sin_addr);
}
printf("Listening on port %d\n",PORT);
WSACleanup(); // Delete later
exit(0);
}
----------------
- vp-programmer
.
- Follow-Ups:
- Re: accept() never returns, just hangs after calling it
- From: Michael K. O'Neill
- Re: accept() never returns, just hangs after calling it
- From: Skywing [MVP]
- Re: accept() never returns, just hangs after calling it
- Prev by Date: Re: CAsyncSocket OnReceive() never called
- Next by Date: accept() never returns, just hangs after calling it
- Previous by thread: socket communication: socket doesn't connect
- Next by thread: Re: accept() never returns, just hangs after calling it
- Index(es):
Relevant Pages
|
|