Valhalla Legends Forums Archive | C/C++ Programming | Need some help!

AuthorMessageTime
Final
Well first of all im starting to learn winsock but I wanted to learn
Asynchronous Socket
Because I think its alot easier to handle clients information because it has its cases for client connect data recieved and data send.

But im having a problem because I dont know how it can be implanted into a program this is the code i wanted to implant but couldnt.


#define YOUR_SOCKET_MSG  WM_USER + 100 // WM_USER is a base value for user-defined messages

...

WSAAsyncSelect(theSocket, hwnd, YOUR_SOCKET_MSG, FD_READ | FD_WRITE | FD_CONNECT);.


//in window procedure:

switch (msg) {
  case YOUR_SOCKET_MSG:
      if (WSAGETSELECTERROR(lParam)) { // If an error occurred,
        closesocket(theSocket); // Close the socket
        WSACleanup(); // Shutdown Winsock
        return NETWORK_ERROR; // Return NETWORK_ERROR, WSAGetLastError(), etc.
      }
      switch (WSAGETSELECTEVENT(lParam)) { // Differentiate between the events
        case FD_READ:
    // Receive data
    break;
case FD_WRITE:
    // Write data
    break;
        case FD_CONNECT:
    // Just connected to server
    break;
      }
      break;
}

How do I get that into a c++ program please if anyone can just make a working example of this would mke my day cuz  i looked into google as well and would give me the exact same stuff to put into a win32 api but dont know were to put it all please help!!
January 6, 2006, 11:57 PM
Kp
[quote author=Final link=topic=13814.msg140864#msg140864 date=1136591829]How do I get that into a c++ program please if anyone can just make a working example of this would mke my day cuz  i looked into google as well and would give me the exact same stuff to put into a win32 api but dont know were to put it all please help!![/quote]

Save the code in a file with the .cpp extension and add the resulting file to your compilation environment (Visual Studio or Dev-C++).
January 7, 2006, 2:42 AM
Final
Ok Well I found a better tutorial on the same subject and I finally came out with this code using the tutorial code to instert into a simple win32 api project and this is what i came out with.

#include <windows.h>
#include <winsock.h>
// the message we'll use for our async notification
#define WM_WSAASYNC (WM_USER +5)


/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
int PASCAL FAR WSAAsyncSelect(SOCKET s,
                              HWND hwnd,
                              unsigned int wMsg,
                              long lEvent);
/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;              /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    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 (
          0,                  /* Extended possibilites for variation */
          szClassName,        /* Classname */
          "Windows App",      /* Title Text */
          WS_OVERLAPPEDWINDOW, /* 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 */
          hThisInstance,      /* Program Instance handler */
          NULL                /* No Window Creation data */
          );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}
// create and test the socket
//Port = socket(AF_INET, SOCK_STREAM, 0);

WSAAsyncSelect(htons(10), hwnd, WM_WSAASYNC, FD_READ | FD_WRITE |FD_ACCEPT | FD_CLOSE)
{
switch(msg)
{
case WM_WSAASYNC:
  {
    // what word?
    switch(WSAGETSELECTEVENT(lParam))
    {
case FD_READ:
{
    int bytes_recv = recv(wParam, &data, sizeof(data), 0);         
}break;
case FD_WRITE:
  {
    //Dont use this for send it wont work for our advantage we must use Send(); in the Read case to make
    //it work properly.
              // enter an infinite loop
    while(TRUE)
    {
      // read in more data from the file and store it in packet.data.
      in.read((char*)&packet.data, MAX_PACKET_SIZE);

      // increment the amount of data sent
      data_sent += strlen(packet.data);

      // send the packet off to the Server if it is filled
     
      if (send(wparam, (char*)(&packet), sizeof(PACKET), 0) == SOCKET_ERROR)
      {
        // check if the network buffer is full and can send no more
        // data. If so then break from the loop
        if (WSAGetLastError() == WSAEWOULDBLOCK)
        {
          // break from the loop - buffer is full
          break;
        }
        else // another error
        {
          // display an error message and clean up
          CleanUp();
          return(0);
        }
      }
    }
  } break;
case FD_ACCEPT:
  {
              // holds size of client struct
    int lenclient = sizeof(client_sock);
    // connect to the server
    Port = accept(wParam, &client_sock, &lenclient);
  } break;
case FD_CLOSE:
  {
  } break;
    }
  }
}
}
/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);      /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

