A download tool is a network tool that receives desired data from the network.
The download tool is a network client. The download process is nothing more than connecting the client to the server, and then
When sending a resource download request, the server starts to send data back and the client receives the data.
A simple download is complete.
All network applications start with socket programming. The download tool is also the simplest
Socket programming started.
The first is two programs: the socket client and the server. Compile in windows vc6.0)
Server:
# Include <Winsock2.h>
# Include <stdio. h>
# Pragma comment (lib, "WS2_32.lib ")
Void main ()
{
WSADATA wsd;
SOCKET server; // server socket
SOCKADDR_IN addrSrv;
Char sendBuf [100];
Char recvBuf [100];
SOCKADDR_IN addrClient;
SOCKET client; // The connected client socket
Int len;
If (WSAStartup (MAKEWORD (2, 2), & wsd )! = 0)
{
Printf ("start up failed! \ N ");
Return;
}
Server = socket (AF_INET, SOCK_STREAM, 0); // create a socket
AddrSrv. sin_addr.S_un.S_addr = htonl (INADDR_ANY); // set the address
AddrSrv. sin_family = AF_INET;
AddrSrv. sin_port = htons (6000); // set the port number
Bind (server, (SOCKADDR *) & addrSrv, sizeof (SOCKADDR); // bind
Listen (server, 5); // you can specify the maximum number of connections.
Len = sizeof (SOCKADDR );
While (1)
{
Client = accept (server, (SOCKADDR *) & addrClient, & len); // receives client connections
Sprintf (sendBuf, "Welcome % s ",
Inet_ntoa (addrClient. sin_addr ));
Send (client, sendBuf, strlen (sendBuf) +); // send information to the client
Recv (client, recvBuf,); // receives client data
Printf ("% s \ n", recvBuf );
Closesocket (client );
}
Closesocket (client); // close the connection
WSACleanup ();
}
Client:
# Include <winsock2.h>
# Include <stdio. h>
# Pragma comment (lib, "WS2_32.lib ")
Void main ()
{
WSADATA wsd;
SOCKET sockClient; // client socket
SOCKADDR_IN addrSrv;
Char recvBuf [100];
If (WSAStartup (MAKEWORD (2, 2), & wsd )! = 0)
{
Printf ("start up failed! \ N ");
Return;
}
SockClient = socket (AF_INET, SOCK_STREAM, 0); // create a socket
AddrSrv. sin_addr.S_un.S_addr = inet_addr ("127.0.0.1 ");
AddrSrv. sin_family = AF_INET;
AddrSrv. sin_port = htons (6000 );
Connect (sockClient, (SOCKADDR *) & addrSrv, sizeof (SOCKADDR); // connect to the server
Recv (sockClient, recvBuf,); // receives server data
Printf ("% s \ n", recvBuf );
Send (sockClient, "hello world", strlen ("hello world") + 1, 0); // send data to the server
Closesocket (sockClient); // close the connection
WSACleanup ();
}
The two programs probably know the socket programming process, and understand that the network is eventually send and recv.
The above two programs can simply transfer files by simply rewriting them.
Change the information sent by the client to the file name of the request.
Send (sockClient, filename, strlen (filename) + 1, 0 );
The server receives the file name:
Recv (client, filename, 100,0 );
The server sends the file information:
If (fp = fopen (filename, "rb") = NULL) closesocket (client); // if the requested file does not exist, close the connection.
N = fread (sendBuf, 1,100, fp );
While (n> 0 ){
Send (client, sendBuf, n, 0); // send the file client
N = fread (sendBuf, 1,100, fp );
}
The client receives the file information:
If (fp = fopen (filename, "wb") = NULL)
{
Return;
}
While (ret = recv (sockClient, recvBuf, 100,0)> 0)
{
Fwrite (recvBuf, 1, ret, fp );
}
In this way, you can use two programs in windows to copy files.
This article from the "Daily C" blog, please be sure to keep this source http://kenshinf.blog.51cto.com/1088256/281910