Write the downloader by yourself-simple socket programming

Source: Internet
Author: User

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

Related Article

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.