#include "resource.h"
#include <windows.h>
#include <thread>
#pragma comment(lib,"ws2_32.lib")
#define BINDOK 10001
#define STOP 10002
#define TIPS 10003
#define binderror 1000
bool b = 0;
struct SET
{
int port1;
int port2;
int port3;
char ip[16];
};
void work(HWND hwnd, SET set);
BOOL WINAPI DlgProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_CLOSE:
{
EndDialog(hwnd, IDCANCEL);
return true;
}
case WM_INITDIALOG:
{
FILE* pf = fopen("set.cfg", "r");
if (pf == NULL) break;
char buf[30] = { 0 };
fgets(buf, 30, pf);
SetDlgItemText(hwnd, IDC_EDIT1, buf);
memset(buf, 0, 30);
fgets(buf, 30, pf);
SetDlgItemText(hwnd, IDC_IPADDRESS1, buf);
memset(buf, 0, 30);
fgets(buf, 30, pf);
SetDlgItemText(hwnd, IDC_EDIT2, buf);
memset(buf, 0, 30);
fgets(buf, 30, pf);
SetDlgItemText(hwnd, IDC_EDIT3, buf);
memset(buf, 0, 30);
fclose(pf);
pf = NULL;
EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0);
break;
}
case WM_COMMAND:
{
int con_id = LOWORD(wparam);
switch (con_id)
{
case IDOK:
{
if (b == 1) break;
b = 1;
SET set = { 0 };
GetDlgItemText(hwnd, IDC_IPADDRESS1, set.ip, 16);
set.port1 = GetDlgItemInt(hwnd, IDC_EDIT1, NULL, 1);
set.port2 = GetDlgItemInt(hwnd, IDC_EDIT2, NULL, 1);
set.port3 = GetDlgItemInt(hwnd, IDC_EDIT3, NULL, 1);
FILE* pf = fopen("set.cfg", "w");
fprintf(pf, "%d\n%s\n%d\n%d", set.port1, set.ip, set.port2, set.port3);
fclose(pf);
pf = NULL;
std::thread t1(work,hwnd, set);
t1.detach();
break;
}
case IDCANCEL:
{
b = 0;
}
}
break;
}
case BINDOK:
{
EnableWindow(GetDlgItem(hwnd, IDC_EDIT1),0);
EnableWindow(GetDlgItem(hwnd, IDC_EDIT2), 0);
EnableWindow(GetDlgItem(hwnd, IDC_EDIT3), 0);
EnableWindow(GetDlgItem(hwnd, IDC_IPADDRESS1), 0);
EnableWindow(GetDlgItem(hwnd, IDOK), 0);
EnableWindow(GetDlgItem(hwnd, IDCANCEL), 1);
SetDlgItemText(hwnd, IDC_EDIT4, "正在转发数据");
break;
}
case STOP:
{
EnableWindow(GetDlgItem(hwnd, IDC_EDIT1), 1);
EnableWindow(GetDlgItem(hwnd, IDC_EDIT2), 1);
EnableWindow(GetDlgItem(hwnd, IDC_EDIT3), 1);
EnableWindow(GetDlgItem(hwnd, IDC_IPADDRESS1), 1);
EnableWindow(GetDlgItem(hwnd, IDOK), 1);
EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0);
SetDlgItemText(hwnd, IDC_EDIT4, "已停止转发");
break;
}
case TIPS:
{
if (wparam == binderror)
{
char tips[30] = { 0 };
sprintf(tips, "绑定UDP:%d端口失败", lparam);
SetDlgItemText(hwnd, IDC_EDIT4, tips);
}
}
break;
}
return FALSE;
}
int WINAPI WinMain(HINSTANCE h1, HINSTANCE h2, LPSTR l3, INT i4)
{
WSADATA vs;
WSAStartup(MAKEWORD(2, 2), &vs);
DialogBox(h1, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
WSACleanup();
return 0;
}
void work(HWND hwnd, SET set)
{
SOCKET udp0 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
int nTimeout = 1000;
//设置接收超时为1000ms
if (SOCKET_ERROR == setsockopt(udp0, SOL_SOCKET, SO_RCVTIMEO, (char *)&nTimeout, sizeof(int)))
{
return;
}
SOCKADDR_IN net0;
net0.sin_family = AF_INET;
net0.sin_addr.S_un.S_addr = INADDR_ANY;
net0.sin_port = htons(set.port1);
if (bind(udp0, (SOCKADDR*)&net0, sizeof(net0)) == SOCKET_ERROR)
{
closesocket(udp0);
SendMessage(hwnd, TIPS, binderror, set.port1);
return;
}
SOCKET udp1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKADDR_IN net1;
net1.sin_family = AF_INET;
net1.sin_addr.S_un.S_addr = INADDR_ANY;
net1.sin_port = htons(set.port2);
if (bind(udp1, (SOCKADDR*)&net1, sizeof(net1)) == SOCKET_ERROR)
{
SendMessage(hwnd, TIPS, binderror, set.port2);
closesocket(udp0);
closesocket(udp1);
return;
}
SOCKADDR_IN net2;
net2.sin_family = AF_INET;
net2.sin_addr.S_un.S_addr = inet_addr(set.ip);
net2.sin_port = htons(set.port3);
SendMessage(hwnd, BINDOK, 0, 0);
while (b)
{
char data[256] = { 0 };
int ret = recv(udp0, data, 256, 0);
if (ret > 0)
{
sendto(udp1, data, ret, 0, (SOCKADDR*)&net2, sizeof(net2));
}
}
SendMessage(hwnd, STOP, 0, 0);
closesocket(udp0);
closesocket(udp1);
return;
}