Welcome to Dream.In.Code
Become an Expert!

Join 149,615 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,850 people online right now. Registration is fast and FREE... Join Now!




networking with winsock for an mmo

 
Reply to this topicStart new topic

networking with winsock for an mmo, and yes I have lookd at beej's guide to networking

flosy
1 Oct, 2007 - 10:23 AM
Post #1

New D.I.C Head
*

Joined: 15 Jul, 2007
Posts: 15


My Contributions
Ok I use dev-c++ I am making an MMO rpg and need a networking libery for dev-c++ so I was told to use winsock. But all the examples that I compile dont work. I have done several tutorials on it and none of them will work. I aded the lib files I was told to (lib/libwsock32.a and lib/libws2_32.a) but it still wont work this is the code.

CODE

#include <iostream>

#define WIN32_MEAN_AND_LEAN
#include <winsock2.h>
#include <windows.h>

using namespace std;

class HRException
{
public:
    HRException() :
         m_pMessage("") {}
    virtual ~HRException() {}
    HRException(const char *pMessage) :
         m_pMessage(pMessage) {}
    const char * what() { return m_pMessage; }
private:
    const char *m_pMessage;
};

const int  REQ_WINSOCK_VER   = 2;    // Minimum winsock version required
const char DEF_SERVER_NAME[] = "www.google.com";
const int  SERVER_PORT       = 80;    
const int  TEMP_BUFFER_SIZE  = 128;

const char HEAD_REQUEST_PART1[] =
{
    "HEAD / HTTP/1.1\r\n"             // Get root index from server
    "Host: "                        // Specify host name used
};

const char HEAD_REQUEST_PART2[] =
{
    "\r\n"                            // End hostname header from part1
    "User-agent: HeadReqSample\r\n" // Specify user agent
    "Connection: close\r\n"         // Close connection after response
    "\r\n"                            // Empty line indicating end of request
};

// IP number typedef for IPv4
typedef unsigned long IPNumber;

IPNumber FindHostIP(const char *pServerName)
{
    HOSTENT *pHostent;

    // Get hostent structure for hostname:
    if (!(pHostent = gethostbyname(pServerName)))
            throw HRException("could not resolve hostname.");
    
    // Extract primary IP address from hostent structure:
    if (pHostent->h_addr_list && pHostent->h_addr_list[0])
        return *reinterpret_cast<IPNumber*>(pHostent->h_addr_list[0]);
    
    return 0;
}

void FillSockAddr(sockaddr_in *pSockAddr, const char *pServerName, int portNumber)
{
    // Set family, port and find IP
    pSockAddr->sin_family = AF_INET;
    pSockAddr->sin_port = htons(portNumber);
    pSockAddr->sin_addr.S_un.S_addr = FindHostIP(pServerName);
}

