自己编的socket文件传输小程序。

来源:互联网 发布:电极编程怎么给负余量 编辑:程序博客网 时间:2024/05/01 14:08
自己编的socket文件传输小程序。【转自:http://hi.baidu.com/r_soul/blog/item/600b2601c47b9a0f7aec2c4c.html】
2007-03-27 12:07

传输端:

#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);
}

原创粉丝点击