Linux Linux program Exercise 10 (Network programming large file sending)

Source: Internet
Author: User
Tags goto htons

//Network Programming Client--Large file transfer#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>intMainintArgChar*args[]) {    if(arg<4) {printf ("Please print three param!\n"); return-1; }    intPort=atoi (args[2]); //Create socket    intSt=socket (Af_inet,sock_stream,0); if(st==-1) {printf ("Create socket failed! Error message:%s\n", Strerror (errno)); return-1; }    //Defien IP Address    structsockaddr_in addr; //Init addrmemset (&AMP;ADDR,0,sizeof(addr)); Addr.sin_family=af_inet; Addr.sin_port=htons (port); Addr.sin_addr.s_addr=INET_ADDR (args[1]); if(Connect (ST,structSOCKADDR *) &addr,sizeof(addr)) ==-1) {printf ("Connect failed! Error message:%s\n", Strerror (errno)); GotoEND; }    //Send File//define file StreamFILE * pfr=NULL; //Open the file in the Read modePfr=fopen (args[3],"R"); if(pfr==NULL) {printf ("Open the file failed!error message:%s\n", Strerror (errno)); GotoEND; }    Charbuf[1024x768]={0}; size_t Num=0;  while(Num=fread (BUF,sizeof(Char),sizeof(BUF), PFR) >0)    {        //send part of the file        if(Send (St,buf,sizeof(Char) *num,0)==-1) {printf ("Send failed!error message:%s\n", Strerror (errno));  Break; } memset (BUF,0,sizeof(BUF));    } fclose (PFR);    End:close (ST); return 0;}
//Network programming Server--Sending large files#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>intMainintArgChar*args[]) {    if(Arg <2) {printf ("Please print the Param!\n"); return-1; }    intPort = atoi (args[1]); intST = socket (af_inet, Sock_stream,0); if(St = =-1) {printf ("Create socket failed! Error message:%s\n", Strerror (errno)); return-1; }    //Defien IP Address    structsockaddr_in addr; memset (&AMP;ADDR,0,sizeof(addr)); Addr.sin_family=af_inet; Addr.sin_port=htons (port); Addr.sin_addr.s_addr=htonl (Inaddr_any); //Bind Port    if(Bind (St, (structSOCKADDR *) &addr,sizeof(addr)) == -1) {printf ("bind failed! Error message:%s\n", Strerror (errno)); GotoEND; }    //Listen    if(Listen (St, -) == -1) {printf ("Listen failed! Error message:%s\n", Strerror (errno)); GotoEND; }    //only one user connection is received    intCLIENTST =0; structsockaddr_in client_addr; memset (&AMP;CLIENT_ADDR,0,sizeof(CLIENT_ADDR)); size_t Client_addrlen=sizeof(CLIENT_ADDR); CLIENTST= Accept (St, (structSOCKADDR *) &client_addr, &Client_addrlen); if(CLIENTST = =-1) {printf ("Accept failed! Error message:%s\n", Strerror (errno)); GotoEND; }    //recv Message    Charbuf[1024x768] = {0 }; intMflag =0; //Open the file streamFILE * PFA =NULL; PFA= fopen ("/home/test/2/1.dat","a"); if(PFA = =NULL) {printf ("Open the file failed! Error message:%s\n", Strerror (errno)); GotoEND; }     while(1) {Mflag= Recv (CLIENTST, buf,sizeof(BUF),0); if(Mflag = =0) {printf ("Client is closed!\n");  Break; } Else if(Mflag = =-1) {printf ("recv message is failed! Error message:%s\n", Strerror (errno));  Break; }        //write the file to the current program directoryFwrite (BUF,sizeof(Char), Mflag, PFA); memset (BUF,0,sizeof(BUF));    } fclose (PFA);    End:close (ST); return 0;}
. Suffixes:.c. OCC=gccSRCS1=MCLIENT.CSRCS2=mserver.cobjs1=$ (srcs1:.c=. O) OBJS2=$ (srcs2:.c=. O) EXEC1=mclEXEC2=mserstart:$ (OBJS1) $ (OBJS2) $ (CC)-o $ (EXEC1) $ (OBJS1) $ (CC)-o $ (EXEC2) $ (OBJS2) @echo"-------OK-----------". C.O: $ (CC)-WALL-G-o [email protected]-C $<Clean:rm-F $ (OBJS1) RM-F $ (EXEC1) RM-F $ (OBJS2) RM-F $ (EXEC2)

Linux Linux program Exercise 10 (Network programming large file sending)

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.