Using the socket to transmit files

Source: Internet
Author: User
Tags htons

Welcome criticism

You can also look at the more detailed here http://www.jb51.net/article/53733.htm

Service Side
#include <stdio.h>#include<string.h>#include<stdlib.h>#include<sys/socket.h>#include<arpa/inet.h>#include<unistd.h>#defineBuffer_size 1024#defineFile_name_size 512voidErrorChar*message);intMainintargcChar*argv[]) { intserv_sock,clnt_sock,i,length,read_length,j; structsockaddr_in serv_addr,clnt_addr; Socklen_t CLNT_ADDR_SZ; CharFile_name[file_name_size],message[buffer_size]; CharBuffer[buffer_size]; if(argc!=2) {printf ("usage:%s<port>\n", argv[0]); Exit (1); } Serv_sock=socket (Pf_inet,sock_stream,0); if(serv_sock==-1) Error ("socket () error"); memset (&AMP;SERV_ADDR,0,sizeof(SERV_ADDR)); Serv_addr.sin_family=af_inet; Serv_addr.sin_addr.s_addr=htonl (Inaddr_any); Serv_addr.sin_port=htons (Atoi (argv[1])); if(Bind (Serv_sock, (structsockaddr*) &serv_addr,sizeof(SERV_ADDR)) ==-1) Error ("bind () error"); if(Listen (Serv_sock,3)==-1) Error ("Listen () Error"); CLNT_ADDR_SZ=sizeof(CLNT_ADDR); for(i=1; i<=3; i++) //While (1){Clnt_sock=accept (Serv_sock, (structsockaddr*) &clnt_addr,&CLNT_ADDR_SZ); if(clnt_sock==-1) Error ("Accept () error"); Elseprintf ("connected client%d \ n '", i); if(Read (clnt_sock,buffer,buffer_size) ==-1) printf ("read () Error"); strcpy (File_name,buffer); printf ("file ' s name%s\n", file_name); FILE*fp=fopen (file_name,"R"); if(fp==NULL) printf ("file%s can not open \ n", file_name); Else{bzero (buffer,buffer_size); while(Length=fread (Buffer,sizeof(Char), BUFFER_SIZE,FP) >0) { if(Write (Clnt_sock,buffer,length) ==-1) {printf ("Send file%s failed\n", file_name); Break; }} fclose (FP); printf ("Transfer succeed\n"); } close (Clnt_sock); } close (Serv_sock); return 0;}voidErrorChar*message) {fputs (Message,stderr); FPUTC ('\ n', stderr); Exit (1);}


Client

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <unistd.h>5#include <arpa/inet.h>6#include <sys/socket.h>7 #defineFILE_NAME_SZ 5128 #defineBuffer_size 10249 voidErrorChar*message);Ten One intMainintargcChar*argv[]) A { - intsock,temp=0; - structsockaddr_in clnt; the intlength,write_length; - CharBuffer[buffer_size+1]; - Charfilename[file_name_sz+1]; - CharMessage[buffer_size]; +Sock=socket (Pf_inet,sock_stream,0); - if(argc!=3) + { Aprintf"usage:%s<ip><port>\n", argv[0]); at } - if(sock==-1) -Error"socket () error"); - -memset (&AMP;CLNT,0,sizeof(CLNT)); -clnt.sin_family=af_inet; inCLNT.SIN_ADDR.S_ADDR=INET_ADDR (argv[1]); -Clnt.sin_port=htons (Atoi (argv[2])); to + if(Connect (sock,structsockaddr*) &AMP;CLNT,sizeof(CLNT)) ==-1) -Error"Connect () Error"); the Else *Puts"connect ... .."); $ Panax Notoginsengprintf"input File Name\t"); -scanf"%s", filename); the + strcpy (buffer,filename); A write (sock,buffer,buffer_size); the +FILE *fp=fopen (filename,"W"); - if(fp==NULL) $ { $printf"Open File%s error\n", filename); -Exit1); - } the Else - {Wuyi bzero (buffer,buffer_size); the while((Length=read (sock,buffer,buffer_size)) >0) - { Wu if(Fwrite (Buffer,sizeof(Char), LENGTH,FP) <length) - { Aboutprintf"file%s Write error\n", filename); $ Break; - } - //printf ("Receive%s succeed\n", filename); -temp=1; A } + if(temp==1) printf ("Receive%s succedd\n", filename); the Elseprintf"Receive%s failed\n", filename); - } $ fclose (FP); the Close (sock); the return 0; the } the - voidErrorChar*message) in { the fputs (message,stderr); theFPUTC ('\ n', stderr); AboutExit1); the}

Using the socket to transmit files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.