bool RequestHeaders(const char *pServername)
{
    SOCKET         hSocket = INVALID_SOCKET;
    char        tempBuffer[TEMP_BUFFER_SIZE];
    sockaddr_in    sockAddr = {0};
    bool        bSuccess = true;
    
    try
    {
        // Lookup hostname and fill sockaddr_in structure:
        cout << "Looking up hostname " << pServername << "... ";
        FillSockAddr(&sockAddr, pServername, SERVER_PORT);
        cout << "found.\n";
    
        // Create socket
        cout << "Creating socket... ";
        if ((hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
            throw HRException("could not create socket.");
        cout << "created.\n";
        
        // Connect to server
        cout << "Attempting to connect to " << inet_ntoa(sockAddr.sin_addr)
             << ":" << SERVER_PORT <<  "... ";
        if (connect(hSocket, reinterpret_cast<sockaddr*>(&sockAddr), sizeof(sockAddr))!=0)
            throw HRException("could not connect.");
        cout << "connected.\n";
        
        cout << "Sending request... ";

        // send request part 1
        if (send(hSocket, HEAD_REQUEST_PART1, sizeof(HEAD_REQUEST_PART1)-1, 0)==SOCKET_ERROR)
            throw HRException("failed to send data.");

        // send hostname
        if (send(hSocket, pServername, lstrlen(pServername), 0)==SOCKET_ERROR)
            throw HRException("failed to send data.");

        // send request part 2
        if (send(hSocket, HEAD_REQUEST_PART2, sizeof(HEAD_REQUEST_PART2)-1, 0)==SOCKET_ERROR)
            throw HRException("failed to send data.");

        cout << "request sent.\n";
        
        cout <<    "Dumping received data...\n\n";
        // Loop to print all data
        while(true)
        {
            int retval;
            retval = recv(hSocket, tempBuffer, sizeof(tempBuffer)-1, 0);
            if (retval==0)
            {
                break; // Connection has been closed
            }
            else if (retval==SOCKET_ERROR)
            {
                throw HRException("socket error while receiving.");
            }
            else
            {
                // retval is number of bytes read
                // Terminate buffer with zero and print as string
                tempBuffer[retval] = 0;
                cout << tempBuffer;
            }
        }
    }
    catch(HRException e)
    {
        cerr << "\nError: " << e.what() << endl;
        bSuccess = false;
    }

    if (hSocket!=INVALID_SOCKET)
    {
        closesocket(hSocket);
    }
    return bSuccess;
}    

int main(int argc, char* argv[])
{
    int iRet = 1;
    WSADATA wsaData;

    cout << "Initializing winsock... ";

    if (WSAStartup(MAKEWORD(REQ_WINSOCK_VER,0), &wsaData)==0)
    {
        // Check if major version is at least REQ_WINSOCK_VER
        if (LOBYTE(wsaData.wVersion) >= REQ_WINSOCK_VER)
        {
            cout << "initialized.\n";
            
            // Set default hostname:
            const char *pHostname = DEF_SERVER_NAME;
            
            // Set custom hostname if given on the commandline:
            if (argc > 1)
                pHostname = argv[1];
                
            iRet = !RequestHeaders(pHostname);
        }
        else
        {
            cerr << "required version not supported!";
        }

        cout << "Cleaning up winsock... ";

        // Cleanup winsock
        if (WSACleanup()!=0)
        {
            cerr << "cleanup failed!\n";
            iRet = 1;
        }  
        cout << "done.\n";
    }
    else
    {
        cerr << "startup failed!\n";
    }
    return iRet;
}


and this is the error
QUOTE

[linker error] undifined reference to winmain@16
id returned 1 exit status
[build error] [projekt.exe] error 1


If anyone knows if I have missed something please tell me

btw im not that sure what the code does but if I can get this working I sould be able to do something more praktical.
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Networking With Winsock For An Mmo
1 Oct, 2007 - 10:33 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
QUOTE(flosy @ 1 Oct, 2007 - 11:23 AM) *
im not that sure what the code does


If you aren't sure what the code does, why are you using it? Why not try following a tutorial and writing your own code? Something like Beej's Guide to Network Programming.
User is offlineProfile CardPM
+Quote Post

flosy
RE: Networking With Winsock For An Mmo
1 Oct, 2007 - 11:42 AM
Post #3

New D.I.C Head
*

Joined: 15 Jul, 2007
Posts: 15


My Contributions
QUOTE(girasquid @ 1 Oct, 2007 - 11:33 AM) *

QUOTE(flosy @ 1 Oct, 2007 - 11:23 AM) *
im not that sure what the code does


If you aren't sure what the code does, why are you using it? Why not try following a tutorial and writing your own code? Something like Beej's Guide to Network Programming.


I have I get the same problem with all of the programes in it
User is offlineProfile CardPM
+Quote Post

fjp
RE: Networking With Winsock For An Mmo
19 Oct, 2007 - 02:13 AM
Post #4

New D.I.C Head
*

Joined: 19 Oct, 2007
Posts: 1


My Contributions
Hi Flosy

works here, (using viz)

I noticed you post on http://www.onrpg.com/boards/67102.html, is this a personal hobby project?

I'm looking to join someone working on a game and interested in doing a mmo server, I have experience in writting an HTTP server and leaderboard server and c++, would you be interested in my help? I wont get involved in opengl/graphics/game code tho, just the server and game client network code.

Do you have a development website/ftp/forum setup ?



This post has been edited by fjp: 19 Oct, 2007 - 02:43 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:31AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month