windows网络编程(四)——不同主机之间传送文件应用

客户端



#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\n",WSAGetLastError());
		WSACleanup();
	//	exit(1);
	}
	//接收服务器传回的数据
	char szBuffer[MAXBYTE] = "";
	//send(sock, szBuffer, MAXBYTE, NULL);
	if (recv(sock, szBuffer, MAXBYTE, NULL) == SOCKET_ERROR)
	{
		printf("接受失败!\n"); closesocket(sock); WSACleanup(); return 0;
	}
	//输出接收到的数据
	printf("Message form server: %s\n", szBuffer);
	char YN[10];
	scanf("%s", YN);//是否传输
	if (send(sock, YN, 2, NULL) == SOCKET_ERROR)
	{
		printf("发送失败!\n"); closesocket(sock); WSACleanup(); return 0;
	}
	int  len;
	if ((len=recv(sock, szBuffer, MAXBYTE, NULL)) == SOCKET_ERROR)
	{
		printf("接受失败!\n"); closesocket(sock); WSACleanup(); return 0;
	}
	printf("Message form server: %s\n", szBuffer);
	scanf("%s", YN);//发送类型
	if (send(sock, YN, 5, NULL) == SOCKET_ERROR)
	{
		printf("发送失败!\n"); closesocket(sock); WSACleanup(); return 0;
	}
	if ((len = recv(sock, szBuffer, MAXBYTE, NULL)) == SOCKET_ERROR)
	{
		printf("接受失败!\n"); closesocket(sock); WSACleanup(); return 0;
	}
	printf("Message form server: %s\n", szBuffer);
	char filename[50];
	printf( "输入待发送的文件名:");
	scanf("%s", filename);
	strcat(fullname, filename);
	FILE*p = fopen(fullname, "rb+");
	if (!p)
	{
		printf("打开文件失败!\n"); 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!\n");
			continue;
		}
		if (send(clntSock, str, strlen(str) + sizeof(char), NULL) == SOCKET_ERROR)
		{
			printf("发送失败\n"); closesocket(clntSock); continue;
		}
		else
		{
			printf("已成功发送一条回复!\n");
		}
		if (recv(clntSock, str, 100, NULL) == SOCKET_ERROR)
		{
			printf("接受失败!\n"); 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("发送失败!\n"); closesocket(clntSock); continue;
		}
		if (recv(clntSock, type, 100, NULL) == SOCKET_ERROR)//收到类型
		{
			printf("接受失败!\n"); 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("发送失败!\n"); closesocket(clntSock); continue;
		}
		char filename[50];//文件名
		itoa(time(0), filename, 10);
		strcat(filename, type);
		strcat(baseicpath, filename);//完整名
		p = fopen(baseicpath, "wb+");
		if (!p){ printf("文件打开失败!\n"); 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;
}




你可能感兴趣的:(windows,C语言,网络编程)