首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    socket---TCP服务器界面

    #include<winsock2.h> #pragma comment(lib,"ws2_32.lib") #define DEFAULT_iPort 5000 LRESULT CALLBACK DialProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow) { DialogBox(hInstance,(LPCSTR)IDD_FORMVIEW, NULL,(DLGPROC)DialProc); return 0; } LRESULT CALLBACK DialProc(HWND hDIg, UINT uID, WPARAM wParam, LPARAM lParam) {             char ip[16]; char port[5];//端口号 char recvBuf[1000];//缓存区大小 char sendBuf[1000]; SOCKET sListen, sAccept; SOCKADDR_IN ser, cli; int iLen = sizeof(cli); sListen = socket(AF_INET, SOCK_STREAM,0); sAccept= socket(AF_INET, SOCK_STREAM,0); switch (uID) { case WM_COMMAND://对话框消息 switch(wParam) { case IDC_IPSTART: {    GetDlgItemText(hDIg, IDC_IP,ip, 16); GetDlgItemText(hDIg, IDC_PORT,port, 16); ser.sin_family = AF_INET; ser.sin_addr.s_addr = inet_addr(ip); ser.sin_port = htons(atoi(port)); bind(sListen, (sockaddr *)&ser, sizeof(ser)); listen(sListen, 5); sAccept = accept(sListen, (sockaddr *)&cli, &iLen); if (INVALID_SOCKET == sAccept) { SetDlgItemText(hDIg, IDC_EDIT1, "服务器创建失败"); } else { SetDlgItemText(hDIg, IDC_RECVBUF, "服务器创建成功"); } } break; case IDC_RECVBUF: if (INVALID_SOCKET == sAccept) { } else { recv(sAccept, recvBuf, sizeof(recvBuf), 1000); SetDlgItemText(hDIg, IDC_RECVBUF, recvBuf); } break; case IDC_SENDBUF: GetDlgItemText(hDIg, IDC_SENDBUF, sendBuf, 1000); send(sAccept, sendBuf, sizeof(recvBuf), 1000); SetDlgItemText(hDIg, IDC_SENDBUF, ""); break; case IDC_STOP://关闭窗口  { closesocket(sAccept);   closesocket(sListen);   WSACleanup(); EndDialog(hDIg,wParam);   } break; } break; default: break; }

    02
    领券