티스토리 뷰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #include <WinSock2.h> #include <Windows.h> #include <stdio.h> #pragma comment(lib, "WS2_32.lib") #include "resource.h" #define WM_NETWORK (WM_USER + 1) HWND hDlgMain; SOCKET Socket; sockaddr_in Address; char ListStr[256]; BOOL CALLBACK MainDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), HWND_DESKTOP, MainDlgProc); return 0; } BOOL CALLBACK MainDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam) { int res; WSAData wsaData; switch (iMessage) { case WM_INITDIALOG: case WM_CREATE: WSAStartup(MAKEWORD(2,2), &wsaData); Socket = socket(PF_INET, SOCK_STREAM, 0); Address.sin_family = AF_INET; Address.sin_addr.s_addr = inet_addr(/*"Ip Address"*/); Address.sin_port = htons(/*port*/); if(((res = connect(Socket, (sockaddr*)&Address, sizeof(Address))) < 0)) { SendMessage(GetDlgItem(hDlg, IDC_LIST1), LB_ADDSTRING, NULL, (LPARAM)TEXT("접속불가")); break; } else { SendMessage(GetDlgItem(hDlg, IDC_LIST1), LB_ADDSTRING, NULL, (LPARAM)TEXT("접속가능")); WSAAsyncSelect(Socket, hDlg, WM_NETWORK, FD_READ | FD_CLOSE); } break; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: break; case IDCANCEL: break; } break; case WM_NETWORK: if(WSAGETSELECTERROR(lParam)) SendMessage(hDlg, WM_CLOSE,NULL, NULL); else { switch (WSAGETSELECTEVENT(lParam)) { case FD_READ: res = recv(Socket, /**/, sizeof(/**/), 0); if(res) { } break; case FD_CLOSE: SendMessage(hDlg,WM_CLOSE,NULL,NULL); break; } } break; case WM_CLOSE: if(Socket != INVALID_SOCKET) closesocket(Socket); WSACleanup(); EndDialog(hDlg,0); PostQuitMessage(0); break; } return 0; } | cs |
'Network Programming > C & C++' 카테고리의 다른 글
select기반 Server (0) | 2017.10.23 |
---|---|
[server]자신의 IP 알아보기 (0) | 2016.06.23 |
socket_server (0) | 2015.06.18 |
댓글