|
Server Help Community forums for Subgame, ASSS, and bots
|
Author |
Message |
Cheese Wow Cheese is so helpful!
Joined: Mar 18 2007 Posts: 1017 Offline
|
Posted: Thu Sep 04, 2008 3:42 pm Post subject: win32/winsock fun |
|
|
|
|
been throwing around win32 and winsock in c++ recently...
so last night, i threw together a simple client/server app in win32 using winsock.
it works just fine, but there are some things i cant do, and cant find the remedy for.
for instance, when i sever the connection from either end, i am unable to reestablish the connection.
i also havnt found a way to open multiple clients without one 'stealing' the connection from the other. ive seen some examples, but not in the style im writing in...
i have also found no way to append the last entry of a list box, only to have 2 entries.
aka:
bleh
blah
to:
bleh blah
(once bleh is in, cant add to it)
i also cant find a way to add a list-view box, and it doesnt even appear, no indication at all. using:
CreateWindow(WC_LISTVIEW,"test",LVS_REPORT|WS_CHILD|LVS_EDITLABELS,5,5,400,400,hwnd,(HMENU)ID_LISTVIEW,hInst,NULL);
cant add a tab control (WC_TABCONTROL) either, same issue as before.
anyone any good at winsock or win32? _________________ SSC Distension Owner
SSCU Trench Wars Developer |
|
Back to top |
|
|
Snrrrub Novice
Joined: May 29 2008 Posts: 37 Offline
|
Posted: Thu Sep 04, 2008 5:05 pm Post subject: |
|
|
|
|
I think some more details would be helpful.
- Are you using TCP or UDP (i.e. what do you mean by "connection")?
- How are you currently attempting to reestablish a connection?
- What specifically is going wrong? Is a Winsock function returning an error code? Or does the other side just not see an incoming connection?
- What do you mean by "stealing" a connection?
- What's the style that you're writing in that prevents you from using one of the other solutions?
Some code snippets (NOT everything!) with key areas explained would be a good way to narrow down the issue and make it easier for others to comment.
I don't do much Win32 UI programming but you might want to check the return value of CreateWindow and do a GetLastError to see if anything went wrong.
Here's a good link to learn about network programming: http://beej.us/guide/bgnet/output/html/multipage/index.html
-Snrrrub |
|
Back to top |
|
|
awaycheese Guest
Offline
|
Posted: Thu Sep 04, 2008 8:46 pm Post subject: |
|
|
|
|
also forgot to add that i cant get tooltips to appear either.
at this point, i dont know whether its udp or tcp, havnt looked yet.
i havnt been using return codes for errors (too troublesome), gonna check that out.
basically on both ends:
WSADATA wsaData;
SOCKET sListen=INVALID_SOCKET;
SOCKET sClient=INVALID_SOCKET;
SOCKADDR_IN LocalAddr;
SOCKADDR_IN RemoteAddr;
...
WSAStartup(MAKEWORD(1, 1), &wsaData);
sListen=socket(AF_INET, SOCK_STREAM, 0);
LocalAddr.sin_family=PF_INET;
LocalAddr.sin_port=htons(5664);
bind(sListen, (PSOCKADDR)&LocalAddr, AddrLen);
listen(sListen, 10);
WSAAsyncSelect(sListen, hwnd, WM_NET, FD_ACCEPT | FD_CLOSE | FD_READ | FD_WRITE);
...
sClient=accept(sListen, (PSOCKADDR)&RemoteAddr, &AddrLen);
...
recv(sClient, buf, MaxBufLen, 0);
...
strcpy(buf, "bleh");
send(sClient, buf, strlen(buf)+1, 0);
...
shutdown(sClient, SD_BOTH);
closesocket(sClient);
sClient=INVALID_SOCKET;
|
those are really all the important parts...
tried severing the connection with just shutdown() or closesocket() alone, but after either of those, reconnection is impossible. |
|
Back to top |
|
|
cheeseaway Guest
Offline
|
Posted: Thu Sep 04, 2008 9:22 pm Post subject: |
|
|
|
|
read more, apparently im using TCP for now... |
|
Back to top |
|
|
Cheese Wow Cheese is so helpful!
Joined: Mar 18 2007 Posts: 1017 Offline
|
Posted: Fri Sep 05, 2008 12:33 am Post subject: |
|
|
|
|
so i add in debug code to check return values, effectively TRIPLING the number of lines of code in my programs.
on the original connect, i get the "Operation would block" message from connect() (should be fine)
everything works fine after, then i sever the connection from the client.
on attempted client reconnection, i get the "Socket operation on non-socket" message from connect().
after original connect, if i sever the connection from the server, then attempt to reconnect from server, i get "Address already in use" from bind() then "Invalid argument" from listen(). on client connect attempt after server kill i get the "Socket operation on non-socket" message from connect().
client connect
RemoteAddr.sin_family=PF_INET;
RemoteAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
RemoteAddr.sin_port=htons(5664);
ret=connect(sClient, (PSOCKADDR)&RemoteAddr, AddrLen);
|
client disconnect
ret=shutdown(sClient, SD_BOTH);
or
ret=shutdown(sClient, SD_BOTH);
ret=closesocket(sClient);
sClient=INVALID_SOCKET; <--might be called
|
server start
ret=WSAStartup(MAKEWORD(1, 1), &wsaData);
sListen=socket(AF_INET, SOCK_STREAM, 0);
LocalAddr.sin_family=PF_INET;
LocalAddr.sin_port=htons(5664);
ret=bind(sListen, (PSOCKADDR)&LocalAddr, AddrLen);
(sListen, 10);
ret=WSAAsyncSelect(sListen, hwnd, WM_NET, FD_ACCEPT | FD_CLOSE | FD_READ | FD_WRITE);
|
server stop
ret=shutdown(sClient, SD_BOTH);
or
ret=shutdown(sClient, SD_BOTH);
ret=closesocket(sClient);
sClient=INVALID_SOCKET; <--might be called
|
i think im closing it down all the way, and am missing something...
"Invalid argument" is probably coming when WSAAsyncSelect() fails because socket isnt open =X
all i want to do is reestablish a connection after it was broken
***update***
got the tab control working very nicely =)
still have no idea how to put things on seperate tabs...
list view control is still nowhere to be found =(
Last edited by Cheese on Fri Sep 05, 2008 2:37 am, edited 1 time in total |
|
Back to top |
|
|
Snrrrub Novice
Joined: May 29 2008 Posts: 37 Offline
|
Posted: Fri Sep 05, 2008 1:34 am Post subject: |
|
|
|
|
When you do a closesocket, you're effectively throwing away the socket handle that you have - you can't perform any more operations on it.
On the client side, you should call socket() to give you a new socket before making a connection. Every new connection needs a new socket.
On the server side, you're getting those errors because your listening socket has already done bind() on the given IP/port combination and you're trying to do it again. When you're "shutting down" the server, you're only closing the connection to the client but your other socket is still listening for incoming connections so you don't have to go through that entire setup process for the next client. Everything up to (and including) the call to listen() is just setting up the listener. You only need to do that once.
Honestly, I think you should read Beej's guide (the link in my previous post) - it's probably the best introduction to sockets you'll ever come across. You'll save a lot of time troubleshooting these problems by getting a solid understanding of what's going on. That's the best advice I can offer you for these problems.
-Snrrrub |
|
Back to top |
|
|
Cheese Wow Cheese is so helpful!
Joined: Mar 18 2007 Posts: 1017 Offline
|
Posted: Fri Sep 05, 2008 2:47 am Post subject: |
|
|
|
|
i did read it, its an excellent reference, but unfortunately theres no section about resurrecting the dead
but i did spend most of my time in 8.1-8.21
also, why does the client hop to FD_CONNECT when the server hasnt been started yet after trying to connect?
and i tried putting socket() in the connect attempt instead of where it is now in WM_CREATE, but it doesnt work, and causes wierd behavior in the server...
and i cant debug socket(), because the return code goes to the handle...
-edit-
i guess i was right originally, but i hadnt brought WSAAsyncSelect() with it, so the 'wierd behavior' was it -almost- working.
the client now works 100% correctly.
server:
renamed 'stop' button to 'sever connection'
removed 'start' button, moved code to init.
so i guess all my winsock problems are solved! ^_^
sometimes, i guess all u need is a kick in the right direction to achieve success... =)
but my win32 problems still remain... |
|
Back to top |
|
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
Software by php BB © php BB Group Server Load: 605 page(s) served in previous 5 minutes.
|