But now Im getting an error around this part.

WSAAsyncSelect(htons(10), hwnd, WM_WSAASYNC, FD_READ | FD_WRITE |FD_ACCEPT | FD_CLOSE)

What am i doing wrong please help me the error message dosent really help its one of those error message that dosent give you a specific and just gives an error of missing a ; or before } i tried everything but no luck could someone help me out im despriate
January 7, 2006, 5:44 AM
Kp
[quote author=Final link=topic=13814.msg140912#msg140912 date=1136612675]What am i doing wrong please help me the error message dosent really help its one of those error message that dosent give you a specific and just gives an error of missing a ; or before } i tried everything but no luck could someone help me out im despriate[/quote]

Have you considered that you're just not ready to do this?  The error message told you exactly what was wrong!  You [u]are[/u] missing a semicolon before the { right next to the WSAAsyncSelect.  Also, please start using code tags when you paste code.  If you're feeling really helpful, you should also turn on lexical highlighting.
January 7, 2006, 5:22 PM
Final
Ok guys after alot of hard work and determination I finally got it working damn was it hard i was messing alot of things i eventually found a game that had that type of socket programming I was trying to learn but when I went into the the folder i thought the client was the server and copyed the wrong code It didnt hit me until like 40 mins later that i was using the wrong one and went into the server thinking it was the same coding as the client as socket type then i opened it and saw it was BLOCKING SOCKETS wich ticked me off but then I thout it over after i thout i was defeated then thout wy not change the client into a server and that is exactly what i did and finally got that shis working now i am one happy teen lol thanks for all trying to help me out on this i really appreciate it.
January 8, 2006, 7:16 AM
Kp
Was there a point to that post?  Was there even a coherent thought in it?  I couldn't find one in that mess...
January 8, 2006, 7:27 AM
shout
[quote author=Kp link=topic=13814.msg140946#msg140946 date=1136654534]
If you're feeling really helpful, you should also turn on lexical highlighting.
[/quote]

[OT] How does one do this?
January 8, 2006, 8:23 AM
Kp
[quote author=Shout link=topic=13814.msg141042#msg141042 date=1136708623]
[quote author=Kp link=topic=13814.msg140946#msg140946 date=1136654534]
If you're feeling really helpful, you should also turn on lexical highlighting.
[/quote]

[OT] How does one do this?
[/quote]

In vim, :sy on.  For the forum, you'd need a helper tool that can take lexically highlighted data in something else (BinaryChat, Visual Studio(?), vim, etc.) and generate a plaintext forum post, marking it up with [font color=] [/font] tags as it goes.  Then you post the resulting text.  You can see examples of this type of thing in the "Funny Clips from Op [vL]" thread, where some of the members post lexically highlighted chat logs.
January 8, 2006, 4:55 PM
shout
[quote author=Kp link=topic=13814.msg141074#msg141074 date=1136739342]
[quote author=Shout link=topic=13814.msg141042#msg141042 date=1136708623]
[quote author=Kp link=topic=13814.msg140946#msg140946 date=1136654534]
If you're feeling really helpful, you should also turn on lexical highlighting.
[/quote]

[OT] How does one do this?
[/quote]

In vim, :sy on.  For the forum, you'd need a helper tool that can take lexically highlighted data in something else (BinaryChat, Visual Studio(?), vim, etc.) and generate a plaintext forum post, marking it up with [font color=] [/font] tags as it goes.  Then you post the resulting text.  You can see examples of this type of thing in the "Funny Clips from Op [vL]" thread, where some of the members post lexically highlighted chat logs.
[/quote]

I thought there might have been a tag.
January 9, 2006, 2:38 AM

Search