View Single Post
Old Jan 16th, 2006, 5:43 PM   #6
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Quote:
Originally Posted by nnxion
Could you post the whole code?

I know it's really messy, should still be readable though.. im still having the same problems as before when trying your method cache, so i im pretty sure im just doing something wrong.

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <winsock2.h>
#include <iostream>
#include <fstream>
#define _WIN32_IE 0x0500
#include <commctrl.h>

#define MAX 500
#define ENTRY 31 
#define MAXPENDING 5
#define BUFFER_SIZE 256
#define EXIT_CALL " exit"
#define CONNECTTO 99
#define PORT 100
#define REMOTEADDRESS1 101
#define MY_SENDBUTTON 301
#define MY_CONNECTBUTTON 300

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

char szClassName[ ] = "WindowsApp";
   
   HWND hwndhello;
   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 */

         int local_socket = 0, remote_socket = 0, menu_switch = 0, message_length = 0, remote_length = 0;
     unsigned short local_port = 0, remote_port = 0;
     struct sockaddr_in local_address, remote_address;
     WSADATA wsa_data;
     char message[BUFFER_SIZE], remote_ip[32];

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

{


INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCtrlEx.dwICC  = ICC_INTERNET_CLASSES;
InitCommonControlsEx(&InitCtrlEx);
    
local_port = 7447;

     if (WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0)
     {
          std::cout << "WSAStartup() failed\n";
          return (1);
     }


remote_port = 51;
fflush(stdin);
               if ((remote_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
               {
                    std::cout << "socket() failed\n";
                    return(1);
               }
    

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure; 
    wincl.style = CS_DBLCLKS;             
    wincl.cbSize = sizeof (WNDCLASSEX);


    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;   
    wincl.cbClsExtra = 0;   
    wincl.cbWndExtra = 0;                
    wincl.hbrBackground = (HBRUSH) (COLOR_WINDOW+18);

   
    if( !RegisterClassEx( &wincl ) )
    {
        MessageBox( hwnd, "Can't register window class!", "Error", MB_OK | MB_ICONEXCLAMATION );
        return 0;
    }


    hwnd = CreateWindowEx (
           0,                   
           szClassName,        
           "Test App",      
           WS_OVERLAPPED | WS_VISIBLE | WS_SYSMENU,
           CW_USEDEFAULT,       
           CW_USEDEFAULT,      
           400,                 
           240,                 
           HWND_DESKTOP,       
           NULL,               
           hThisInstance,      
           NULL                
           );

   
    ShowWindow (hwnd, nFunsterStil);

   
    while (GetMessage (&messages, NULL, 0, 0))
    {
      
        TranslateMessage(&messages);
      
        DispatchMessage(&messages);
    }

 
    return messages.wParam;
}




LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 char szText[BUFFER_SIZE]; 
    switch (message)               
    {
           case WM_CREATE:
                {
                     
                      
                     hwndhello= CreateWindowEx (
                      0,
                      "button",
                      "Connect",
                      WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
                      312,
                      165,
                      66,
                      26,
                      hwnd,
                      (HMENU) MY_CONNECTBUTTON,
                      GetModuleHandle( NULL ),
                      NULL
                      );                     
                     
                      /* Send button */
                     hwndhello= CreateWindowEx (
                      0,
                      "Button",
                      "Send",
                      WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | ES_PASSWORD,
                      235,
                      165,
                      66,
                      26,
                      hwnd,
                      (HMENU) MY_SENDBUTTON,
                      GetModuleHandle( NULL ),
                      NULL
                      );     
                      /* Message send box */
                      hwndhello= CreateWindowEx (
                      WS_EX_WINDOWEDGE,
                      "edit",
                      NULL,
                      WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | WS_BORDER | ES_WANTRETURN,
                      30,
                      169,
                      200,
                      22,
                      hwnd,
                      NULL,
                      GetModuleHandle( NULL ),
                      NULL
                      ); 
                      /* Message recieve box */
                      hwndhello= CreateWindowEx (
                      WS_EX_WINDOWEDGE,
                      "edit",
                      NULL,
                      WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
                      30,
                      12,
                      200,
                      150,
                      hwnd,
                      NULL,
                      GetModuleHandle( NULL ),
                      NULL
                      );
                      /* Message recieve box */
                      hwndhello= CreateWindowEx (
                      WS_EX_WINDOWEDGE,
                      "edit",
                      NULL,
                      WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
                      30,
                      32,
                      200,
                      130,
                      hwnd,
                      NULL,
                      GetModuleHandle( NULL ),
                      NULL
                      );
                      /* Port address */
                     hwndhello= CreateWindowEx (
                      WS_EX_WINDOWEDGE,
                      "edit",
                      "51",
                      WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | WS_BORDER | ES_NUMBER | ES_RIGHT,
                      334,
                      100,
                      52,
                      22,
                      hwnd,
                      (HMENU) PORT,
                      GetModuleHandle( NULL ),
                      NULL
                      );
                      CreateWindow(
                      WC_IPADDRESS,   // predefined class 
                      "",	   // text 
                      WS_VISIBLE | WS_CHILD,  // styles 
                      270,
                      50,
                      116,
                      22,	// button height 
                      hwnd,	   // parent window 
                      (HMENU)REMOTEADDRESS1,	   // ID 
                      (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), // App instance
                      NULL);	 // po
                      /* Port address */
                     hwndhello= CreateWindowEx (
                      WS_EX_WINDOWEDGE,
                      "static",
                      "Port Number",
                      WS_CHILD | WS_VISIBLE,
                      304,
                      79,
                      85,
                      18,
                      hwnd,
                      NULL,
                      GetModuleHandle( NULL ),
                      NULL
                      );
                      /* Ip address */
                     hwndhello= CreateWindowEx (
                      WS_EX_WINDOWEDGE,
                      "static",
                      "IP Address",
                      WS_CHILD | WS_VISIBLE,
                      314,
                      31,
                      74,
                      18,
                      hwnd,
                      NULL,
                      GetModuleHandle( NULL ),
                      NULL
                      );
                      /* Connect to.. */
                     hwndhello= CreateWindowEx (
                      WS_EX_WINDOWEDGE,
                      "static",
                      "Connected to..",
                      WS_CHILD | WS_VISIBLE,
                      32,
                      14,
                      197,
                      18,
                      hwnd,
                      (HMENU) CONNECTTO,
                      GetModuleHandle( NULL ),
                      NULL
                      );

;}
  
                case WM_COMMAND:
    if( HIWORD( wParam ) == BN_CLICKED )
             {
                 
    switch(LOWORD(wParam))
    {
    
        case REMOTEADDRESS1:
      if( GetWindowTextLength(GetDlgItem(hwnd, REMOTEADDRESS1)) > 15){
      MessageBox( hwnd, "You must enter a port number now!", "Error", MB_OK | MB_ICONEXCLAMATION );
      }
      return TRUE;
             break;
             }

        case MY_CONNECTBUTTON:

                              if( GetWindowTextLength(GetDlgItem(hwnd, REMOTEADDRESS1)) == 8 ||
                                  GetWindowTextLength(GetDlgItem(hwnd, PORT)) < 2)
                              {
                                  MessageBox( hwnd, "Please fill all the fields!", "Error", MB_OK | MB_ICONEXCLAMATION );
                                  return 0;
                              }

      


        
        remote_address.sin_family = AF_INET; /* Internet address family */
        remote_address.sin_addr.s_addr = inet_addr("127.0.0.1"); /* Server IP address */
        remote_address.sin_port = htons(remote_port); /* Server port */

        if (connect(remote_socket, (struct sockaddr *) &remote_address, sizeof(remote_address)) < 0)
        {
        std::cout << "connect() failed\n";

            break;   
            }

            
            default:
            return DefWindowProc (hwnd, message, wParam, lParam);
            
            case WM_DESTROY:
            PostQuitMessage (0);
            break; 

    }
}
}
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote