I have tried several times to get winsock to work and my applications online, but to no avail. Recently, I started making a simple messaging program. For testing purpose, i tried to see if it would connect on my own computer, and it did. However, when I try to have a friend connect to me from over the internet, things get into a loop on my end (which is partially my fault for coding it that way), and quickly returns an error on the client end.

Here is my code:
Code:
#include <windows.h>
#include <winsock2.h>
#include <stdio.h>

#define NETWORK_ERROR     -1
#define NETWORK_OK         0
#define ID_MESSAGELIST     100
#define ID_MESSAGE         101
#define ID_NICK            102
#define ID_CONNECT         103
#define ID_DISCONNECT      104
#define ID_HOST            105
#define ID_SEND            106

void ReportError(int, const char *);

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

char szClassName[ ] = "MessengerTest";
HWND hwnd, messagelist;
WORD sockVersion;
WSADATA wsaData;
int iResult;
SOCKET m_socket;
SOCKET AcceptSocket;
bool ishost;

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

{
    MSG messages;
    WNDCLASSEX wincl;

    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_BACKGROUND;

    if (!RegisterClassEx (&wincl))
        return 0;

    hwnd = CreateWindowEx (
           0,
           szClassName,
           "Messenger Test",
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,
           CW_USEDEFAULT,
           544,
           375,
           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)
{
    HDC hdc;
    PAINTSTRUCT ps;
    LPSTR szMessage = "Messages:";
    LPSTR szMessage2 = "Your Nickname:";
    LPSTR szMessage3 = "Enter a Message Here:";
    LPSTR szMessage4 = "Winsock Test: A Messaging Program";
    switch (message)
    {
        case WM_PAINT:
             hdc = BeginPaint(hwnd, &ps);
             SetBkColor(hdc, 0x007500);
             TextOut(hdc, 16, 256, szMessage, strlen(szMessage));
             TextOut(hdc, 16, 150, szMessage2, strlen(szMessage2));
             TextOut(hdc, 16, 200, szMessage3, strlen(szMessage3));
             TextOut(hdc, 16, 16, szMessage4, strlen(szMessage4));
             EndPaint(hwnd, &ps);
             break;
        case WM_CREATE:
            CreateWindow("Button", "Host", WS_BORDER | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 248, 48, 100, 30, hwnd, (HMENU) ID_HOST, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
            CreateWindow("Button", "Send", WS_BORDER | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 160, 151, 100, 30, hwnd, (HMENU) ID_SEND, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
            CreateWindow("Button", "Connect", WS_BORDER | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 16, 48, 100, 30, hwnd, (HMENU) ID_CONNECT, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
            CreateWindow("Button", "Disconnect", WS_BORDER | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 132, 48, 100, 30, hwnd, (HMENU) ID_DISCONNECT, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
            CreateWindow("Edit", "", WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, 16, 166, 80, 16, hwnd, (HMENU) ID_NICK, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
            CreateWindow("Edit", "", WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, 16, 216, 506, 16, hwnd, (HMENU) ID_MESSAGE, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
            messagelist = CreateWindow("Listbox", "", WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_BORDER | LBS_HASSTRINGS, 16, 272, 506, 60, hwnd, (HMENU) ID_MESSAGELIST, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
            SendMessage(messagelist, LB_ADDSTRING, 0, (LPARAM) "Program Started...");
            break;
        case WM_COMMAND:
             switch(LOWORD(wParam)) {
                 case ID_SEND:
                      break;
                 case ID_HOST:
                      iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
                      if ( iResult != NO_ERROR ) {
                          MessageBox(hwnd, "Error initializing Winsock!", "Error!", MB_ICONSTOP | MB_OK);
                          exit(0);
                      }
                      m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
                      if ( m_socket == INVALID_SOCKET ) {
                         MessageBox(hwnd, "Error at socket():", "Error!", MB_ICONSTOP | MB_OK);
                         MessageBox(hwnd, _itoa(WSAGetLastError(), NULL, 10), "Error!", MB_ICONSTOP | MB_OK);
                         WSACleanup();
                         exit(0);
                      }
                      sockaddr_in service;
                      service.sin_family = AF_INET;
                      service.sin_addr.s_addr = INADDR_ANY;
                      service.sin_port = htons( 27015 );
                      if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
                          MessageBox(hwnd, "bind() Failed!", "Error!", MB_ICONSTOP | MB_OK);
                          closesocket(m_socket);
                          exit(0);
                      }
                      SendMessage(messagelist, LB_ADDSTRING, 0, (LPARAM) "Created Host");
                      SendMessage(messagelist, LB_ADDSTRING, 0, (LPARAM) "Waiting for a Connection...");
                      if ( listen( m_socket, 1 ) == SOCKET_ERROR ) {
                          MessageBox(hwnd, "Error listening on socket!", "Error!", MB_ICONSTOP | MB_OK);
                          exit(0);
                      }
                      while (1) {
                            AcceptSocket = SOCKET_ERROR;
                            while (AcceptSocket == SOCKET_ERROR) {
                                  AcceptSocket = accept( m_socket, NULL, NULL );
                            }
                            SendMessage(messagelist, LB_ADDSTRING, 0, (LPARAM) "Client Connected!");
                            m_socket = AcceptSocket; 
                            break;
                      }
                      ishost = true;
                      break;
                 case ID_CONNECT:
                      iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
                      if ( iResult != NO_ERROR ) {
                           MessageBox(hwnd, "Error initializing Winsock!", "Error!", MB_ICONSTOP | MB_OK);
                           exit(0);
                      }
                      m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
                      if ( m_socket == INVALID_SOCKET ) {
                          MessageBox(hwnd, "Error at socket():", "Error!", MB_ICONSTOP | MB_OK);
                          MessageBox(hwnd, _itoa(WSAGetLastError(), NULL, 10), "Error!", MB_ICONSTOP | MB_OK);
                          WSACleanup();
                          exit(0);
                      }
                      sockaddr_in clientService;
                      clientService.sin_family = AF_INET;
                      clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
                      clientService.sin_port = htons( 27015 );
                      SendMessage(messagelist, LB_ADDSTRING, 0, (LPARAM) "Connecting...");
                      if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
                          MessageBox(hwnd, "Failed to connect.", "Error!", MB_ICONSTOP | MB_OK);
                          WSACleanup();
                          exit(0);
                      }
                      SendMessage(messagelist, LB_ADDSTRING, 0, (LPARAM) "Connected to Host!");
                      break;
                 case ID_DISCONNECT:
                      closesocket(m_socket);
	                  closesocket(AcceptSocket);
                      break;
             }
             break;
        case WM_DESTROY:
             closesocket(m_socket);
	         closesocket(AcceptSocket);
             PostQuitMessage (0);
             break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
I know that the send function doesnt work, I havent tried to fix it becuase i cant really test it.

Another thing is that when you click the host button for instance, I tell the messages box to say that you have completed making a host, waiting for client to connect, etc. However, it doesnt disply these messages untill after a connection. is this becuase it has to wait to go through the message loop again to process it?

Could it be my firewall, if the code is alright? I have Mcafee, but it also didnt work on a friend of mine's computer and he has a different firewall.


This is really bugging me. Much of the winsock part of the code came directly or indirectly from online tutorials, wo I do not understand why it is failing me.

All help is very much appreciated. Thanks in advance .

I am using Dev-C++ 4.9.9.2, and like I said I have Mcafee for a firewall.