Simple socket File Upload instance based on C in Linux

Source: Internet
Author: User

This instance is a client that transfers files to 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 fu_server_port 8080/* set port */
# Define fu_buffersize 1024/* Set buffer size */
# Define fu_listenq 10/* Set listen Max conncent */

Int main (INT argc, char * argv [])
{
Int fu_listenfd, fu_connfd, fu_filefd;/* descriptor */
Int fu_read, fu_write;
Int struct_size;
Struct sockaddr_in fu_servaddr, fu_cliaddr;
Char * fu_filename;
Char * P;
Char buffer [fu_buffersize];

Fu_listenfd = socket (af_inet, sock_stream, 0);/* Create socket */
If (fu_listenfd =-1 ){
Perror ("fu_socket ");
Exit (1 );
}

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

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

/* Bind fu_listenfd */

If (-1 = (BIND (fu_listenfd, (struct sockaddr *) & fu_servaddr, sizeof (fu_servaddr )))){
Perror ("fu_bind ");
Exit (1 );
}

/* Listen fu_listenfd */
If (-1 = (Listen (fu_listenfd, fu_listenq ))){
Perror ("fu_listen ");
Exit (1 );
}

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

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

Fu_connfd = accept (fu_listenfd, (struct sockaddr *) & fu_cliaddr, & struct_size );
If (-1 = fu_connfd ){
Perror ("fu_accpet ");
Continue;
}

Fu_filename = "/root/backup.txt ";
Printf ("will upload file name is: % s \ n", fu_filename );

Fu_filefd = open (fu_filename, o_rdwr | o_creat | o_trunc, s_irwxu );
If (fu_filefd <0 ){
Perror ("Open localhost file ");
Continue;
}

While (fu_read = read (fu_connfd, buffer, fu_buffersize )){
If (fu_read <0 ){
Perror ("read client file ");
Break;
}
If (-1 = write (fu_filefd, buffer, fu_read )){
Perror ("writing to filefd error ");
Break;
}
}
If (-1 = fu_read |-1 = fu_write) continue;
Close (fu_filefd );
Close (fu_connfd );
Printf ("File Upload success! \ N ");
}
Close (fu_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 fu_server_port 8080/* set port */
# Define fu_buffersize 1024/* Set buffer size */

Int main (INT argc, char * argv [])
{
Int fu_sockfd, fu_filefd;/* descriptor */
Int fu_read, fu_write;
Int struct_size;
Struct sockaddr_in fu_sockaddr;
Char * fu_filename;
Char * P;
Char buffer [fu_buffersize];

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

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

Fu_sockaddr.sin_family = af_inet;/* init serveraddr */
Inet_aton (argv [1], & fu_sockaddr.sin_addr );
Fu_sockaddr.sin_port = htons (fu_server_port );

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

Fu_filename = "./will.txt ";
Printf ("will upload file name is: % s \ n", fu_filename );
Fu_filefd = open (fu_filename, o_rdonly );
If (-1 = fu_filefd ){
Perror ("Open will upload file ");
Exit (1 );
}
While (fu_read = read (fu_filefd, buffer, fu_buffersize )){
If (-1 = fu_read ){
Perror ("read will upload file ");
Exit (1 );
}

P = buffer;
While (fu_write = write (fu_sockfd, P, fu_read )){
If (-1 = fu_write ){
Perror ("write client file ");
Break;
}
Else if (fu_read = fu_write) break;
Else if (fu_write> 0 ){
P + = fu_write;
Fu_read-= fu_write;
}
}
If (-1 = fu_write) Exit (1 );
}
Close (fu_filefd );
Close (fu_sockfd );
Printf ("file already is uploaded! \ N ");

}

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.