Valhalla Legends Forums Archive | C/C++ Programming | Making a server and need some help. c++

AuthorMessageTime
Final
Ok well I have been working on this server for a couple of weeks. But now it seems something is wrong I was wondering anyone would take the time and tell me what seems to be wrong. Because For some reason it does multiple calls to FD_ACCEPT and the person is only connecting once.?
[code]
#include <windows.h>
#include <winsock2.h>
#include <iostream>
#include <string>
#include <time.h>
#include <fstream>
#include <sstream>
#include <mysql/mysql.h>
#include <exception>
#include <stdio.h>
using namespace std;
int users=0,max_users=20;
SOCKET s[100],c[20];
sockaddr_in serverInfo;
DWORD sessionid[20];
char* cn[20];
char* username[20];
char packetbuffer[256];
bool conn[20];
bool ver[20];
bool used[20];
HWND hedit_1,hedit_2,user_count;
HINSTANCE g_hInst;
#include "Resource.h"
#include "serverfs.h"
#include "packetbuffer.h"


LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)

{
    HWND hwnd;              /* This is the handle for our window */
    MSG msg;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
   
    /* The Window structure */
    wincl.hInstance = hinstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
          WS_EX_CLIENTEDGE,                  /* Extended possibilites for variation */
          szClassName,        /* Classname */
          "Archiac Times Server!",      /* Title Text */
          (WS_SYSMENU | WS_MINIMIZEBOX), /* default window */
          CW_USEDEFAULT,      /* Windows decides the position */
          CW_USEDEFAULT,      /* where the window ends up on the screen */
          544,                /* The programs width */
          375,                /* and height in pixels */
          HWND_DESKTOP,        /* The window is a child-window to desktop */
          NULL,                /* No menu */
          hinstance,      /* Program Instance handler */
          NULL                /* No Window Creation data */
          );

    /* Make the window visible on the screen */
    CreateWindowEx(0,                                      //more or 'extended' styles
                      TEXT("BUTTON"),                        //'class' of control to create
                      TEXT("Send"),            //the control caption
                      WS_VISIBLE | WS_CHILD,  //control style: how it looks
                      425,                                    //control position: left
                      300,                                    //control position: top
                      100,                                    //control width
                      25,                                    //control height
                      hwnd,                                  //parent window handle
                      (HMENU)IDB_BUTTON,                                  //control's ID
                      g_hInst,                                //application instance
                      NULL);
    hedit_1=CreateWindowEx(WS_EX_CLIENTEDGE,                      //more or 'extended' styles
                      TEXT("EDIT"),                          //'class' of control to create
                      TEXT(""),      //the control caption
                      WS_CHILD|WS_VISIBLE|WS_BORDER,          //control style: how it looks
                      10,                                    //control position: left
                      300,                                    //control position: top
                      400,                                  //control width
                      25,                                    //control height
                      hwnd,                                  //parent window handle
                        (HMENU)IDC_EDITBOX_,
                        g_hInst,
                        NULL);
                       
    ShowWindow (hwnd, ncmdshow);
    UpdateWindow(hwnd);
    WSADATA w;
