Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <WinSock2.h>
  2. #include <WS2tcpip.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. constexpr unsigned int BUFSIZE = 512;
  7. #pragma comment(lib, "ws2_32.lib")
  8.  
  9. // 소켓 함수 오류 출력 후 종료
  10. void err_quit(char* msg)
  11. {
  12. LPVOID lpMsgBuf;
  13.  
  14. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  15. NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  16. (LPTSTR)&lpMsgBuf, 0, NULL);
  17.  
  18. MessageBox(NULL, (LPCTSTR)lpMsgBuf, (LPCWSTR)msg, MB_ICONERROR);
  19.  
  20. LocalFree(lpMsgBuf);
  21.  
  22. exit(-1);
  23. }
  24.  
  25. // 소켓 함수 오류 출력
  26. void err_display(char* msg)
  27. {
  28. LPVOID lpMsgBuf;
  29.  
  30. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  31. NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  32. (LPTSTR)&lpMsgBuf, 0, NULL);
  33.  
  34. printf("[%s] %ws", msg, (LPCTSTR)lpMsgBuf);
  35.  
  36. LocalFree(lpMsgBuf);
  37. }
  38.  
  39. int wmain(int argc, char* argv[])
  40. {
  41. int retval;
  42.  
  43. // 윈속 초기화
  44. WSADATA wsa;
  45.  
  46. if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  47. {
  48. return -1;
  49. }
  50.  
  51. // socket()
  52. SOCKET listen_sock = socket(AF_INET, SOCK_STREAM, 0);
  53.  
  54. if (listen_sock == INVALID_SOCKET)
  55. {
  56. err_quit("socket()");
  57. }
  58.  
  59. // bind()
  60. SOCKADDR_IN serveraddr;
  61. ZeroMemory(&serveraddr, sizeof(serveraddr));
  62. serveraddr.sin_family = AF_INET;
  63. serveraddr.sin_port = htons(9000);
  64. serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  65.  
  66. retval = bind(listen_sock, (SOCKADDR*)&serveraddr, sizeof(serveraddr));
  67.  
  68. if (retval == SOCKET_ERROR)
  69. {
  70. err_quit("bind()");
  71. }
  72.  
  73. // listen()
  74. retval = listen(listen_sock, SOMAXCONN);
  75.  
  76. if (retval == SOCKET_ERROR)
  77. {
  78. err_quit("listen()");
  79. }
  80.  
  81. while (true)
  82. {
  83. wprintf(L"Start!\n");
  84.  
  85. // 데이터 통신에 사용할 변수
  86. SOCKADDR_IN clientaddr;
  87. int addrlen = sizeof(clientaddr);
  88.  
  89. // accept()
  90. SOCKET client_sock = accept(listen_sock, (SOCKADDR*)&clientaddr, &addrlen);
  91.  
  92. wprintf(L"accept!\n");
  93.  
  94. if (client_sock == INVALID_SOCKET)
  95. {
  96. err_display("accept()");
  97. continue;
  98. }
  99.  
  100. WCHAR addrBuf[BUFSIZE + 1];
  101. InetNtop(AF_INET, &clientaddr, (PWSTR)& addrBuf, BUFSIZE);
  102.  
  103. wprintf(L"\n[TCP Server] Client Connect : IP = %s, Port = %d\n",
  104. addrBuf, ntohs(clientaddr.sin_port));
  105.  
  106.  
  107. // 클라이언트와 데이터 통신
  108. while (true)
  109. {
  110. char buf[BUFSIZE + 1];
  111.  
  112. // 데이터 받기
  113. retval = recv(client_sock, buf, BUFSIZE, 0);
  114.  
  115. if (retval == SOCKET_ERROR)
  116. {
  117. err_display("recv()");
  118. break;
  119. }
  120. else if (retval == 0)
  121. {
  122. break;
  123. }
  124.  
  125. // 받은 데이터 출력
  126. buf[retval] = '\0';
  127. printf("%s", buf);
  128. }
  129.  
  130. // closesocket()
  131. closesocket(client_sock);
  132.  
  133. wprintf(L"\n[TCP Server] Client Disconnect : IP = %s, Port = %d\n",
  134. addrBuf, ntohs(clientaddr.sin_port));
  135. }
  136.  
  137. // closesocket()
  138. closesocket(listen_sock);
  139.  
  140. // 윈속 종료
  141. WSACleanup();
  142.  
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement