Home > other >  File transfer for connectionless UDP protocol
File transfer for connectionless UDP protocol

Time:09-23

Spent almost a day, consult the teacher zheng class code oneself fumble out udp protocol file transfers, specific function as follows
1. The client to the server sends a want to download a file name
2. The server receives the file name, open the corresponding files, read the file content, and then sent back to the client, the file sent by complete with a length of 0 udp segments as a symbol of
3. The client will receive the local data to the specified file name,

The client code:

//client
#include

#include

#include

# pragma comment (lib, "ws2_32. Lib")

# define MAX_BUF 65536

Int main ()

{

WSAData WSAData;

Int err=WSAStartup (WINSOCK_VERSION, & amp; WsaData);

If (0!=(err)

{

return -1;

}

The SOCKET sock;

The sock=socket (AF_INET, SOCK_DGRAM, 0);

If (INVALID_SOCKET==the sock)

{

Printf (" socket () Failed: % d \ n ", WSAGetLastError ());

WSACleanup ();

return -1;

}



Char data []="Hello World!" ;

Char dest_ip []="192.168.1.104,";//destination IP


Unsigned short dest_port=20000;//destination port



Sockaddr_in RemoteAddr;

RemoteAddr. Sin_family=AF_INET;

RemoteAddr. Sin_port=htons (dest_port);

RemoteAddr. Sin_addr. S_addr=inet_addr (dest_ip);

Char rbuf [MAX_BUF];

Memset (rbuf, 0, MAX_BUF);

Int RemoteLen=sizeof (RemoteAddr);

Char path [100]={" 0 "};

Char name [100]={" 0 "};

Printf (" send file name \ n ");

Gets (path);

The FILE * fp=fopen (path, "wb");

if(! Fp)

{

Printf (" failed to open file!" );

return 1;

}

The else

{

Printf (" file has been opened, wait for transmission \ n ");

}

While (1)

{
Sendto (sock, path, strlen (path), 0, (sockaddr *) & amp; RemoteAddr, sizeof (RemoteAddr));

Memset (rbuf, 0102, 4);

Int sByte=sendto (sock, rbuf, strlen (rbuf), 0, (sockaddr *) & amp; RemoteAddr, sizeof (RemoteAddr));

If (SOCKET_ERROR==sByte)

{

Printf (" sendto () Failed: % d \ n ", WSAGetLastError ());

Closesocket (sock);

WSACleanup ();

return -1;

}

Recvfrom (sock, name, 1024, 0, (sockaddr *) & amp; RemoteAddr, & amp; RemoteLen);

Memset (rbuf, 0102, 4);

Int rByte=recvfrom (sock, rbuf, 1024, 0, (sockaddr *) & amp; RemoteAddr, & amp; RemoteLen);
If (SOCKET_ERROR==rByte)

{

Printf (" recvfrom () Failed: % d \ n ", WSAGetLastError ());

Closesocket (sock);

WSACleanup ();

return -1;

}

Fwrite (rbuf, 1, strlen (rbuf), fp);

The fseek (fp, 0, 2);

Printf (" write file content is: \ n \ n % s ", rbuf);
}

fclose(fp);

Closesocket (sock);

WSACleanup ();

return 0;

}

The server-side code:
//the server

#include

#include

#include

# pragma comment (lib, "ws2_32. Lib")

Int main ()

{

WSAData WSAData;

Int err=WSAStartup (WINSOCK_VERSION, & amp; WsaData);

If (0!=(err)

{

return -1;

}

The SOCKET sock;

The sock=socket (AF_INET, SOCK_DGRAM, 0);

If (INVALID_SOCKET==the sock)

{

Printf (" socket () Failed: % d \ n ", WSAGetLastError ());

WSACleanup ();

return -1;

}



Sockaddr_in LocalAddr;

LocalAddr. Sin_family=AF_INET;

LocalAddr. Sin_port=htons (20000);

LocalAddr. Sin_addr. S_addr=htonl (INADDR_ANY);

Err=bind (sock, (sockaddr *) & amp; LocalAddr, sizeof (LocalAddr));

If (SOCKET_ERROR==err)

{

Printf (" the bind () Failed: % d \ n ", WSAGetLastError ());

Closesocket (sock);

WSACleanup ();

return -1;

}

Sockaddr_in RemoteAddr;

Int RemoteLen=sizeof (RemoteAddr);

Char path [100]={" 0 "};

Char name [100]={" 0 "};

While (1)

{

Printf (" please enter the file path \ n ");

Gets (path);

The FILE * fp=fopen (path, "rb");//to read and write binary remember add b



if(! Fp)

{

Printf (" error!" );

return 1;

}



The else

{

Printf (" file has been opened, wait for transport... \n");

}

Char rbuf [1024]={0};

While (1)

{

Recvfrom (sock, name, 1024, 0, (sockaddr *) & amp; RemoteAddr, & amp; RemoteLen);

Printf (" file name is: \ n \ n % s ", name);

Memset (rbuf, 0102, 4);

Int rByte=recvfrom (sock, rbuf, 1024, 0, (sockaddr *) & amp; RemoteAddr, & amp; RemoteLen);

If (SOCKET_ERROR==rByte)

{

Printf (" recvfrom () Failed: % d \ n ", WSAGetLastError ());

Closesocket (sock);

WSACleanup ();

return -1;

}

while(! The feof (fp))
{
Sendto (sock, path, strlen (path), 0, (sockaddr *) & amp; RemoteAddr, sizeof (RemoteAddr));

Memset (rbuf, 0102, 4);

Fread (rbuf, 1102 4, fp);

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related