int error = WSAStartup (0x0202,&w);
if (error)
{
MessageBox (NULL,"Error: You need WinSock 2.2!","Error",MB_OK);
PostQuitMessage (0);
return 0;
}
if (w.wVersion!=0x0202)
{
MessageBox (NULL,"Error:  Wrong WinSock version!","Error",MB_OK);
PostQuitMessage (0);
WSACleanup ();
return 0;
}
//BIND CONNECTION//
s[0] = socket (AF_INET,SOCK_STREAM,IPPROTO_TCP); // create socket
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(1007);
    ncmdshow= bind(s[0], (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
listen(s[0], 10);
//END OF BIND//
// Make socket asynchronous and hook to WM_SOCKET message defined earlier
WSAAsyncSelect(s[0], hwnd, WM_SOCKET, FD_READ | FD_WRITE |FD_ACCEPT | FD_CLOSE);
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&msg, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&msg);
        /* Send message to WindowProcedure */
        DispatchMessage(&msg);
    }
}
/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lparam)
{
static HINSTANCE hLib;
static TCHAR    chCntrlName[32];
    switch (msg)
    {
    case WM_CREATE:
        hLib=LoadLibrary(TEXT("RICHED20.DLL"));
        if (!hLib)
            {
            hLib=LoadLibrary(TEXT("RICHED32.DLL"));
            if (!hLib)
                {
                MessageBox( NULL,
                            TEXT("Failed to load rich edit library"),
                            TEXT("ERROR"),
                            MB_OK|MB_ICONERROR);
                return 0;
                }
            else
                {
                lstrcpy(chCntrlName,TEXT("RICHEDIT"));
                }
            }
        else
            {
            lstrcpy(chCntrlName,RICHEDIT_CLASS);
            }
        hedit_2=CreateWindowEx(WS_EX_CLIENTEDGE,
            chCntrlName,
            "Server Has Intiliazed.",
            WS_VISIBLE|WS_CHILD|ES_MULTILINE|WS_VSCROLL,
            10,
            10,
            400,
            280,
            hwnd,
            NULL,
            NULL,
            NULL);
            break;
    case WM_DESTROY:
            PostQuitMessage (0);      /* send a WM_QUIT to the message queue */
            break;
    case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
        case IDB_BUTTON:
                {
                    switch (HIWORD(wParam))
                    {
                        case BN_CLICKED:
                            /*ZeroMemory(packetbuffer, 256);
                            char message[5000];
                            char csnt[MAX_PATH];
                            GetWindowText(hedit_1, message, 5000);
                            sprintf(csnt, "%s%s","Server: ",message);
                            addchat(csnt);
                            sendchat(message);
                            */
                            //Keep this Unaccessable for now//
                           
                        break;
                    }
                }
                break;
                }
                }
                break;

        case WM_SOCKET:
        {
if (WSAGETSELECTERROR(lparam))
{
                }
}
switch (WSAGETSELECTEVENT(lparam))
{
            case FD_READ:
            {
              checkclients();
              sockaddr_in incoming_info;
              int socklen = sizeof(incoming_info);
              ZeroMemory(packetbuffer, 256);
              recvfrom(wParam, packetbuffer, sizeof(packetbuffer), 0,(struct sockaddr *)&incoming_info, &socklen);
              pb.pbuffer(packetbuffer,inet_ntoa(incoming_info.sin_addr),wParam);
            } break;
            case FD_WRITE:
            {
            } break;
            case FD_ACCEPT:
              {
              ZeroMemory(packetbuffer, 256);
              bool found=false;
              for(int i=0;i<max_users;i++){
                      if(used[i]!=true && found!=true){
                      sockaddr_in clientinfo;
                      int clientinfosize;           
                      char* client_ip;
                      char sx[MAX_PATH];
                    c[i]=accept(wParam, (struct sockaddr *)&clientinfo, &clientinfosize);
                    client_ip=inet_ntoa(clientinfo.sin_addr);
                      cn[i]=client_ip;
                    used[i]=true;
                    found=true;
                      addchat("A User Has Connected To the Server");
                      break;
                      }
                      if(found!=false){
                      break;
                      }
                      }
                } break;
                case FD_CLOSE:
                {
                for(int i=0;i<max_users;i++){
                if (c[i] == INVALID_SOCKET && used[i]!=false) {
                        used[i]=false;
                        char sx[MAX_PATH];
                        sprintf(sx,"%s Has Disconnected",cn[i]);
                        addchat(sx);
                        i=10000;
            }
                }
                } break;
}
} break;
//Socket End//
        default:
            return DefWindowProc (hwnd, msg, wParam, lparam);
    }
    return 0;
}
[/code]
Hmm I tried something and it worked but i still want comments on this if you would.
April 23, 2006, 6:56 AM
Topaz
This isn't your code...
April 23, 2006, 10:02 AM
warz
I don't know about that. He has been talking to me about this for a few days now.
April 23, 2006, 10:20 AM
Topaz
The spelling and grammar in the comments is perfect - if you read his other posts, thats a huge contrast.
April 23, 2006, 10:39 AM
Myndfyr
[quote author=Topaz link=topic=14830.msg151097#msg151097 date=1145788756]
The spelling and grammar in the comments is perfect - if you read his other posts, thats a huge contrast.
[/quote]

I would definitely not say "perfect"
April 23, 2006, 10:42 AM
Final
I feel outraged at the commentary that you guys set out on my code. First of all I spent weeks doing this and theirs more behind this. But im yet to say im sorry you feel that way.
April 23, 2006, 4:46 PM
Explicit[nK]
[quote author=Topaz link=topic=14830.msg151097#msg151097 date=1145788756]
The spelling and grammar in the comments is perfect - if you read his other posts, thats a huge contrast.
[/quote]

It's a combination of code from tutorials, and pieces of his own.
April 23, 2006, 7:18 PM
Final
?... Dude ok Watch I use code from devc++ as my skeleton ? everything else in there is mine ? like wtf.
April 24, 2006, 5:10 AM
Yegg
[quote author=Final link=topic=14830.msg151177#msg151177 date=1145855417]
?... Dude ok Watch I use code from devc++ as my skeleton ? everything else in there is mine ? like wtf.
[/quote]

What they're saying is that all of the code was not hand written personally, by you. One very evident reason is because of your spelling and grammar. The comments in there are quite informative and there are quite a few in the code that I would not believe to be yours. Certain ways that the code was formatted looks like what no human would write, unless it changed after being posted on the forum.

I don't see how this could take weeks to complete, even if you spent 10 minutes a day. Unless of course there is much much more code than this?

Perhaps you didn't get information from for example tutorials like Explicit insisted, he was most likely simply guessing, don't worry about it.

Explain more about your first post before making it, it should prevent such an occurence like this to happen.
April 24, 2006, 7:58 PM
Final
Well Theres more to this than you see. All I was asking is critisim on the winsocketry code at the bottom. Just to see if i can do anything better I would really appreciate that.
I cant bring all code behind the main because it is being held in 4 other pages for neatness in the programming. (Sorry if this angers anyone)
April 24, 2006, 11:36 PM

Search