WinSock 2 Simple Client Program not Working

Started by
4 comments, last by DrCoolSanta 16 years, 8 months ago
With a little help from MSDN tutorial to create a WinSock application, I am trying to create a simple client, I am just doing this for a little exercise for myself, plus I want to learn some network programming (I can't get SDL_net to work.) It is a console application. When I run it, the black cmd.exe window comes with a windows error message, those with the options Send and Don't Send. Here is the code: #include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include <winsock2.h> #include <ws2tcpip.h> int main(int argc, char* argv[]) { WSADATA wsaData; int iResult; iResult=WSAStartup(MAKEWORD(2, 2), &wsaData); if(iResult!=0) { printf("Cannot start WinSock\n"); return 1; } struct addrinfo *result=NULL, *ptr=NULL, hints; ZeroMemory(&hints, sizeof(hints)); hints.ai_family=AF_UNSPEC; hints.ai_socktype=SOCK_STREAM; hints.ai_protocol=IPPROTO_TCP; iResult=getaddrinfo(argv[1], argv[2], &hints, &result); if(iResult!=0) { printf("Cannot resolve host\n"); WSACleanup(); return 1; } SOCKET ConnectSocket=INVALID_SOCKET; ConnectSocket=socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); if(ConnectSocket==INVALID_SOCKET) { printf("Error in the socket\n"); freeaddrinfo(result); WSACleanup(); return 1; } iResult=connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); if(iResult==SOCKET_ERROR) return 4; { printf("Error in the socket\n"); closesocket(ConnectSocket); ConnectSocket=INVALID_SOCKET; return 1; } closesocket(ConnectSocket); WSACleanup(); return 0; } Well I guess the code is right, and maybe my parameters for my compiler are wrong. I am using Dev-C++, and i used the button to add the library to the linker, here is what I can see in the linker field. ../../Dev-Cpp/lib/libws2_32.a ../../Dev-Cpp/lib/libwsock32.a Is there anything I am missing? Or is it something too obvious I didn't notice? Please tell me the possibilities or correct the code. PS: I havn't implimented Send and Recieve functions yet, and i would also like to know that what all DLL files might be used since I'll prefer to supply them with the program.
Advertisement
struct addrinfo *result=NULL, *ptr=NULL, hints;.....ConnectSocket=socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);


ptr == NULL

The rest of the initialization is fishy as well.

hints.ai_family=AF_UNSPEC;


This is usually AF_INET, although it probably doesn't need to be.
That is something I didn't write myself so I guess it probably wouldn't be the case, I ripped it off from MSDN, so you know, nothing could create such a fatal error.

I'll try though, changing these values, and edit/post message to tell you what happened.

I would also like to know if there are any tutorials which can help me use winsock from console applications and not that window.h stuff.

As of now I did try to download VC++, the free edition, but it says it needs to download 94MB which I can't since I am running out of the amount of download I can do this month. I have a feeling that the compiler, ie Dev-C++ might be the thing causing problems.

EDIT: Nothing that I could change much, my program still doesn't work.
EDIT: Szome corrections of stupid things fixed it, i am sorry for being a little n00bish and not seeing the code earlier. The things were like some errors in If statements and stuff like that fixed it. Thanks for the help anyway, since now I'll use INET insted of UNSPEC

[Edited by - DrCoolSanta on July 26, 2007 8:08:52 AM]
I'll assume this is the example you're referring to.

What happens if you copy that verbatim and compile it?

It's not an IDE problem, and given that this is the most basic winsock, it shouldn't be a compiler problem either.

When it comes to tutorials, there are small, but important differences between various platforms. And what exactly they are, given that winsock on dev-C++ (gcc), I don't really know which APIs get linked and how.
A sockaddr_in struct is more helpful to me when working through connecting to a server, I usually end up with code like this :

//WSAStartup blah blah//socket structure propertiesstruct sockaddr_in SockAddr;SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //connect to thyselfSockAddr.sin_port = htons(80); //which portSockAddr.sin_family = AF_INET;memset(SockAddr.sin_zero, 0, 8);SOCKET mysocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);connect(mysocket, (sockaddr*)&SockAddr, sizeof(SockAddr));//to quit the socket you shut it downshutdown(mysocket, 2);closesocket(mysocket);WSACleanup();


[Edited by - TheyDontCallMeMatt on July 26, 2007 10:21:53 AM]
That's my opinion.
Thank you TheyDontCallMeMatt, I'll probably use that when I am doing something more serious, right now I'll stick to addrinfo, because MSDN doesn't explaing much, how it works.

Antheus, as I said earlier, I have fixed it, don't worry, it was just some silly mistakes. And actually that wasn't the example look for the Basic Code and stuff.

This topic is closed to new replies.

Advertisement