UDP-based file transfer program

Source: Internet
Author: User
Tags htons
I. Question

Use Standard C to compile the file transfer program between the UDP server and client based on socket communication in Linux.

Requirements:

1. Compile the server program and name it udp_server.c.

2. Compile the client program and name it udp_client.c.

3. Start file transfer by sending the string starttransfer from the client, send the string quittransfer to exit the file transfer, and end the programs on both sides.

4. Metadata, which is stored in the same directory of the client program.

Ii. Procedures

Udp_server.c

# Include <stdio. h>
# Include <string. h>
# Include <unistd. h>
# Include <ctype. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <sys/Mman. h>

# Define maxlen 4096

Int Port = 9012;
Int to_port = 5678;
Char to_ip [] = "127.0.0.1 ";
Char startcmd [13] = "starttransfer ";
Char quitcmd [12] = "quittransfer ";

Int main ()
{
Struct sockaddr_in local_sin;
Struct sockaddr_in to_sin;
Int mysock;

File * FP;
Int filelen;

Char Buf [maxlen];

Int Len;
Int I;
Int recvlen = sizeof (struct sockaddr_in), sendlen = sizeof (struct sockaddr_in );

Bzero (& local_sin, sizeof (local_sin ));
Local_sin.sin_family = af_inet;
Local_sin.sin_addr.s_addr = inet_addr ("127.0.0.1 ");
Local_sin.sin_port = htons (port );

Bzero (& to_sin, sizeof (to_sin ));
To_sin.sin_family = af_inet;
To_sin.sin_addr.s_addr = inet_addr (to_ip );
To_sin.sin_port = htons (to_port );

Mysock = socket (af_inet, sock_dgram, 0); // create a socket
Int bind_stats = BIND (mysock, (struct sockaddr *) & local_sin, sizeof (local_sin); // bind
If (bind_stats =-1 ){
Perror ("BIND error ");
Exit (-1 );
}
Len = recvfrom (mysock, Buf, maxlen, 0, (struct sockaddr *) & to_sin, & recvlen );
If (LEN =-1 ){
Perror ("recvfrom error ");
Exit (-1 );
}
If (strncmp (BUF, startcmd, 13) = 0) // start transmission
{
// Open the file
If (FP = fopen ("mysend.txt", "R") = NULL)
{
Printf ("mysend.txt open failure !! \ N ");
Return-1;
}
Fseek (FP, 0, seek_end );
Filelen = ftell (FP );
Fseek (FP, 0, seek_set );
Printf ("filelen = % d \ n", filelen );

While (1)
{
Bzero (BUF, maxlen );
Fread (BUF, maxlen, 1, FP );
If (filelen> = maxlen)
{
Len = sendto (mysock, Buf, maxlen, 0, (struct sockaddr *) & to_sin, sendlen );
If (LEN <0)
{
Printf ("Send failure !! \ N ");
Break;
}
Filelen-= maxlen;
}
Else
{
Sendto (mysock, Buf, filelen, 0, (struct sockaddr *) & to_sin, sendlen );
If (LEN <0)
{
Printf ("Send failure !! \ N ");
Break;
}
Sendto (mysock, quitcmd, strlen (quitcmd), 0, (struct sockaddr *) & to_sin, sendlen );
Break;
}
}
}
Printf ("Quit \ n ");
Return 0;
}

Udp_client.c

# Include <stdio. h>
# Include <string. h>
# Include <unistd. h>
# Include <ctype. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <sys/Mman. h>

# Define maxlen 4096

Int Port = 5678;
Int to_port = 9012;
Char to_ip [] = "127.0.0.1 ";
Char startcmd [13] = "starttransfer ";
Char quitcmd [12] = "quittransfer ";

Void main ()
{
Struct sockaddr_in local_sin;
Struct sockaddr_in to_sin;
Int mysock;

File * FP;

Char Buf [maxlen];

Int Len;
Int I;
Int recvlen = sizeof (struct sockaddr_in), sendlen = sizeof (struct sockaddr_in );

Bzero (& local_sin, sizeof (local_sin ));
Local_sin.sin_family = af_inet;
Local_sin.sin_addr.s_addr = inet_addr ("127.0.0.1 ");
Local_sin.sin_port = htons (port );

Bzero (& to_sin, sizeof (to_sin ));
To_sin.sin_family = af_inet;
To_sin.sin_addr.s_addr = inet_addr ("127.0.0.1 ");
To_sin.sin_port = htons (to_port );

Mysock = socket (af_inet, sock_dgram, 0); // create a socket
BIND (mysock, (struct sockaddr *) & local_sin, sizeof (local_sin); // bind

I = sendto (mysock, startcmd, 13, 0, (struct sockaddr *) & to_sin, sendlen); // start File Transfer
If (I =-1 ){
Perror ("sendto error ");
Exit (-1 );
}
// Open the file
If (FP = fopen ("myrecv.txt", "W") = NULL)
{
Printf ("myrecv.txt open failure !! \ N ");
Return-1;
}

While (1)
{
Bzero (BUF, maxlen );
Len = recvfrom (mysock, Buf, maxlen, 0, (struct sockaddr *) & to_sin, & recvlen );
If (strncmp (BUF, quitcmd, 12) = 0)
{
Fclose (FP );
Break;
}
Fwrite (BUF, Len, 1, FP );
}
Close (mysock );
Printf ("Quit \ 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.