Author: Nishant S Original link: http: //www.codeproject.com/internet/winsockintro02.asp Download Project
Second, simple TCP client
Introduction
This article is the ending of "Winsock Program Design (1) - Simple TCP Server", if you haven't read Part 1, I still suggest you first read. In this article, I will demonstrate how to write a simple TCP client program. We have to write a program that will connect to an HTTP server and get a file.
A simple TCP client program process
1. Use WSAStartup () to initialize the Winsock library. 2. Create an ipproto_tcp socket using socket (). 3, use gethostByName () / gethostbyaddr () to get host information. 4, use connect () and the socket connection server we created. 5. Use Send () / RECV () to send and receive data until our TCP session ends. 6. Use the CloseSocket () to turn off the socket connection. 7, use wsacleanup () to release Winsock.
Initialize Winsock
Just as other Winsock programs, we need to initialize the Winsock library. This is basically an inspection that Winsock is available in the current system. For previous versions, we must of course hope that this is.
INT WSARET = WSAStartup (0x101, & WSADATA); if (WSaret) return;
Create Socket
Sockets are an entity that acts as an endpoint between the client and the server. Once the client is connected to the server, there will be two sockets - the socket on the client and the socket on the other server. Let us call them for Clientsock and Serversock. When the client uses Send () when ClientSock uses Send (), the server can use RECV () to receive the data sent by the client, and vice versa. For our purposes, we use a function named Socket () to create a socket.
Socket conn; conn = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); if (conn == invalid_socket) return;
Get host information
Obviously, we have to get its information before connecting to the host (server). We can use two functions - GethostByname () and gethostbyaddr (). When we have a server's DNS name, we can use the gethostByname () function, such as the name, or the name of the codeproject.com or ftp.myserver.org. When we have an IP address of the server to be connected, you can use the gethostbyaddr () function, such as 192.168.1.1 or 202.54.100. Obviously, we hope that our end users can use the DNS name and use IP addresses. So, for these work, it is transparent to him, we need to play a small trick like this. We use inet_addr () to the entrance string, and this function converts an IP address into a standard network address format. In this way, if it returns a failure, we can know that this string is not an IP address. If it is successful, we can assume that it is a valid IP address.
if (inet_addr (servername) == INADDR_NONE) {hp = gethostbyname (servername);} else {addr = inet_addr (servername); hp = gethostbyaddr ((char *) & addr, sizeof (addr), AF_INET);} if (hp == NULL) {CloseSocket (CONN); return;} Connect to the server
The Connect () function is used to connect to the target server. We pass it our previously created sockets and a SockAddr structure. We use the host address returned by gethostbyname () / gethostbyaddr () to assign a value for the SockAddr member, and enter a valid port to connect.
Server.sin_addr.s_addr = * ((unsigned long *) hp-> h_addr); server.sin_family = AF_INET; Server.sin_Port = HTONS (80); if (Conn, Struct SockAddr *) & Server, Sizeof (Server ))) {CloseSocket (CONN); RETURN;}
Session
When the socket connection is established, the client and server can send / receive data by send () and RECV (). This is often referred to as TCP sessions. We need HTTP sessions for our specific situation. Compared to those complex SMTP or POP3 protocols, it is still relatively simple. The HTTP get command is used to get files from the HTTP server. This file can be an HTML file, an image file, a compressed file, an MP3 file, and more. In this way, this file will be sent (this is its simplest form). Of course, there are some more complex methods to use this command.
Get http-path-to-file / r / n / r / n
In our program, we send a get command like this:
Sprintf (BUFF, "Get% S / R / N / R / N", FilePath); Send (CONN, BUFF, STRLEN (BUFF), 0)
When we send this command, we should know that the server will start sending us the files we requested. Just like we use Send () to send our commands, we can use RECV () to receive data sent by the server. We call RECV () until it returns zero, and we will know that the server has been sent. And, for our specific case, we can write these data into the file, just like we have to download and save this file.
While (Y = Recv (CONN, BUFF, 512, 0)) {F.Write (BUFF, Y);
Shut down connection
Now that our session is over, we have to close the connection. In our case, the HTTP connection will be turned off by the server after the file is sent, but this is not tight, we still need to close our socket and release the resource. In a more complex session, we typically call Shutdown () before calling CloseSocket () to determine that the buffer has been refreshed, otherwise some data may be lost.
CloseSocket (CONN);
Release Winsock
We call wsacleanup () to end the use of Winsock.
WSACLEANUP ();
Thank you for reading.