Inexplicable problem using dev-cpp to compile simple winsock example

This is a discussion on Inexplicable problem using dev-cpp to compile simple winsock example within the Windows Programming forums, part of the Platform Specific Boards category; Howdy all. I am having some serious trouble using Bloodshed dev-cpp to compile an example winsock server I got from ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    Inexplicable problem using dev-cpp to compile simple winsock example

    Howdy all. I am having some serious trouble using Bloodshed dev-cpp to compile an example winsock server I got from the web and messed around with a little bit. I am not sure what I'm doing wrong but I hope someone here can help. The following is the code which handles incoming data.

    Code:
    int HandleData(WPARAM wParam, LPARAM lParam)
    {
    	SOCKET sock = (SOCKET)wParam;
    	WORD event = LOWORD(lParam);
    	WORD error = HIWORD(lParam);
    
    	if (event == FD_CLOSE)
    	{
    		closesocket(sock);
    	}
    	else if (event == FD_READ)
    	{
    		char szBuffer[1024];
    		char *ret;
            ZeroMemory(szBuffer, 1024);
    		recv(sock, szBuffer, 1024, 0);
            MessageBox(Main.hWnd, szBuffer, "Received Data!", MB_OK);
    		if (strstr(szBuffer, "result")){
            SetWindowText(Edit2.hWnd, szBuffer);}
            closesocket(sock);
    	}
    	return TRUE;
    }
    If i leave in the "MessageBox(Main.hWnd, szBuffer, "Received Data!", MB_OK);" line then everything works as expected, (Edit2 displays my buffer.) but if I comment out the MessageBox line than Edit2 remains empty even when the buffer contains the magic word.(In this case 'result'). How is this possible, and how can I 'fix' it so that it behaves the way I mean for it to behave? Thank you for wasting your time reading this

    Oh-Yeah... I am using Bloodshed dev-cpp version 4.9.8.0 if that helps.

    <Edit: I fixed my wonky code tags...I dont know what I was thinking before >
    Last edited by emonk; 04-03-2003 at 07:05 AM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    OK. I noted that several people who posted after I did got responses before I did. Usually this means that i didn't provide enough information for people to help me. So, here goes.

    The following is the entire source of my program.

    Code:
    /****************************************************************************
     * server.cpp * Example Winsock server                                      *
     ****************************************************************************
     * Based on Example Winsock server by: Jonathan Overholt                      http://overholt.thinkhost.com *
     ****************************************************************************/
    
    #include <windows.h>
    #include <stdio.h>
    #include <winsock.h>
    
    //#pragma comment(lib, "wsock32.lib")
    
    struct MAIN
    {
    	HINSTANCE hInst;
    	HWND hWnd;
    
    	SOCKET Sock;
    	sockaddr_in Sin;
    } Main;
    
    struct Edit
    {
    	HINSTANCE hInst;
    	HWND hWnd;
    
    	SOCKET Sock;
    	sockaddr_in Sin;
    } Edit;
    
    struct Edit2
    {
    	HINSTANCE hInst;
    	HWND hWnd;
    
    	SOCKET Sock;
    	sockaddr_in Sin;
    } Edit2;
    
    int InitSocket(int nPort);
    int SendData(SOCKET sock, char * lpszData);
    LRESULT WINAPI WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    int HandleData(WPARAM wParam, LPARAM lParam);
    int HandleAccept(WPARAM wParam, LPARAM lParam);
    char editCmd[1024];
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, char * lpCmdLine, int nShowCmd)
    {
    /* hPrev is NULL unless there's another instance running.
                        We don't want two servers now, do we? */
    	if (hPrev)
    		return -1;
    
    	Main.hInst = hInst;
    
    	WSAData wsa;
    	WSAStartup(MAKEWORD(1, 1), &wsa);
    
    	WNDCLASS wnd;
    	wnd.cbClsExtra = 0;
    	wnd.cbWndExtra = 0;
    	wnd.hbrBackground = (HBRUSH) COLOR_WINDOW;
    	wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wnd.hInstance = Main.hInst;
    	wnd.lpfnWndProc = WinProc;
    	wnd.lpszClassName = "GenericServerClass";
    	wnd.lpszMenuName = NULL;
    	wnd.style = CS_HREDRAW | CS_VREDRAW;
    
    	RegisterClass(&wnd);
    	
    
    	Main.hWnd = CreateWindow("GenericServerClass", "Serving on port 1234...", WS_CAPTION | WS_POPUPWINDOW, 0, 0, 400, 600, NULL, NULL, Main.hInst, NULL);
    	Edit.hWnd = CreateWindow("EDIT", NULL, WS_CHILD|ES_MULTILINE, 1, 10, 398, 270, Main.hWnd, NULL, Edit.hInst, NULL);
    	Edit2.hWnd = CreateWindow("EDIT", NULL, WS_CHILD|ES_MULTILINE, 1, 300, 398, 270, Main.hWnd, NULL, Edit.hInst, NULL);
    	if (!Main.hWnd)
    		return -2;
    
    	if (!InitSocket(1234))
    	{
    		char szBuffer[1024];
    		
    		sprintf(szBuffer, "Error initializing listening socket [%d]", WSAGetLastError());
    
    		MessageBox(Main.hWnd, szBuffer, "Error", MB_OK);
    	}
    
    	ShowWindow(Main.hWnd, SW_SHOWNORMAL);
    	ShowWindow(Edit.hWnd, SW_SHOWNORMAL);
    	ShowWindow(Edit2.hWnd, SW_SHOWNORMAL);
    
    	MSG msg;
    
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	WSACleanup();
    
    	return msg.wParam;
    
    }
    
    int InitSocket(int nPort)
    {
    	if ((Main.Sock = socket(PF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR)
    		return FALSE;
    
    	Main.Sin.sin_family = AF_INET;
    	Main.Sin.sin_port = htons(nPort);
    
    	if (bind(Main.Sock, (sockaddr *)&Main.Sin, sizeof(sockaddr_in)) == SOCKET_ERROR)
    		return FALSE;
    
    	if (listen(Main.Sock, 200) == SOCKET_ERROR)
    		return FALSE;
    
    	WSAAsyncSelect(Main.Sock, Main.hWnd, WM_USER + 1, FD_ACCEPT);
    
    	return TRUE;
    }
    
    int SendData(SOCKET sock, char * lpszData)
    {
    	return send(sock, lpszData, strlen(lpszData), 0);
    }
    
    LRESULT WINAPI WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch (uMsg)
    	{
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    
    	case WM_USER + 1:
    		HandleAccept(wParam, lParam);
    		break;
    
    	case WM_USER + 2:
    		HandleData(wParam, lParam);
    		break;
    	}
    
    	return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    
    int HandleData(WPARAM wParam, LPARAM lParam)
    {
    	SOCKET sock = (SOCKET)wParam;
    	WORD event = LOWORD(lParam);
    	WORD error = HIWORD(lParam);
    
    	if (event == FD_CLOSE)
    	{
    		closesocket(sock);
    	}
    	else if (event == FD_READ)
    	{
    		char szBuffer[1024];
    		char *ret;
            ZeroMemory(szBuffer, 1024);
    		recv(sock, szBuffer, 1024, 0);
            MessageBox(Main.hWnd, szBuffer, "Received Data!", MB_OK);
    		if (strstr(szBuffer, "result")){
            SetWindowText(Edit2.hWnd, szBuffer);}
            closesocket(sock);
    	}
    	return TRUE;
    }	
    
    int HandleAccept(WPARAM wParam, LPARAM lParam)
    {
    	SOCKET sock = (SOCKET)wParam;
    	WORD event = LOWORD(lParam);
    	WORD error = HIWORD(lParam);
    	
    	if (event == FD_ACCEPT)
    	{
    		sockaddr_in NewRemoteSin;
    		SOCKET newsock = accept(sock, (sockaddr*)&NewRemoteSin, NULL);
    
    		WSAAsyncSelect(newsock, Main.hWnd, WM_USER + 2, FD_READ | FD_CLOSE);
    		GetWindowText(Edit.hWnd, editCmd,1023); 
    		SendData(newsock, editCmd);
    	}
    	return TRUE;
    }
    This program when executed should listen on port 1234, and if a connection is made it should send the contents of Edit1 to the requesting client (A web browser works fine for testing). I Usually put this in the top text box:
    Code:
    <HTML>
    <FORM METHOD="POST" ACTION="http://10.1.12.97:1234?">
    <TEXTAREA NAME="result" ROWS=6 COLS=40>
    </TEXTAREA>
    <INPUT TYPE="submit">
    <INPUT TYPE="reset">
    </FORM>
    </HTML
    If you then click the 'submit' button in your webbrowser it should send a put request which (amongst other crap) contain a 'result=' string (if there is something in the TEXTAREA). If the program receives a packet that has the string 'result' in it it should display that packets contents in Edit2. This is where my code goes wrong. If I leave in line 167 (MessageBox(Main.hWnd, szBuffer, "Received Data!", MB_OK) than everything works according to plan, but if I take line 167 out Edit2 remains empty. I cannot figure out why. This is just a learning exercise to help me figure out how to code with the winsock API's, but it has turned out to be much more difficult than I imagined. Any help or advice anyone could offer would be highly appreciated. I hope that this provides more of the necessary information to allow someone a better chance of understanding what I am asking.
    Last edited by emonk; 04-03-2003 at 11:55 AM.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    Lightbulb

    WTF!!! I just tried using IE6 instead of using Mozilla like I had been, and now it works fine! I dont get it! Mozilla 1.3 does not work and IE6 does. Now I'm even more confused. I need to go read some RFC's.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> I noted that several people who posted after I did got responses before I did.

    Could also mean that the people who replied later don't use the same IDE/compiler as you and did not want to make things worse for you by suggesting things that were inappropriate. Nothing personal!

    You are using a different header and library to me. I use winsock2.h and ws2_32.lib.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    "Could also mean that the people who replied later don't use the same IDE/compiler as you and did not want to make things worse for you by suggesting things that were inappropriate. Nothing personal!"

    Of course, of course. I didn't mean to come across as a whiner, I just thought that perhaps it was my fault for not phrasing my question well enough, or perhaps not including enough code. I have seen people yelled at for both things around here. Im always afraid of coming of as one of these half wits begging someone to do there homework for them without at least trying to do the basic research on their own first God! If I thought people saw me that way I'd probably give up on the Forums thing altogether, and just go pay someone to listen to me complain about how ignorant I am (A teacher ? )

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    No worries.

    I did have a quick look at it and couldn't see anything wrong, but as I said, I use completely different tools, so even if you had posted the whole thing first I couldn't have compiled it, and if I bodged it so I could, there is no way I could say it would behave the same way.

    Generally, when faced with odd behaviour, it is a good idea to give enough to enable someone to compile it. You can stare at code for an hour and learn less than you could with 2 minutes in a decent debugger.

    The ideal of course, is to reduce the problem to a small simple program which shows the errant behaviour. If that is not possible, quote the bits you are talking about in your text, and zip all the necessary files together and attach the .zip to your post.

    People who make it easy for others to help tend to get more help.

    You actually did well, you mentioned which compiler you were using - most people don't, on the C/C++ boards, they don't even mention which OS they are using often!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    adrianxw,
    I tried using the library and include you mentioned instead (winsock2), but it made no difference at all. It just compiled and ran the same. It was worth a shot though

    Whoever,
    "Generally, when faced with odd behaviour, it is a good idea to give enough to enable someone to compile it. You can stare at code for an hour and learn less than you could with 2 minutes in a decent debugger."

    That is interesting. In all of the tutorials, FAQ's, and books i have read on c/c++ I have yet to read much about debugging. I actually know a little x86 assembly language, so you would think I would be able to just jump right in and go.Holy crap! I just noticed dev-cpp has a built in debugger!!!! I was just going to ask where I could find a good free one. Now all I need to do is find some good tutorials or instructions on how to use it If anyone knows of a good tute I certainly would love to hear about it before I spend the next ten years wading through the thousands I just found with my Google search.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Just an idea...............

    I notice you are not checking the amount of data that recv() read.

    It could be that the messagebox is holding up the program long enough so all the data is read in one read ( the next read) rather than being split onto smaller chunks.

    If the data is split up the string 'result' may not ALL be in the same read causing the strstr() to 'fail'.

    You may want to wait until all the data has been recieved before sending it to the edit. If dealing only with simple strings I suggest sending the lengh of the string first as an int, then waiting for this amount to be read before processing.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  2. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM
  3. winsock recv() function problem
    By Rizwan Rafique in forum Windows Programming
    Replies: 5
    Last Post: 11-29-2001, 06:35 PM
  4. Simple boolean problem
    By larry in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2001, 08:49 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21