Example of simple socket File Download Based on C in Linux

Source: Internet
Author: User
Tags htons

This instance is used by the client to download files from the server:

ServerCodeImplementation:

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <sys/STAT. h>
# Include <unistd. h>
# Include <ARPA/inet. h>
# Include <errno. h>
# Include <fcntl. h>

# Define fd_server_port 8088/* set port */
# Define fd_buffersize 1024/* Set buffer size */
# Define fd_listenq 10/* Set listen Max conncent */

Int main (INT argc, char * argv [])
{
Int fd_listenfd, fd_connfd, fd_filefd;/* descriptor */
Int fd_read, fd_write;
Int struct_size;
Struct sockaddr_in fd_servaddr, fd_cliaddr;
Char * fd_filename;
Char * P;
Char buffer [fd_buffersize];

Fd_listenfd = socket (af_inet, sock_stream, 0);/* Create socket */
If (fd_listenfd =-1 ){
Perror ("fd_socket ");
Exit (1 );
}

Memset (& fd_servaddr, 0, sizeof (fd_servaddr);/* servaddr set 0 */

Fd_servaddr.sin_family = af_inet;/* init serveraddr */
Fd_servaddr.sin_addr.s_addr = inaddr_any;
Fd_servaddr.sin_port = htons (fd_server_port );

/* Bind fd_listenfd */

If (-1 = (BIND (fd_listenfd, (struct sockaddr *) & fd_servaddr, sizeof (fd_servaddr )))){
Perror ("fd_bind ");
Exit (1 );
}

/* Listen fd_listenfd */
If (-1 = (Listen (fd_listenfd, fd_listenq ))){
Perror ("fd_listen ");
Exit (1 );
}

/* File download server start */
While (1 ){
Printf ("file download server starting... \ n ");

Memset (& fd_cliaddr, 0, sizeof (fd_cliaddr ));
Struct_size = sizeof (fd_cliaddr );

Fd_connfd = accept (fd_listenfd, (struct sockaddr *) & fd_cliaddr, & struct_size );
If (-1 = fd_connfd ){
Perror ("fd_accpet ");
Continue;
}

Fd_filename = "/root/tmp.txt ";
Printf ("will download file name is: % s \ n", fd_filename );
Fd_filefd = open (fd_filename, o_rdonly );
If (-1 = fd_filefd ){
Perror ("Open will download file ");
Continue;
}
While (fd_read = read (fd_filefd, buffer, fd_buffersize )){
If (-1 = fd_read ){
Perror ("read will download file ");
Break;
}

P = buffer;
While (fd_write = write (fd_connfd, P, fd_read )){
If (-1 = fd_write ){
Perror ("write client file ");
Break;
}
Else if (fd_read = fd_write) break;
Else if (fd_write> 0 ){
P + = fd_write;
Fd_read-= fd_write;
}
}
}
If (-1 = fd_read |-1 = fd_write) continue;
Close (fd_filefd );
Close (fd_connfd );
Printf ("file already is downloaded! \ N ");
}
Close (fd_listenfd );
Return 0;
}

Client code implementation:

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <sys/STAT. h>
# Include <unistd. h>
# Include <ARPA/inet. h>
# Include <errno. h>
# Include <fcntl. h>

# Define fd_server_port 8088/* set port */
# Define fd_buffersize 1024/* Set buffer size */

Int main (INT argc, char * argv [])
{
Int fd_sockfd, fd_filefd;/* descriptor */
Int fd_read, fd_write;
Int struct_size;
Struct sockaddr_in fd_sockaddr;
Char * fd_filename;
Char * P;
Char buffer [fd_buffersize];

If (argc! = 2 ){
Perror ("fd_usage: <./client> <Server IP> \ n ");
Exit (1 );
}
Fd_sockfd = socket (af_inet, sock_stream, 0);/* Create socket */
If (fd_sockfd =-1 ){
Perror ("fd_socket ");
Exit (1 );
}

Memset (& fd_sockaddr, 0, sizeof (fd_sockaddr);/* servaddr set 0 */

Fd_sockaddr.sin_family = af_inet;/* init serveraddr */
Inet_aton (argv [1], & fd_sockaddr.sin_addr );
Fd_sockaddr.sin_port = htons (fd_server_port );

If (-1 = (connect (fd_sockfd, (struct sockaddr *) & fd_sockaddr, sizeof (fd_sockaddr )))){
Perror ("fd_connect ");
Exit (1 );
}
Printf ("start connecting... \ n ");

Fd_filename = "./backup.txt ";
Fd_filefd = open (fd_filename, o_rdwr | o_creat | o_trunc, s_irwxu );
If (-1 = fd_filefd ){
Perror ("Open the backup file ");
Exit (1 );
}
While (fd_read = read (fd_sockfd, buffer, fd_buffersize )){
If (-1 = fd_read ){
Perror ("read Server File ");
Exit (1 );
}

/* P is used to wirte Error */
P = buffer;
While (fd_write = write (fd_filefd, P, fd_read )){
If (-1 = fd_write ){
Perror ("Write localhost file ");
Exit (1 );
}
Else if (fd_read = fd_write) break;
Else if (fd_write> 0 ){
P + = fd_write;
Fd_read-= fd_write;
}
}
}
Close (fd_filefd );
Close (fd_sockfd );
Printf ("finsh File Download! \ N ");
Return 0;

}

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.