Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <windows.h>
- #include <stdio.h>
- #include <tchar.h>
- //#include <winsock2.h>
- #include <string>
- #pragma comment (lib, "ws2_32.lib")
- #define SRV_IP "192.168.1.6"
- #define SRV_PORT 44000
- #define BUFFERSIZE 2000000
- #define filePath _T("C:\\Users\\einst\\Documents\\Cpp\\Log.txt")
- using namespace std;
- typedef struct {
- char msg[BUFFERSIZE];
- LPSYSTEMTIME time;
- } sendMsg;
- typedef struct
- {
- SOCKET sock;
- } THREAD_PARAM;
- DWORD g_BytesTransferred = 0;
- int sendall(CHAR *msg, int len, SOCKET sock) {
- CHAR *buf = msg;
- int remained = len;
- int error = 0, count;
- while (remained && !error) {
- count = send(sock, buf, remained, 0);
- error = (count == 0);
- remained -= count;
- buf += count;
- }
- return error;
- }
- SOCKET ConnectToServer(char *ip, USHORT port) {
- //Init winsock
- WORD wVersionRequested = MAKEWORD(2, 0);
- WSADATA wsaData;
- int err;
- err = WSAStartup(wVersionRequested, &wsaData);
- if (err)
- return INVALID_SOCKET;
- //Create socket
- SOCKET sock;
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock == INVALID_SOCKET)
- return INVALID_SOCKET;
- //Connect to the server
- struct sockaddr_in saServer;
- saServer.sin_family = AF_INET;
- saServer.sin_addr.s_addr = inet_addr(ip);
- saServer.sin_port = htons(port);
- err = connect(sock, (sockaddr*)&saServer, sizeof(saServer));
- if (err)
- return INVALID_SOCKET;
- return sock;
- }
- void SendFile(SOCKET sock) {
- HANDLE hFile = CreateFile(filePath,
- GENERIC_READ,
- 0,
- NULL,
- OPEN_ALWAYS,
- FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
- 0);
- sendMsg readBuffer;
- GetLocalTime(readBuffer.time);
- sendall((char*)&readBuffer.msg, sizeof(sendMsg), sock);
- CloseHandle(hFile);
- }
- DWORD WINAPI ClientThread(void *pParam) {
- THREAD_PARAM *pInfo = (THREAD_PARAM*)pParam;
- SendFile(pInfo->sock);
- return 0;
- }
- int main() {
- HANDLE hThread;
- THREAD_PARAM tp;
- SOCKET sock;
- sock = ConnectToServer(SRV_IP, SRV_PORT);
- if (sock != INVALID_SOCKET) {
- tp.sock = sock;
- hThread = CreateThread(NULL, 0, ClientThread, &tp, 0, NULL);
- }
- closesocket(sock);
- WSACleanup();
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement