基于UDP协议的文件传输

原创 2015年07月09日 17:24:31

客户端将本地文件传输给服务端,服务端接收文件并保存,输出文件名和内容

客户端:

#include<stdio.h>
#include<string.h>
#include<Winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main()
{
	WSAData wsaData;
	int err=WSAStartup(WINSOCK_VERSION,&wsaData);
	if(0!=err)
	{
		return -1;
	}
	SOCKET sock;
	sock=socket(AF_INET,SOCK_DGRAM,0);
	if(INVALID_SOCKET==sock)
	{
		printf("socket()Failed:%d\n",WSAGetLastError());
		WSACleanup();
		return -1;
	}
	
	char data[]="Hello World!";
	char dest_ip[]="222.196.201.224";      //目的IP
	unsigned short dest_port=20000;//目的端口
	
	sockaddr_in RemoteAddr;
	RemoteAddr.sin_family = AF_INET;
	RemoteAddr.sin_port = htons(dest_port);
	RemoteAddr.sin_addr.s_addr=inet_addr(dest_ip);
	
	
	char path[100]={"0"};
    while(1)
	{
		printf("请输入文件路径:  (例如D:\\1.txt)\n");
	gets(path);
    FILE* fp=fopen(path,"rb"); // 读写二进制文件 记得 加  b
	
    if(!fp)
    {
        printf("error!");
        return 1;
    }
	
	else
	{
		printf("文件已经打开,等待传输...\n");
	}
	
	char rbuf[1024]={0};
	while(!feof(fp))
	{	
		 sendto(sock,path,strlen(path),0,(sockaddr*)&RemoteAddr,sizeof(RemoteAddr));
		memset(rbuf,0,1024);
		fread(rbuf,1,1024,fp);
		int sByte = sendto(sock,rbuf,strlen(rbuf),0,(sockaddr*)&RemoteAddr,sizeof(RemoteAddr));
		if(SOCKET_ERROR==sByte)
		{
			printf("sendto()Failed:%d\n",WSAGetLastError());
			closesocket(sock);
			WSACleanup();
			return -1;
		}
	}
	fclose(fp);
}
    closesocket(sock);
    WSACleanup();
    return 0;
}

服务端:

#include<stdio.h>
#include<string.h>
#include<Winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define MAX_BUF 65536
int main()
{
	WSAData wsaData;
	int err=WSAStartup(WINSOCK_VERSION,&wsaData);
	if(0!=err)
	{
		return -1;
	}
	SOCKET sock;
	sock=socket(AF_INET,SOCK_DGRAM,0);
	if(INVALID_SOCKET==sock)
	{
		printf("socket()Failed:%d\n",WSAGetLastError());
		WSACleanup();
		return -1;
	}
	
	sockaddr_in LocalAddr;
	LocalAddr.sin_family=AF_INET;
	LocalAddr.sin_port=htons(20000);
	LocalAddr.sin_addr.s_addr=htonl(INADDR_ANY);
	
	err=bind(sock,(sockaddr*)&LocalAddr,sizeof(LocalAddr));
	if(SOCKET_ERROR==err)
	{
		printf("bind()Failed:%d\n",WSAGetLastError());
		closesocket(sock);
		WSACleanup();
		return -1;
	}
	
	char rbuf[MAX_BUF];
	memset(rbuf,0,MAX_BUF);
	sockaddr_in RemoteAddr;
	
	int RemoteLen = sizeof(RemoteAddr);
	
	
	char path[100]={"0"};
	char name[100]={"0"};
	printf("请输入写入文件路径:(例如E:\\2.txt)\n");
	gets(path);
	
    FILE *fp=fopen(path,"wb");
    
    if(!fp)
    {
        printf("打开文件失败!");
        return 1;
    }
	else
	{
		printf("文件已经打开,等待传输...\n");
	}
	
	while(1)
	{	
		recvfrom(sock,name,1024,0,(sockaddr*)&RemoteAddr,&RemoteLen);
		printf("文件名字为:\n %s\n",name);
		memset(rbuf,0,1024);
		int rByte=recvfrom(sock,rbuf,1024,0,(sockaddr*)&RemoteAddr,&RemoteLen);
		
		if(SOCKET_ERROR==rByte)
		{
			printf("recvfrom()Failed:%d\n",WSAGetLastError());
			closesocket(sock);
			WSACleanup();
			return -1;
		}
		fwrite(rbuf,strlen(rbuf),1,fp);
		fseek(fp,0,2);
		printf("写入的文件内容为:\n %s\n",rbuf);
		
	}
fclose(fp);
	
    closesocket(sock);
    WSACleanup();
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

UDP-键盘录入方式数据

import java.net.*; import java.io.*; class  UdpSend2 { public static void main(String[] args) th...

UDP-键盘录入方式数据

//UPD-键盘录入方式数据 import java.net.*; import java.io.*;class UdpSend2 { public static void main(Str...

tcp udp实现文件传输(java)

tcp    package org.tcp;import java.io.BufferedInputStream;   import java.io.DataInputStream;   i...

单个文件传输UDP协议

  • 2012-08-20 21:46
  • 3.23MB
  • 下载

java UDP聊天与文件传输

package rgy.com.UDP3; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.Actio...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)