练习socket编程的一个文件传输小程序

1586阅读 0评论2011-04-27 0xC1988
分类:LINUX

下载附件,make一下,具体用法是
tfserver
tfclient
 
这个程序用于练习socket网络编程
 
tfserver代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <sys/socket.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>

  11. const int port=1025;
  12. const int backlog=5;

  13. int main(int argc,char **argv){
  14.     int fd,listenfd,connectfd;
  15.     struct sockaddr_in server;
  16.     struct sockaddr_in client;
  17.     socklen_t addrlen;

  18.     if(argc<2){
  19.         fprintf(stderr,"Usage: %s ",argv[0]);
  20.         exit(1);
  21.     }


  22.     //建立套接字
  23.     if(-1==(listenfd=socket(AF_INET,SOCK_STREAM,0))){
  24.         perror("socket() error\n");
  25.         exit(1);
  26.     }

  27.     int opt=SO_REUSEADDR;
  28.     setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

  29.     bzero(&server,sizeof(server));

  30.     server.sin_family=AF_INET;
  31.     server.sin_port=htons(port);
  32.     server.sin_addr.s_addr=htonl(INADDR_ANY);

  33.     if(-1==bind(listenfd,(struct sockaddr *)&server,sizeof(server))){
  34.         perror("bind() error\n");
  35.         exit(1);
  36.     }

  37.     if(-1==listen(listenfd,backlog)){
  38.         perror("listen() error\n");
  39.         exit(1);
  40.     }

  41.     addrlen=sizeof(client);

  42.     if(-1==(connectfd=accept(listenfd,(struct sockaddr *)&client,&addrlen))){
  43.         perror("accept() error\n");
  44.         exit(1);
  45.     }

  46.     if(-1==(fd=open(argv[1],O_RDONLY))){
  47.         perror("open() error\n");
  48.         close(connectfd);
  49.         close(listenfd);
  50.         exit(1);
  51.     }

  52.     char buf[512];
  53.     int len=0;
  54.     bzero(buf,sizeof(buf));

  55.     while((len=read(fd,buf,sizeof(buf)))>0){
  56.         write(connectfd,buf,len);
  57.     }

  58.     close(fd);
  59.     close(connectfd);
  60.     close(listenfd);

  61.     return 0;
  62. }

 

tfclient的代码:

 

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>
  10. #include <fcntl.h>
  11. #include <netdb.h>


  12. const int port= 1025;

  13. int main(int argc,char **argv){
  14.     struct sockaddr_in server;
  15.     int connectfd,fd;
  16.     struct hostent *he;

  17.     if(argc<3){
  18.         fprintf(stderr,"Usage: %s ",argv[0]);
  19.         exit(1);
  20.     }

  21.     if(NULL==(he=gethostbyname(argv[1]))){
  22.         perror("gethostbyname() error\n");
  23.         exit(1);
  24.     }

  25.     if(-1==(connectfd=socket(AF_INET,SOCK_STREAM,0))){
  26.         perror("sock() error\n");
  27.         exit(1);
  28.     }

  29.     bzero(&server,sizeof(server));
  30.     server.sin_family=AF_INET;
  31.     server.sin_port=htons(port);
  32.     server.sin_addr=*((struct in_addr *)he->h_addr);
  33.     

  34.     if(-1==connect(connectfd,(struct sockaddr *)&server,sizeof(server))){
  35.         perror("connect() error\n");
  36.         exit(1);
  37.     }

  38.     if(-1==(fd=open(argv[2],O_CREAT|O_WRONLY,S_IRWXU))){
  39.         perror("open() error\n");
  40.         exit(1);
  41.     }

  42.     int len=0;
  43.     char buf[512];
  44.     bzero(buf,sizeof(buf));

  45.     while((len=read(connectfd,buf,sizeof(buf)))>0){
  46.         write(fd,buf,len);
  47.     }
  48.     close(fd);
  49.     close(connectfd);
  50.     return 0;
  51. }
 tf.rar   
上一篇:递归合并两个有序链表为一个有序链表
下一篇:Linux的两个定时器