Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. //#include <winsock2.h>
  6. #include <string>
  7. #pragma comment (lib, "ws2_32.lib")
  8. #define SRV_IP "192.168.1.6"
  9. #define SRV_PORT 44000
  10. #define BUFFERSIZE 2000000
  11. #define filePath _T("C:\\Users\\einst\\Documents\\Cpp\\Log.txt")
  12. using namespace std;
  13. typedef struct {
  14. char msg[BUFFERSIZE];
  15. LPSYSTEMTIME time;
  16. } sendMsg;
  17.  
  18. typedef struct
  19. {
  20. SOCKET sock;
  21. } THREAD_PARAM;
  22.  
  23. DWORD g_BytesTransferred = 0;
  24.  
  25. int sendall(CHAR *msg, int len, SOCKET sock) {
  26. CHAR *buf = msg;
  27. int remained = len;
  28. int error = 0, count;
  29. while (remained && !error) {
  30. count = send(sock, buf, remained, 0);
  31. error = (count == 0);
  32. remained -= count;
  33. buf += count;
  34. }
  35. return error;
  36. }
  37.  
  38. SOCKET ConnectToServer(char *ip, USHORT port) {
  39. //Init winsock
  40. WORD wVersionRequested = MAKEWORD(2, 0);
  41. WSADATA wsaData;
  42. int err;
  43. err = WSAStartup(wVersionRequested, &wsaData);
  44. if (err)
  45. return INVALID_SOCKET;
  46.  
  47. //Create socket
  48. SOCKET sock;
  49. sock = socket(AF_INET, SOCK_STREAM, 0);
  50. if (sock == INVALID_SOCKET)
  51. return INVALID_SOCKET;
  52.  
  53. //Connect to the server
  54. struct sockaddr_in saServer;
  55. saServer.sin_family = AF_INET;
  56. saServer.sin_addr.s_addr = inet_addr(ip);
  57. saServer.sin_port = htons(port);
  58. err = connect(sock, (sockaddr*)&saServer, sizeof(saServer));
  59. if (err)
  60. return INVALID_SOCKET;
  61.  
  62. return sock;
  63. }
  64.  
  65. void SendFile(SOCKET sock) {
  66. HANDLE hFile = CreateFile(filePath,
  67. GENERIC_READ,
  68. 0,
  69. NULL,
  70. OPEN_ALWAYS,
  71. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  72. 0);
  73. sendMsg readBuffer;
  74. GetLocalTime(readBuffer.time);
  75.  
  76. sendall((char*)&readBuffer.msg, sizeof(sendMsg), sock);
  77.  
  78. CloseHandle(hFile);
  79. }
  80.  
  81. DWORD WINAPI ClientThread(void *pParam) {
  82. THREAD_PARAM *pInfo = (THREAD_PARAM*)pParam;
  83. SendFile(pInfo->sock);
  84. return 0;
  85. }
  86.  
  87. int main() {
  88. HANDLE hThread;
  89. THREAD_PARAM tp;
  90. SOCKET sock;
  91. sock = ConnectToServer(SRV_IP, SRV_PORT);
  92. if (sock != INVALID_SOCKET) {
  93. tp.sock = sock;
  94. hThread = CreateThread(NULL, 0, ClientThread, &tp, 0, NULL);
  95. }
  96.  
  97. closesocket(sock);
  98. WSACleanup();
  99. getchar();
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement