自己编的socket文件传输小程序。
来源:互联网 发布:电极编程怎么给负余量 编辑:程序博客网 时间:2024/05/01 14:08
传输端:
#include <stdio.h>
#include <iostream>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define command "clear"
#include <errno.h>
#define big_size 2048
#define small_size 50
using namespace std;
int main(void)
{
char c=0,buf[big_size],file[small_size],host_name[small_size];
int fromlen=0,source=0;
int k,s,ns;
struct sockaddr_in sin;
struct hostent * he;
system(command);
cout<<"enter filename:"<<endl;
memset(file,0,small_size);
cin>>file;
if ((source=open(file, O_RDONLY|O_SYNC) ) < 0)
{
perror("open");
exit(1);
}
memset(host_name, 0, small_size);
sprintf(host_name, "%s", "localhost");
if((he=gethostbyname(host_name))==NULL)
{
perror("gethostbyname");
exit(2);
}
cout<<"gethostbyname"<<endl;
if((s=socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket");
exit(3);
}
cout<<"socket"<<endl;
sin.sin_family=AF_INET;
sin.sin_port=htons(21);
memcpy(&sin.sin_addr,he->h_addr,he->h_length);
if(bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0 )
{
printf("errno is %d/n",errno);
printf("errno`s msg is %s/n",strerror(errno));
printf("prot aready be use");
exit(4);
}
cout<<"bind"<<endl;
if(listen(s,5) < 0)
{
fprintf(stderr, "sever:listen/n");
exit(5);
}
cout<<"listen"<<endl;
while(1)
{
cout<<("waiting....")<<endl;
if((ns=accept(s, NULL, NULL)) < 0 )
{
fprintf(stderr, "sever:accept/n");
exit(6);
}
cout<<"accept"<<endl;
lseek(source,0L,0);
cout<<"transmiting..."<<endl;
write(ns,file,sizeof(file));
cout<< file<<endl;
memset(buf, 0, big_size);
while((k = read(source, buf, sizeof(buf))) > 0)
{
write(ns,buf,k);
memset(buf, 0, big_size);
}
cout<<"transmit down"<<endl;
close(ns);
}
close(source);
exit(0);
}
接受端:
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>
#define command "clear"
#include <unistd.h>
#include <errno.h>
#include <string.h>
#define big_size 1024
#define small_size 50
using namespace std;
int main(int argc,char*argv[])
{
if(argc!=2)
{
cout<<"please enter new file pathname"<<endl;
exit(1);
}
cout<<"your pathname for new file is :"<<argv[1]<<endl;
char buf[big_size]={0},file[small_size]={0},host_name[small_size]={0};
int target,ret;
int bytes,sockfd;
struct sockaddr_in sin ;
struct hostent *h;
system(command);
sprintf(host_name, "%s", "localhost");
if((h=gethostbyname(host_name))==NULL)
{
perror("gethostbyname");
exit(1);
}
cout<<"gethostbyname"<<endl;
if((sockfd=socket(AF_INET, SOCK_STREAM, 0 )) < 0)
{
perror("socket");
exit(2);
}
cout<<"socket"<<endl;
memset((char*)&sin, '/0', sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_port=htons(1500);
sin.sin_addr=*(struct in_addr*)h->h_addr;
if((ret=connect(sockfd, (struct sockaddr*)&sin, sizeof(sin)))==-1)
{
perror("connect");
exit(3);
}
cout<<"connect"<<endl;
memset(buf, 0, big_size);
while(read(sockfd, file, sizeof(file)) < 0);
sprintf(buf, "%s_%s", "receive", file);
if((target=open(argv[1], O_WRONLY|O_CREAT|O_TRUNC,00070)) < 0)
{
perror("open");
exit(4);
}
cout<<"open"<<endl;
memset(buf, 0, big_size);
while((bytes = read(sockfd, buf, sizeof(buf))) > 0)
{
write(target,buf,bytes);
memset(buf, 0, big_size);
}
cout<<"receive file down"<<endl;
close(sockfd);
close(target);
}
- 自己编的socket文件传输小程序。
- 利用socket实现简单的文件传输小程序
- 编的小程序
- 基于socket的Java文件传输的程序
- 一个简单的socket文件传输程序
- 基于socket网络编程的文件传输程序
- linux socket 文件传输程序
- 一个有待完善的java小程序 上完3节java课后自己编的
- 我自己编的一个c语言小程序:银行基本功能的实现
- 第一次自己编出来的程序
- 网络编程 基于Socket的多文件传输程序实现(一)
- 文件传输程序个人小问题
- 自己写的小程序
- 一些小的计算程序就行 谁会编程序
- 我自己编的程序:小型的教务管理系统
- 自己编的关于完数的程序
- 自己编的迭代法求平方根的C++程序
- Linux下的socket文件传输
- 免激活版的Windows Server 2003无法打SP2补丁
- scanf()函数的用法和实践
- 关于sql中时间的格式转换
- java httpclient(1)
- DNS
- 自己编的socket文件传输小程序。
- const char * and char * const
- 集群的负载均衡技术
- C语言的inline
- VS2005 sp1安装提速,重温一下。
- 开博了!!
- [转载]七个受用一生的心理寓言
- 如何恢复MS SQL数据库的MDF文件
- 3D定胆码秘籍