Thread: winsock help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    18

    winsock help

    I have been working on this project for a while. Its my first winsock application so im probably missing something simple. The problem in having is that is cannot send more than 8 characters at a time. here is the source code.

    Code:
    #include <windows.h>
    #include <winsock.h>
    
    #include "main.h"
    #include "sock.h"
    
    HWND hwnd;               /* This is the handle for our window */
    
    HWND ipedit;             /* Handle for ip address edit window */
    HWND port;               /* Handle for tcp port edit window */
    HWND msgtosend;          /* Handle for message to send over connection edit window */
    HWND connectbutton;      /* Handle for connect button */
    HWND sendmsgbutton;      /* Handle for the button to send messages */
    HWND msgs;               /* Handle for static message history control */
    
    bool connected = false;
    int len;
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "Socket Tester";
    SOCKET s;
    
    bool Connect(char* ipaddress, int port)
    {
    SOCKADDR_IN target;
    target.sin_family = AF_INET;
    target.sin_port = htons(port);
    target.sin_addr.s_addr = inet_addr(ipaddress);
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(s==INVALID_SOCKET)return false;
    
    if(connect(s,(SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
    {
         return false;
    }
    WSAAsyncSelect(s,hwnd,WINSOCK_MESSAGE,FD_READ|FD_CLOSE|FD_ACCEPT);    
    return true;
    }
    
    void CloseSockets(void)
    {
         if(s) closesocket(s);
         WSACleanup();
    }                 
    
    void Append(char* text)
    {
         int nTxtLen = GetWindowTextLength(msgs); // Get length of existing text
    	 SendMessage(msgs, EM_SETSEL, nTxtLen, nTxtLen);	// move caret to end
    	 SendMessage(msgs, EM_REPLACESEL, 0, (LPARAM)text);	    // append text
    	 SendMessage(msgs, EM_SCROLLCARET, 0, 0);		// scroll to caret
    }
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        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 (GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON));
        wincl.hIconSm = (HICON)LoadImage (GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 16, 16, 0);
        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 */
               "Socket Tester",     /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               530,                 /* The programs width */
               410,                 /* 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;
    }
    
    
    /*  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_CREATE:
                 {
                 WSADATA wsadata;
    
                 int error = WSAStartup(0x0202, &wsadata);
    
                 //Did something happen?
                 if (error)
    	            return false;
    
                  //Did we get the right Winsock version?
                  if (wsadata.wVersion != 0x0202)
                  {
    	             WSACleanup(); //Clean up Winsock
    	             return false;
                  }
             
                 
                 ipedit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,
                          10, 10, 100, 20, hwnd, (HMENU)IDC_IPTEXT, GetModuleHandle(NULL), NULL);      
                 if(ipedit==NULL)MessageBox(hwnd,"Error 1","Error",0);
                 SendMessage(ipedit, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                                     MAKELPARAM(false,0));
                 
                 port = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL|ES_NUMBER,
                        115, 10, 50, 20, hwnd, (HMENU)IDC_PORT, GetModuleHandle(NULL), NULL);
                 if(port==NULL)MessageBox(hwnd,"Error 2","Error",0);
                 SendMessage(port, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                                     MAKELPARAM(false,0));
                 
                 connectbutton = CreateWindowEx(0, "BUTTON", "Connect", WS_CHILD|WS_VISIBLE, 180, 10,
                                 50, 20, hwnd, (HMENU)IDC_CONNECTBUTTON, GetModuleHandle(NULL), NULL);
                 if(connectbutton==NULL)MessageBox(hwnd,"Error 3","Error",0);
                 SendMessage(connectbutton, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                                           MAKELPARAM(false,0));
                 
                 msgs = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD|WS_VISIBLE|ES_READONLY|ES_MULTILINE|WS_VSCROLL|ES_AUTOVSCROLL, 
                        10, 40, 500, 300, hwnd, (HMENU)IDC_MSGS, GetModuleHandle(NULL), NULL);
                 if(msgs==NULL)MessageBox(hwnd,"Error 4","Error",0);
                 SendMessage(msgs, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                                   MAKELPARAM(false,0));
                 
                 msgtosend = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,
                             10, 350, 400, 20, hwnd, (HMENU)IDC_MSGTOSEND, GetModuleHandle(NULL), NULL);
                 if(msgtosend==NULL)MessageBox(hwnd,"Error 5","Error",0);
                 SendMessage(msgtosend, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                                   MAKELPARAM(false,0));
                 
                 sendmsgbutton = CreateWindowEx(0, "BUTTON", "Send", WS_CHILD|WS_VISIBLE, 420, 350, 50, 20,
                                 hwnd, (HMENU)IDC_SENDMSGBUTTON, GetModuleHandle(NULL), NULL);
                 if(sendmsgbutton==NULL)MessageBox(hwnd, "Error 6", "Error", 0);
                 SendMessage(sendmsgbutton, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                                   MAKELPARAM(false,0));
                 }
                 break;
            case WM_COMMAND:
                 switch(LOWORD(wParam))
                 {
                        case IDC_CONNECTBUTTON:
                             {
                             len = GetWindowTextLength(GetDlgItem(hwnd, IDC_IPTEXT));
                             if(len > 0)
                             {
                                    int i;
                                    char* buf;
                                    buf = (char*)GlobalAlloc(GPTR, len + 1);
                                    GetDlgItemText(hwnd, IDC_IPTEXT, buf, len + 1);
                                    int port2 = GetDlgItemInt(hwnd,IDC_PORT,NULL,true);
                                    
                                    if(Connect(buf,port2)){SetWindowText(msgs,"Connected successfully!\r\n");connected = true;}
                                    
                                    GlobalFree((HANDLE)buf);
                             }
                             }
                             break;
                             
                        case IDC_SENDMSGBUTTON:
                             {
                                  if(connected)
                                  {
                                     len = GetWindowTextLength(msgtosend);
                                     if(len > 0)
                                     {
                                            char* buf;
                                            buf = (char*)GlobalAlloc(GPTR, len + 1);
                                            GetDlgItemText(hwnd, IDC_MSGTOSEND, buf, len + 1);
                                            Append(strcat(buf,"\r\n"));
                                            send(s,strcat(buf,"\r\n"),sizeof(buf)+5,0);
                                     }
                                  }                       
                             }
                             break;
                 }            
                 break;
            case WINSOCK_MESSAGE:
                 switch(lParam)
                 {
                         case FD_READ:
                              char buff[80];
                              memset(buff,0,sizeof(buff));
                              recv(s,buff,sizeof(buff)-1,0);
                              Append(strcat(buff,"\r\n"));
                              break;
                         case FD_CLOSE:
                              if(s)closesocket(s);
                              connected=false;
                              Append("Connection closed.\r\n");
                              break;
                 }     
                 break;
            case WM_DESTROY:
                CloseSockets(); 
                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;
    }
    The two headers are just for defining things. Could someone please help?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    char* buf;
    send(s,strcat(buf,"\r\n"),sizeof(buf)+5,0);
    sizeof(buf) is the same as sizeof(char*).

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Winsock - Where do i start?
    By Brain Cell in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-14-2005, 01:39 PM
  4. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM