Sign in to follow this  
ARC inc

Problems with Winsock

Recommended Posts

Ok, so I am still working on a few things for my GameServer, I don't get any real Compiler errors but more or less bugs I've been able to notice while it's running -It's not connecting to the internet(As far as I can see) -I can't get my ListenSocket loop to work properly so I can accept more then 1 connection. Here is my code. winsockmain.cpp
/**
This Source Code is not compelte. nor is it ready for working conditions, This source code is to be only 
viewed on codeguru.org/forum/index.php
This code was made by: Andrew "Tivoilos" Kapaldo with DevC++/Code::Blocks IDE
I retrain all rights with this code, any use of this code in anyway without permsion is illegal!
for questions or concerns reguarding my code please email me at
kapaldo2009@gmail.com**/

#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>


int main()
{

#define DEFAULT_PORT "27015"
//Some Winsock2 Functions/Declarations!
struct addrinfo *result = NULL,
                *ptr    = NULL,
                
                hints;
WSADATA wsaData;
WORD wVersion;

SOCKET ListenSocket = INVALID_SOCKET;


int ewsResult;
int EWS;

wVersion = MAKEWORD (2,2);

//Starting up Winsock2
EWS = WSAStartup(wVersion, &wsaData);

if (EWS != 0) 
{
        printf("Could not find the right Winsock DLL for EWS!\n");
}


//Getting the Right Version
if (LOBYTE(wVersion) != 2)
   {
           printf("You do not have the right Winsock Version!\n");
   WSACleanup();
   return 1;
   }
//Now that we have it we can move on!
else
{   
 printf("Winsock Version 2 Found!\n");
 
return 0;
}

//declaring All the Major winsock2 stuff!
ZeroMemory(&hints, sizeof(hints));
hints.ai_family     = AF_INET;
hints.ai_socktype   = SOCK_STREAM;
hints.ai_protocol   = IPPROTO_TCP;
hints.ai_flags      = AI_PASSIVE;

//Resolving IP Address, and port!
ewsResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);

//oops! Looks like we had a error (:[)
if(ewsResult != 0)
{
             printf("Unable to find a proper connection of IP and Port: %d\n", ewsResult);
             WSACleanup();
             return 1;
}
//Got a connection now we can move on
else
{
    printf("Connected succesfully!\n");
}

//Creating a Socket for the Server! (:])
if (ListenSocket == INVALID_SOCKET)
{
                 printf("Error at Sockets: %d\n", WSAGetLastError);
                 WSACleanup();
                 return 1;
}
else 
{
     printf("Socket was made!\n");
     return 0;
}

ewsResult = bind(ListenSocket, 
          result->ai_addr, (int)result->ai_addrlen);
if(ewsResult == SOCKET_ERROR)
{
             printf("There was a problem with binding the socket: %d\n", WSAGetLastError);
             freeaddrinfo(result);
             closesocket(ListenSocket);
             WSACleanup();
             return 1;
}
else
{
    printf("Bind successfully!");
    return 0;
}

for (ListenSocket; SOMAXCONN; != SOCKET_ERROR) //This is supposed to be the Listen Loop but does not work I know that for a fact cuz this is where I get any error
{
if(listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
{
                        printf("Error with listening socket: %d\n");
WSACleanup();
return 1;
}

else
{
    printf("Listening on a Socket!");
    return 0;     
}
}

}
}

Share this post


Link to post
Share on other sites
I know it does not compile I was testing stuff to see if I could get things to work never switched it out.

Anyway I'll probably just rewrite this program.

Share this post


Link to post
Share on other sites
Quote:
Original post by ARC inc
I know it does not compile I was testing stuff to see if I could get things to work never switched it out.

Anyway I'll probably just rewrite this program.


It looks like you never change the value of ListenSocket(you're initializing it to INVALID_SOCKET at its declaration) - so you should be stopping here:

if (ListenSocket == INVALID_SOCKET)
{
printf("Error at Sockets: %d\n", WSAGetLastError);
WSACleanup();
return 1;
}

Share this post


Link to post
Share on other sites
Quote:
I retrain all rights with this code, any use of this code in anyway without permsion is illegal!
for questions or concerns reguarding my code please email me at


Just to avoid legal hassles, do you give permission to posters in this thread to download the code, and perhaps run it in order to find the bugs? Are we allowed to even read it? Can we get this in legally certified written form?

Assuming someone suggests a correction to the code, will you consider that a copyright infringement?

I'm just afraid that if I even look at the code, let alone try to find or fix a problem, I may get sued into poverty.

PS: The legal issues regarding software are fubar enough without random licenses. If you feel the need to put a license on code, I'd strongly suggest you use one of common licenses which have had legal ramifications and usefulness reviewed.

Also, while it seems like a good idea at first, putting personal e-mail into source will hurt you down the line. Aside from spam bots, emails tend to change, so does code ownership, and it results in a mess.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this