sockets

Started by
4 comments, last by oneillci 23 years, 1 month ago
I''m new to DirectX programming and I''m doing a breakout clone as a college project. I have to add some network functionality to my game though and I''m adding a two player pong version. I think I''ve got my server up and running. On my server I have the following SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in addr addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr.sin_port = 5001; if (bind(s,(struct sockaddr *)&addr, sizeof(addr))==SOCKET_ERROR){ // error WSACleanup (); // unload WinSock return 0; // quit } WSAAsyncSelect(s, hWnd, SOCKEV, FD_READ | FD_WRITE | FD_ACCEPT | FD_CONNECT | FD_CLOSE); if( listen(s,2) == SOCKET_ERROR ) { // error! unable to listen WSACleanup (); return 0; } SOCKEV is something I have #define ''d so I''ll be notified. On the client I have SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr.sin_port = htons(5001); len = sizeof(addr); if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR) { // error WSACleanup (); // unload WinSock return 0; // quit } WSAAsyncSelect(s, hWnd, SOCKEV, FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE); if( connect(s, (LPSOCKADDR)&addr, len) == SOCKET_ERROR) { WSACleanup (); // unload WinSock return 0; // quit } It is failing here at the connect. Any ideas? Any help would be much appreciated and I''ll post this on the network forum, thanks. Ciaran
Advertisement
I dont think you bind for the client - just connect..

--LordKaT


Resist Windows XP''s Invasive Production Activation Technology!
I tried this already and no joy. The segments of code I posted were in my winmain after I registered the class, created the window and displayed it...don''t think this makes a difference. Any more idea''s anyone?

Ciaran
Have you checked the error with WSAGetLastError()?
When you use WSAAsyncSelect() it automatically puts the socket in non-blocking mode. See followin (from msdn docs)

--begin quote--
With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. In this case, there are three possible scenarios:

Use the select function to determine the completion of the connection request by checking to see if the socket is writeable.
If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that the connect operation is complete (successfully or not).
If the application is using WSAEventSelect to indicate interest in connection events, then the associated event object will be signaled indicating that the connect operation is complete (successfully or not).

--end quote--

Not sure if this is your problem, but hopefully it helps.

Fly-n-boy
Joe
-flynboy
Yeah I think that was it. I used WSAGetLastError() to see if it was WSAEWOULDBLOCK and my compiler didn''t step into it but then I set a breakpoint at my FD_CONNECT and it went into it. All I have to do now is figure out how to send messages!!

Thanks a million,
Ciaran
i have same you problem. but i can connect when dont use Acynconues Socket. Can i connect you? My Email yutuse@hotmail.com

This topic is closed to new replies.

Advertisement