C语言实现TCP文件传输(源码讲解)

服务器代码server.c全部代码

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

#pragma comment(lib, "Ws2_32.lib")

int main(void)
{
	//版本为2.2
	WORD wdVersion = MAKEWORD(2, 2);
	WSADATA wdScokMsg;
	int nRes = WSAStartup(wdVersion, &wdScokMsg);

	if (0 != nRes)
	{ 
		switch (nRes)
		{
		case WSASYSNOTREADY:
			printf("重启下电脑试试,或者检查网络库");
			break;
		case WSAVERNOTSUPPORTED:
			printf("请更新网络库");
			break;
		case WSAEINPROGRESS:
			printf("请重新启动");
			break;
		case WSAEPROCLIM:
			printf("请尝试关掉不必要的软件,以为当前网络运行提供充足资源");
			break;
		}

		return 0;
	}

	//校验版本
	if (2 != HIBYTE(wdScokMsg.wVersion) || 2 != LOBYTE(wdScokMsg.wVersion))
	{
		//说明版本不对
		//清理网络库
		WSACleanup();
		return 0;
	}

	//创建自己的socket
	SOCKET socketServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == socketServer)
	{
		int a = WSAGetLastError();
		//清理网络库
		WSACleanup();
		return 0;
	}

	//127.0.0.1为本机地址,12345为本机不被占用的任一端口,推荐10000~20000
	struct sockaddr_in si;
	si.sin_family = AF_INET;
	si.sin_port = htons(12345);
	si.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    //绑定ip和端口号
	if (SOCKET_ERROR == bind(socketServer, (const struct sockaddr*)&si, sizeof(si)))
	{
		int a = WSAGetLastError();
		closesocket(socketServer);
		//清理网络库
		WSACleanup();
		return 0;
	}
	//监听是否有来自客户端的连接
	if (SOCKET_ERROR == listen(socketServer, SOMAXCONN))
	{
		int a = WSAGetLastError();
		closesocket(socketServer);
		//清理网络库
		WSACleanup();
		return 0;
	}

	struct sockaddr_in clientMsg;
	int len = sizeof(clientMsg);
	//接受来自客户端的链接
	SOCKET socketClient = accept(socketServer, (struct sockaddr*)&clientMsg, &len);
	if (INVALID_SOCKET == socketClient)
	{
		printf("客户端连接失败\n");
		int a = WSAGetLastError();
		closesocket(socketServer);
		//清理网络库
		WSACleanup();
		return 0;

	}
	printf("客户端连接成功\n");

	//初始化缓冲区
	char buf [1024]={"0"};
	char path[1024]={"0"};
	FILE*out;
	//接受客户端发来的请求,path内存放客户端所要下载的文件名,用对方socker,故为socketClient
int res = recv(socketClient, path, 50, 0);
		if (0 == res)
		{
			printf("链接中断、客户端下线\n");
		}
		else if (SOCKET_ERROR == res)
		{
			//出错了
			int a = WSAGetLastError();
			//根据实际情况处理
		}
		else
	{	
		//打开要传输的文件
		if((out = fopen(path,"r")) == NULL)
		{
			printf("cannot open this file\n");
		}
		else
		{
			fgets(buf,sizeof(buf),out);
		//将文件内容存放在buf中发送给客户端,用对方socker,故为socketClient
		if (SOCKET_ERROR == send(socketClient, buf, strlen(buf), 0))
		{
			//出错了
			int a = WSAGetLastError();
			//根据实际情况处理

		}
		}
	}
	fclose(out);
	


	//清理网络库
	closesocket(socketClient);
	closesocket(socketServer);
	WSACleanup();

	system("pause");
	return 0;
}

客户端代码client.c全部代码

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#pragma comment(lib, "Ws2_32.lib")

int main(void)
{
	//版本为2.2
	WORD wdVersion = MAKEWORD(2, 2);
	WSADATA wdScokMsg;
	int nRes = WSAStartup(wdVersion, &wdScokMsg);

	if (0 != nRes)
	{
		switch (nRes)
		{
		case WSASYSNOTREADY:
			printf("重启下电脑试试,或者检查网络库");
			break;
		case WSAVERNOTSUPPORTED:
			printf("请更新网络库");
			break;
		case WSAEINPROGRESS:
			printf("请重新启动");
			break;
		case WSAEPROCLIM:
			printf("请尝试关掉不必要的软件,以为当前网络运行提供充足资源");
			break;
		}

		return 0;
	}

	//校验版本
	if (2 != HIBYTE(wdScokMsg.wVersion) || 2 != LOBYTE(wdScokMsg.wVersion))
	{
		//说明版本不对
		//清理网络库
		WSACleanup();
		return 0;
	}

	//创建服务器socket
	SOCKET socketServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == socketServer)
	{
		int a = WSAGetLastError();
		//清理网络库
		WSACleanup();
		return 0;
	}

	//链接服务器
	//127.0.0.1为本机地址,12345为本机不被占用的任一端口,推荐10000~20000
	struct sockaddr_in serverMsg;
	serverMsg.sin_family = AF_INET;
	serverMsg.sin_port = htons(12345);
	serverMsg.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

	//向服务器发送连接请求
	if (SOCKET_ERROR == connect(socketServer, (struct sockaddr*)&serverMsg, sizeof(serverMsg)))
	{
		int a = WSAGetLastError();
		closesocket(socketServer);
		//清理网络库
		WSACleanup();
		return 0;
	}
	//初始化缓冲区
	char path[1024]={"0"};
	char buf[1024]={"0"};


	//向服务器发送要下载的文件名,用对方socker,故为socketServer
	printf("请输入想要下载的文件名:\n");
	gets(path);
		if (SOCKET_ERROR == send(socketServer, path, strlen(path), 0))
		{
			//出错了
			int a = WSAGetLastError();
			//根据实际情况处理
		}

	FILE*in;
	//创建要下载的文件的空白文件用于接受数据
	if((in=fopen(path,"w+"))==NULL)
	{
		printf("无法打开文件\n");
	}
	else
	{
		//接受服务器发来的数据,用对方socker,故为socketServer
		int res = recv(socketServer, buf, 50, 0);
		if (0 == res)
		{
			printf("链接中断、客户端下线\n");
		}
		else if (SOCKET_ERROR == res)
		{
			//出错了
			int a = WSAGetLastError();
			//根据实际情况处理
		}
		else
		{
			printf("%s\n",  buf);
			//将数据写入到文件内
			res = fputs(buf, in);
		if(EOF==res)
		{
			printf("下载文字内容失败\n");
		}
		else
		{
			printf("下载成功\n");
		}
		}
	
	}
	fseek(in,0L,2);
	fclose(in);
	//清理网络库
	closesocket(socketServer);
	WSACleanup();

	system("pause");
	return 0;
}

使用方法

先运行server.c,再运行client.c,在client.c运行窗口输入要下载的文件名,回车

你可能感兴趣的:(c,c语言,tcp/ip,网络)