VC Programmer

hello everyone,

i am a beginner in vc programming for winsock, and am running into a strange error condition - my program never 'comes back' after making the accept call! 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...

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 Smile

#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_zeroMusic)
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);
}

----------------



Re: Visual C++ General Problem with accept() - winsock programming

VC Programmer

not sure if this is of any help, but I am running visual studio 2003 on a winxp-sp1 machine. any comments to understand / resolve this will be much appreciated

thanks

- vc programmer





Re: Visual C++ General Problem with accept() - winsock programming

VC Programmer

hi,

for some reason, as described above, the call to accept() never returns.

dont know if this extra info will be helpful -

after pressing ctrt+c , i get tyhe following "The thread 'Win32 Thread' (0xf88) has exited with code -1073741510 (0xc000013a)."

any suggestions from anyone does this seems like a winsock issue (after all, when you make an api call, atleast an error should be returned - here, its nothing! )

- vc programmer





Re: Visual C++ General Problem with accept() - winsock programming

VC Programmer

hi all,

since i am the only one who has been posting in this thread, can you guys atleast confirm if the code is working on your systems

dunno, but with this being the msdn forums, and so many well qualified guys, i was hoping for alteast 2 cents... Smile

- vc programmer





Re: Visual C++ General Problem with accept() - winsock programming

VC Programmer

hi all,

since i am the only one posting in this thread, can someone confirm if accept() is happening successfully on their system

i am running windows xp sp1, plus vs.net 2003.

- vc programmer





Re: Visual C++ General Problem with accept() - winsock programming

Baash05

accept it a "blocking call".. It waits for ever

To deal with this i've put the accept in a threaded function.

I time out the threaded function.

Here's how I call it.

AcceptParameter ap;
ap.m_socket = m_Socket;
ap.m_pConnectedSocket = NULL;

HANDLE hThread = CreateThread(NULL, 0, Accept, &ap, 0 ,NULL);
if(WaitForSingleObject(hThread, p_timeout) == WAIT_FAILED)
return NULL;

AcceptParameter is a struct with the calling accepting socket (m_socket) and the returned socket in m_pConnectedSocket

p_timeout is a milisecond interval (int) I typically have it set to 5000

You'll have to instantiate your own threadable Accept function but that shouldn't be too hard.

Hope that's enough






Re: Visual C++ General Problem with accept() - winsock programming

Ðãv? S. Â???????

The Accept() function will only return if you get a successful connection. It is a blocking function meaning any further code after it will halt until it returns. Your problem is probably that your not getting a successful connection.





Re: Visual C++ General Problem with accept() - winsock programming

VC Programmer

thanks a lot ... you are the only one who bothered replying

could you also help with this ->
i am using send & recv to take in data from the client as follows -

while(1){
recv(s,(char *) &inputBuffer[recvdBytes], 1, 0);
num = inputBuffer[recvdBytes];
recvdBytes++;
if(num==13 || num==10) break;
}
inputBuffer[recvdBytes] = '\0';



i want to end the input once the user hits the neter key, how do i do that... thanks