[WinSock] Connect to IP?

I've been trying to do this app today, but without much luck, in this app you can basically host or connect to someone else (p2p chat) but unfortunatelly I can connect to any IP I want, and it don't even have to be an IP, I can just write random things to IP Address edit box and it will connect me... (I won't get any error or anything, app will simply continue) O_o i'm pretty new to winsock, can someone help me? hosting works fine I guess

http://pastebin.com/tdVyCpX8
Last edited on
someone?
What is your problem?

I'd really suggest to use an up to date GUI framework like wxWidget or QT for things like that
(I like to use plain win32 API)

I have no idea how to use WSAAsyncGetHostByAddr or however is that function called, I just want to connect to the IP provided when connecting, I added message box to everything and well error is in WSAGETASYNCERROR(lParam) in this part of the code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
if(idTask == (HWND)wParam)
			{
				hostent *host = (struct hostent *) buf;
				sockaddr *addr = NULL;
				sockaddr_in addr_ip4;
				size_t addrLen = 0;
				if(WSAGETASYNCERROR(lParam) != 0)
				{
					MessageBox(hWnd, "WSAGETASYNCERROR Error!", NULL, MB_ICONERROR | MB_OK);
					return 0;
				}
				/*addr_ip4.sin_family = host->h_addrtype;
				addr_ip4.sin_port = htons(port);
				memcpy(&(addr_ip4.sin_addr), host->h_addr_list[0], host->h_length);
				addr = (struct sockaddr *)&addr_ip4;
				addrLen = sizeof(addr_ip4);*/
				addr_ip4.sin_family = AF_INET;
				addr_ip4.sin_port = htons(port);
				addr_ip4.sin_addr.S_un.S_addr = inet_addr(ip_addr);
				if ((sock = socket(host->h_addrtype, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET){
					MessageBox(hWnd, "Can't create socket!", NULL, MB_ICONERROR | MB_OK);
					return 0;
				}
				if (connect(sock, addr, addrLen) == SOCKET_ERROR){
					MessageBox(hWnd, "Can't connect to the server!", NULL, MB_ICONERROR | MB_OK);
					return 0;
				}
				if (WSAAsyncSelect(sock, MainhWnd, WM_SOCKET, FD_READ) == SOCKET_ERROR){
					MessageBox(hWnd, "Can't read from the socket!", NULL, MB_ICONERROR | MB_OK);
					return 0;
				}
			}
			break;
		}


but I guess somethings wrong with the WSAAsyncGetHostByAddr, can someone provide me example on how to use it? I didnt find anything, not even on msdn, also am I doing this right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sockaddr_in addr_ip4;
				size_t addrLen = 0;
				if(WSAGETASYNCERROR(lParam) != 0)
				{
					MessageBox(hWnd, "WSAGETASYNCERROR Error!", NULL, MB_ICONERROR | MB_OK);
					return 0;
				}
				/*addr_ip4.sin_family = host->h_addrtype;
				addr_ip4.sin_port = htons(port);
				memcpy(&(addr_ip4.sin_addr), host->h_addr_list[0], host->h_length);
				addr = (struct sockaddr *)&addr_ip4;
				addrLen = sizeof(addr_ip4);*/
				addr_ip4.sin_family = AF_INET;
				addr_ip4.sin_port = htons(port);
				addr_ip4.sin_addr.S_un.S_addr = inet_addr(ip_addr);
				if ((sock = socket(host->h_addrtype, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET){
					MessageBox(hWnd, "Can't create socket!", NULL, MB_ICONERROR | MB_OK);
					return 0;
				}
				if (connect(sock, addr, addrLen) == SOCKET_ERROR){
					MessageBox(hWnd, "Can't connect to the server!", NULL, MB_ICONERROR | MB_OK);
					return 0;
				}
				if (WSAAsyncSelect(sock, MainhWnd, WM_SOCKET, FD_READ) == SOCKET_ERROR){
					MessageBox(hWnd, "Can't read from the socket!", NULL, MB_ICONERROR | MB_OK);
					return 0;
				}


or how correctly should I connect to the IP with client? I've been following book, but no success
bump, also this: http://cplusplus.com/forum/beginner/80842/ (don't want to bump 2 topics at same time)
Last edited on
Not many of us really understand the Winsock calls (those WSA... calls).

Why don't you just stick to what the sockets library supports to get your stuff working, before diving off into the uncharted territory of Winsock specific asynchronous calls.
WSA is basic method for async windows app... I don't feel like using blocking thingy with my APP...
There are some examples posted earlier this year in the Windows forum. You need to tell it what you're interested (read/write/select), so you're doing the wrong thing.

Don't go off thinking that that is the best way to use sockets on Windows because it isn't.
the point of the async result is that an error is not necessarily an error. The meaning may also that the command is not completed.
uhm, so what should I do? I'm doing simple chat p2p app, there's whole source code in the main post
for WSAAsyncGetHostByAddr read this:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms741519%28v=vs.85%29.aspx

they recommend getnameinfo

if you use async function you have the following problem:

Msdn wrote:
The return value specifies whether or not the asynchronous operation was successfully initiated. It does not imply success or failure of the operation itself.

in other words: if you have an error you need to check out what error it is in order to decide whether it's a real error or not
Topic archived. No new replies allowed.