Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TCPChatClient.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "stdio.h"
- #include "conio.h"
- #include "winsock2.h"
- #include "ws2tcpip.h"
- struct packet
- {
- BYTE head;
- WORD length;
- BYTE data[4096];
- };
- SOCKET c;
- SOCKADDR_IN cAddr;
- DWORD WINAPI FileSender(LPVOID Param) {
- char filename[256],buf[4096];
- int bytesend, ret;
- sprintf(filename, (char*)Param);
- packet pk_send, pk_recv;
- FILE* fp = fopen(filename, "rb");
- fseek(fp, 0, SEEK_END);
- int filesize = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- while (!feof(fp)) {
- //bytesend = fread(buf, 1, 4096, fp);
- bytesend = fread_s(buf, 4096, 1, 4096, fp);
- if (bytesend > 0) {
- pk_send.head = 4;
- pk_send.length = bytesend;
- memcpy(pk_send.data, buf, bytesend);
- ret = send(c, (char*)&pk_send, sizeof(struct packet), 0);
- if (ret == SOCKET_ERROR ) printf("Send Error!\n");
- //Sleep(1);
- }
- }
- fclose(fp); printf("Da gui xong!\n");
- return 0;
- }
- DWORD WINAPI ReceiverThread(LPVOID Param) {
- int len, bytesend;
- char buf[4096], filename[256];
- int filesize, byterecv;
- packet pk_send, pk_recv;
- while (true) {
- len = recv(c, (char*)&pk_recv, sizeof(struct packet), MSG_WAITALL);
- if (pk_recv.head == 0) {
- pk_recv.data[pk_recv.length - 1] = 0;
- printf("%s\n", pk_recv.data);
- continue;
- }
- if (pk_recv.head == 2) {
- strncpy(filename, (char*)pk_recv.data, 255);
- filename[255] = 0;
- printf("Server dong y nhan file %s!\n",filename);
- CreateThread(0, 0, FileSender, (LPVOID)filename, 0, 0);
- continue;
- }
- if (pk_recv.head == 3) {
- printf("Server tu choi gui file!\n");
- continue;
- }
- if (pk_recv.head == 5) {
- continue;
- }
- }
- return 0;
- }
- int main()
- {
- WSADATA wsadata;
- int ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
- c = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- cAddr.sin_family = AF_INET;
- cAddr.sin_port = htons(8888);
- cAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- connect(c, (sockaddr*)&cAddr, sizeof(cAddr));
- char filename[1024], buf[1024];
- int filesize,bytesend;
- FILE* fp;
- packet pk_send, pk_recv;
- CreateThread(0, 0, ReceiverThread, 0, 0, 0);
- while (true) {
- fflush(stdin);
- fgets(buf, 1024, stdin);
- if (strnicmp(buf, "FILE ", 5) == 0) {
- buf[strlen(buf) - 1] = 0;
- char target[32];
- char* pos = NULL;
- pos = strchr(buf + 5, ' ');
- if (pos != 0) {
- strncpy(filename, buf + 5, pos - buf - 5);
- filename[pos - buf - 5] = 0;
- sprintf(target, pos + 1);
- }
- fp = fopen(filename, "rb");
- if (fp != NULL) {
- fseek(fp, 0, SEEK_END);
- filesize = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- fclose(fp);
- pk_send.head = 1;
- pk_send.length = 255 + 32 + 4;
- memcpy(pk_send.data, filename, 255);
- memcpy(pk_send.data + 255, target, 32);
- memcpy(pk_send.data + 255 + 32, &filesize, 4);
- send(c, (char*)&pk_send, sizeof(struct packet), 0);
- }
- else {
- printf("[Error] File not found!\n");
- }
- continue;
- }
- pk_send.head = 0;
- pk_send.length = strlen(buf);
- memcpy(pk_send.data, buf, pk_send.length);
- send(c, (char*)&pk_send, sizeof(struct packet), 0);
- }
- closesocket(c);
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment