🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #4696

Winsock - App Hangs after sending a few messages.

#4696
From: Luke
Date: Tue, 04 Jan 2005 10:03
68 lines
4350 bytes
Hi everybody,

I searched up and down for similar problems before posting and then figured I should ask.  I'm developing a test winsock application in Builder 5,  lan instant messege tool.  So far I am able to make the connection and can continously send data back and forth, as long as I keep typing.  Currently I have the recv() function inside a TTimer that checks if their is information to receive every ten seconds. If I allow both applications to sit idle for longer than ten seconds I'm no longer able to use them.

Some initial ideas were that they were setup as blocking connections (which I found out later that they are by default) and if one socket did not complete a send command before attempting a retrieval there would be problems. However I added the WSAAsyncSelect functions into the code to make them non-blocking connections, and have the same problem.  

Through more reading I found that when you use WSAAsyncSelect you can setup WndProc to handle your windowns messages.  That process I am unfamiliar with how to setup if I started with Borland's Scratch Application (from the new prompt).

If anyone would be willing to look through the code snippets (there placed throughout event handlers in the application) and give me some pointers or just explain how to setup a windows message handling system I would highly appreciate it.

// Here are some variables  and functions setup prior to socket creation.
    SOCKET Client, Server;
    HOSTENT Host;
    SOCKADDR_IN LocalSocketIn;
    WSADATA  wsaData;
    HOSTENT  *pHostEnt;
    char     Hostname[100];

    WSAStartup(0x101,&wsaData);
    gethostname(Hostname, sizeof(Hostname));
    pHostEnt = gethostbyname(Hostname);

// Here is the code used to setup the client socket.
    Client = socket(AF_INET, SOCK_STREAM, 0);
    Host.h_addr_list = pHostEnt->h_addr_list;
    memset(&LocalSocketIn, 0, sizeof LocalSocketIn);
    LocalSocketIn.sin_family = AF_INET;
    LocalSocketIn.sin_addr.s_addr = ((struct in_addr *)(Host.h_addr))->s_addr;
    LocalSocketIn.sin_port = htons(55000);
    LocalSocketIn.sin_addr.s_addr = inet_addr("My IP Address");   // removed the actuall IP address for the post
    connect(Client, (SOCKADDR *)&LocalSocketIn, sizeof(SOCKADDR_IN);

// Here is the code used to setup the server socket  
    Server = socket(AF_INET, SOCK_STREAM, 0);
    memset(&LocalSocketIn, 0, sizeof LocalSocketIn );
    LocalSocketIn.sin_family = AF_INET;
    LocalSocketIn.sin_addr.s_addr = INADDR_ANY;
    LocalSocketIn.sin_port = htons(55000);

    if (bind(Server, (SOCKADDR *)&LocalSocketIn, sizeof(SOCKADDR_IN)) != SOCKET_ERROR)
    {
        while (listen(Server, MAXCONN) == SOCKET_ERROR);
        Label6->Caption = "Server Listening";       

        int length;
        length = sizeof LocalSocketIn;
        Client = accept(Server, (SOCKADDR *)&LocalSocketIn, &length);
        WSAAsyncSelect(Client, hWnd, C_SOCKET_MSG, 0);
    }    

// Here are my send and receive functions (used for both the server and the client side)
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        if(send(Client, Message->Text.c_str(), Message->Text.Length(), NULL) == SOCKET_ERROR)
            HandleError();
    }

    void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
        char temp[100];
        if (recv(Client, temp, 100, NULL) == SOCKET_ERROR) HandleError();
        AnsiString temp2(temp);
        Memo->Lines->Strings[0] = temp2;
    }

Thanks,
CrazyLuke
Technical Details
Message-ID: <41da69f3$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,a0eb9df8217c6d4f,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Luke" <lukedog264@msn.com>
Sender: "Luke" <lukedog264@msn.com>
Reply-To: "Luke" <lukedog264@msn.com>
Subject: Winsock - App Hangs after sending a few messages.
Newsgroups: borland.public.cppbuilder.internet.socket
X-User-Info: 67.161.139.207     
NNTP-Posting-Host: 207.105.83.66
Message-ID: <41da69f3$1@newsgroups.borland.com>
Date: 4 Jan 2005 02:03:31 -0800
X-Trace: newsgroups.borland.com 1104833011 207.105.83.66 (4 Jan 2005 02:03:31 -0800)
Lines: 68
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail