windows 네트워크 프로 그래 밍 (4) - 서로 다른 호스트 간 전송 파일 응용

클 라 이언 트
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")  //   ws2_32.dll
int main(){
	char fullname[200] = "C:\\Users\\Administrator\\Desktop\\";


	//   DLL
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);
	//     
	SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	//        
	sockaddr_in sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));  //      0  
	sockAddr.sin_family = PF_INET;
	sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	sockAddr.sin_port = htons(1234);

	
	if (connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR)) == SOCKET_ERROR)
	{
		closesocket(sock);
		printf("error connect!  %d
",WSAGetLastError()); WSACleanup(); // exit(1); } // char szBuffer[MAXBYTE] = ""; //send(sock, szBuffer, MAXBYTE, NULL); if (recv(sock, szBuffer, MAXBYTE, NULL) == SOCKET_ERROR) { printf(" !
"); closesocket(sock); WSACleanup(); return 0; } // printf("Message form server: %s
", szBuffer); char YN[10]; scanf("%s", YN);// if (send(sock, YN, 2, NULL) == SOCKET_ERROR) { printf(" !
"); closesocket(sock); WSACleanup(); return 0; } int len; if ((len=recv(sock, szBuffer, MAXBYTE, NULL)) == SOCKET_ERROR) { printf(" !
"); closesocket(sock); WSACleanup(); return 0; } printf("Message form server: %s
", szBuffer); scanf("%s", YN);// if (send(sock, YN, 5, NULL) == SOCKET_ERROR) { printf(" !
"); closesocket(sock); WSACleanup(); return 0; } if ((len = recv(sock, szBuffer, MAXBYTE, NULL)) == SOCKET_ERROR) { printf(" !
"); closesocket(sock); WSACleanup(); return 0; } printf("Message form server: %s
", szBuffer); char filename[50]; printf( " :"); scanf("%s", filename); strcat(fullname, filename); FILE*p = fopen(fullname, "rb+"); if (!p) { printf(" !
"); closesocket(sock); WSACleanup(); return 0; } while ((len=fread(szBuffer, 1, 99, p))>0) { send(sock, szBuffer, len, NULL); } fclose(p); // shutdown(sock, SD_SEND); closesocket(sock); // DLL WSACleanup(); system("pause"); return 0; }

서버 쪽
#include <stdio.h>
#include<iostream>
#include <winsock2.h>
#include<string>
#include<ctime>
using namespace std;
#pragma comment (lib, "ws2_32.lib")  //   ws2_32.dll
int main(){
	//    DLL
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);
	//     
	SOCKET servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	//     
	sockaddr_in sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));  //      0  
	sockAddr.sin_family = PF_INET;  //  IPv4  
	sockAddr.sin_addr.s_addr = INADDR_ANY;  //   IP  
	sockAddr.sin_port = htons(1234);  //  
	bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
	//      
	listen(servSock, 20);
	//       
	while (1)
	{
		FILE*p;
		char type[100];
		char baseicpath[200] = "C:\\Users\\Administrator\\Desktop\\receivefiles\\";
		char *fullpath[200];
		int len;
		SOCKADDR clntAddr;
		int nSize = sizeof(SOCKADDR);
		SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize);
		//        
		char str[100] = "Are you sending a file?(sending Y/N)";
		if (clntSock == INVALID_SOCKET)
		{
			printf("  socket!
"); continue; } if (send(clntSock, str, strlen(str) + sizeof(char), NULL) == SOCKET_ERROR) { printf("
"); closesocket(clntSock); continue; } else { printf(" !
"); } if (recv(clntSock, str, 100, NULL) == SOCKET_ERROR) { printf(" !
"); closesocket(clntSock); continue; } switch (str[0]) { case 'Y': case 'y': // {strcpy(str, "please note the type of your file(.txt/.pdf/.PNG——only three type can be supported)"); if (send(clntSock, str, strlen(str) + sizeof(char), NULL) == SOCKET_ERROR)// { printf(" !
"); closesocket(clntSock); continue; } if (recv(clntSock, type, 100, NULL) == SOCKET_ERROR)// { printf(" !
"); closesocket(clntSock); continue; } strcpy(str, "please send your file(s)!I'm ready!"); if (send(clntSock, str, strlen(str) + sizeof(char), NULL) == SOCKET_ERROR)// { printf(" !
"); closesocket(clntSock); continue; } char filename[50];// itoa(time(0), filename, 10); strcat(filename, type); strcat(baseicpath, filename);// p = fopen(baseicpath, "wb+"); if (!p){ printf(" !
"); closesocket(clntSock); continue; } int len; while ((len = recv(clntSock, str, 99, NULL)) >0) { fwrite(str, 1, len, p); } break; } case'N': case'n': { break; } break; } shutdown(clntSock, SD_RECEIVE); fclose(p); // closesocket(clntSock); } closesocket(servSock); // DLL WSACleanup(); system("pause"); return 0; }

좋은 웹페이지 즐겨찾기