Reputation: 1
I'm making a program with Code::Blocks in C++ To Send and Receive data with Winsocks but when I try to receive data my program freezes, please help. I don't know why? I was able to get just 1 line without using a loop so I added a loop to see if I could get more lines that come in, and now it just freezes.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "resource.h"
#include <winsock.h>
using namespace std;
SOCKET mysocket;
SOCKADDR_IN SockAddr;
char buf1[120];
char buf2[120];
char ReadIp[120];
int UsePort;
int n;
HINSTANCE hInst;
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
WSADATA WsaDat;
WSAStartup(MAKEWORD(1,1), &WsaDat);
mysocket = socket(AF_INET, SOCK_STREAM, 0);
return TRUE;
case WM_CLOSE:
EndDialog(hwndDlg, 0);
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BTN_QUIT:
EndDialog(hwndDlg, 0);
return TRUE;
case BtnSend:
GetDlgItemText(hwndDlg, PacketText, buf2, sizeof(buf2));
send(mysocket, buf2, sizeof(buf2), 0);
return TRUE;
case IDC_BTN_TEST:
UsePort = GetDlgItemInt(hwndDlg, PortText, NULL, FALSE);
GetDlgItemText(hwndDlg, IpText, ReadIp, sizeof(ReadIp));
SockAddr.sin_port = htons(UsePort);
SockAddr.sin_addr.s_addr=inet_addr(ReadIp);
SockAddr.sin_family = AF_INET;
connect(mysocket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr));
do {
n = recv(mysocket, buf1, sizeof(buf1), 0);
if (n > 0)
SetDlgItemText(hwndDlg, List1, buf1);
} while (n > 0);
return TRUE;
}
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst = hInstance;
// The user interface is a modal dialog box
return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}
Upvotes: 0
Views: 572
Reputation: 182789
It freezes because it's waiting for data. If you don't want to wait for data, don't make a Winsock call that does that. Use one of the many non-blocking I/O methods available on Windows such as Async operations, I/O completion ports or completion events.
By the way, your code seems badly broken in many ways. It seems to be missing a protocol. You can't use TCP without designing and implementing a protocol on top of TCP. You will get totally unreliable behavior if you don't process the data you've received through some sort of protocol engine that reconstructs the transmitted information. For example, in your case, if someone sends "foo", the final text will depend on how the data got assembled for transport. It might be "foo", but it might be "o" since the "fo" can be overwritten.
If the other side is sending lines, as you claim, you need to code this end to receive lines. That is, you need to keep receiving data until you have a line and not let later receive overwrite earlier receives so that you can re-assemble the transmitted line. The code to actually implement the receiving of lines is totally missing in the code you pasted. (If, for example, a "line" is defined as a bunch of bytes terminated by a newline character, there needs to be code to search for the newline characters and assemble everything before them into a single buffer for processing.)
Upvotes: 1