tuanln

TCPChatClient

Nov 18th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. // TCPChatClient.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "stdio.h"
  6. #include "conio.h"
  7. #include "winsock2.h"
  8. #include "ws2tcpip.h"
  9.  
  10. struct packet
  11. {
  12.     BYTE head;
  13.     WORD length;
  14.     BYTE data[4096];
  15. };
  16.  
  17. SOCKET c;
  18. SOCKADDR_IN cAddr;
  19.  
  20.  
  21. DWORD WINAPI FileSender(LPVOID Param) {
  22.     char filename[256],buf[4096];
  23.     int bytesend, ret;
  24.     sprintf(filename, (char*)Param);
  25.     packet pk_send, pk_recv;
  26.     FILE* fp = fopen(filename, "rb");
  27.     fseek(fp, 0, SEEK_END);
  28.     int filesize = ftell(fp);
  29.     fseek(fp, 0, SEEK_SET);
  30.  
  31.     while (!feof(fp)) {
  32.         //bytesend = fread(buf, 1, 4096, fp);
  33.         bytesend = fread_s(buf, 4096, 1, 4096, fp);
  34.         if (bytesend > 0) {
  35.             pk_send.head = 4;
  36.             pk_send.length = bytesend;
  37.             memcpy(pk_send.data, buf, bytesend);
  38.             ret = send(c, (char*)&pk_send, sizeof(struct packet), 0);
  39.             if (ret == SOCKET_ERROR ) printf("Send Error!\n");
  40.             //Sleep(1);    
  41.         }
  42.     }
  43.     fclose(fp); printf("Da gui xong!\n");
  44.     return 0;
  45. }
  46.  
  47. DWORD WINAPI ReceiverThread(LPVOID Param) {
  48.     int len, bytesend;
  49.     char buf[4096], filename[256];
  50.     int filesize, byterecv;
  51.     packet pk_send, pk_recv;
  52.  
  53.     while (true) {
  54.         len = recv(c, (char*)&pk_recv, sizeof(struct packet), MSG_WAITALL);
  55.         if (pk_recv.head == 0) {
  56.             pk_recv.data[pk_recv.length - 1] = 0;
  57.             printf("%s\n", pk_recv.data);
  58.             continue;
  59.         }
  60.         if (pk_recv.head == 2) {
  61.             strncpy(filename, (char*)pk_recv.data, 255);
  62.             filename[255] = 0;
  63.             printf("Server dong y nhan file %s!\n",filename);
  64.             CreateThread(0, 0, FileSender, (LPVOID)filename, 0, 0);
  65.             continue;
  66.         }
  67.         if (pk_recv.head == 3) {
  68.             printf("Server tu choi gui file!\n");
  69.             continue;
  70.         }
  71.         if (pk_recv.head == 5) {
  72.            
  73.             continue;
  74.         }
  75.     }
  76.  
  77.     return 0;
  78. }
  79.  
  80. int main()
  81. {
  82.     WSADATA wsadata;
  83.     int ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
  84.  
  85.     c = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  86.  
  87.     cAddr.sin_family = AF_INET;
  88.     cAddr.sin_port = htons(8888);
  89.     cAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  90.  
  91.     connect(c, (sockaddr*)&cAddr, sizeof(cAddr));
  92.  
  93.     char filename[1024], buf[1024];
  94.     int filesize,bytesend;
  95.     FILE* fp;
  96.     packet pk_send, pk_recv;
  97.  
  98.     CreateThread(0, 0, ReceiverThread, 0, 0, 0);
  99.     while (true) {
  100.         fflush(stdin);
  101.         fgets(buf, 1024, stdin);
  102.         if (strnicmp(buf, "FILE ", 5) == 0) {
  103.             buf[strlen(buf) - 1] = 0;
  104.             char target[32];
  105.             char* pos = NULL;
  106.             pos = strchr(buf + 5, ' ');
  107.             if (pos != 0) {
  108.                 strncpy(filename, buf + 5, pos - buf - 5);
  109.                 filename[pos - buf - 5] = 0;
  110.                 sprintf(target, pos + 1);
  111.             }
  112.             fp = fopen(filename, "rb");
  113.             if (fp != NULL) {
  114.                 fseek(fp, 0, SEEK_END);
  115.                 filesize = ftell(fp);
  116.                 fseek(fp, 0, SEEK_SET);
  117.                 fclose(fp);
  118.                 pk_send.head = 1;
  119.                 pk_send.length = 255 + 32 + 4;
  120.                 memcpy(pk_send.data, filename, 255);
  121.                 memcpy(pk_send.data + 255, target, 32);
  122.                 memcpy(pk_send.data + 255 + 32, &filesize, 4);
  123.                 send(c, (char*)&pk_send, sizeof(struct packet), 0);
  124.             }
  125.             else {
  126.                 printf("[Error] File not found!\n");
  127.             }
  128.             continue;
  129.         }
  130.         pk_send.head = 0;
  131.         pk_send.length = strlen(buf);
  132.         memcpy(pk_send.data, buf, pk_send.length);
  133.         send(c, (char*)&pk_send, sizeof(struct packet), 0);
  134.     }
  135.     closesocket(c);
  136.     WSACleanup();
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment