Getting Started with C + + network programming under Windows

Source: Internet
Author: User
Tags htons

WinSocket are divided into stream sockets and datagram sockets. WinSocket programming is generally based on the C/S mode.
The processes on the server side are generally as follows:
1. Initialize WinSocket
2. Set up socket
3. Bonding with the machine (BIND)
4. Start monitoring (Listen)
5. Then establish a connection with the client (accept)
6. Then communicate with the client (send, recv)
7. When the communication is complete, close the connection
8. Release the relevant resources of WinSocket
The process on the client side is generally as follows:
1. Initialize WinSocket
2. Set up socket
3. Bonding with the machine (BIND)
4. Connect to the server (connect)
5, communication with the server (send, recv)
6. When the communication is complete, close the connection
7. Release the resources occupied by WinSocket
Compared to the Java network programming, I found that there are three different places, in Java server socket is a serversocket class,client socket is the socket Class; there is no first step in Java (initialize) and the third step (BIND) process.
It is necessary to initialize WinSocket because the Winsock service is implemented in the form of a dynamic connection library Winsock DLL, so you must first call the initialization function (WSAStartup) to initialize the Winsock DLL, negotiate the version support for Winsock, and allocate the necessary resources; In the third step of the bind process, when the socket (socket) is created, the socket is not associated with information such as the native address and port, and the BIND process accomplishes this task.
Now start my first program, do Windows networking programming, need to include winsock2.h or mswsock.h header files in the program, and you need to add the introduction ws2_32.lib or Winsock32.lib library files to them separately in Microsoft Visual Studio 8\vc\platformsdk\lib
The function of my program is to implement the client and the server to connect, when connected, at both ends of the display connection is successful (mainly to familiarize themselves with the above process)
Server-side code:

#include"stdafx.h"#include"WinSock2.h"#include<iostream>using namespacestd;int_tmain (intARGC, _tchar*argv[])    {Wsadata wsadata;    SOCKET Listeningsocket;    Sockaddr_in serveraddr;    SOCKET newconnection;    Sockaddr_in clientaddr; intPort =5150; WSAStartup (Makeword (2,2), &wsadata);//initializing Windows Socket 2.2Listeningsocket= Socket (af_inet, sock_stream, ipproto_tcp);//create a new socket to respond to client connection requests; The Af_inet field is the type that represents the network address, Af_inet represents the communication in the Internet domain; Sock_stream represents the type of socket, Sock_ Stream represents a stream socket IPPROTO_TCP indicates the protocol type, IPPROTO_TCP or 0 represents the TCP/IP protocolserveraddr.sin_family= Af_inet;//fill in the server address letterServeraddr.sin_port = htons (port);//Port is 5150SERVERADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);//Inaddr_any represents the network address used on the machine, for example, you have two network cards on the machine, then the data to reach the two net card, your socket can be notified; htonl convert IP address to network formatBind (Listeningsocket, (SOCKADDR *) &serveraddr,sizeof(SERVERADDR));//when the socket function is called to create a socket, the socket is not connected to the local address and port information, and the BIND function will do the workListen (Listeningsocket,2);//start listening, specifying a maximum number of simultaneous connections of 2    intLen =sizeof(CLIENTADDR); Newconnection= Accept (Listeningsocket, (SOCKADDR *) &clientaddr, &len);//accept a new connectioncout<<"************************************************"<<Endl; cout<<"****new Client has been connected******"<<Endl; cout<<"*************************************************"<<Endl; /*When the connection is established, we can communicate with the client here and transmit the data.*/closesocket (newconnection); //Close ConnectionClosesocket (Listeningsocket);//and turn off the monitor socketWSACleanup (); //Releasing related resources for the Windows Socket DLL    return 0;}

Client-side code:

#include"stdafx.h"#include"WinSock2.h"#include<iostream>using namespacestd;int_tmain (intARGC, _tchar*argv[])    {Wsadata wsadata;    SOCKET s;    Sockaddr_in serveraddr; intPort =5150; WSAStartup (Makeword (2,2), &wsadata);//initializing Windows Socket 2.2s = socket (af_inet, sock_stream, ipproto_tcp);//create a new socket to connect to the serverserveraddr.sin_family=af_inet; Serveraddr.sin_port=htons (Port); ServerAddr.sin_addr.s_addr= Inet_addr ("127.0.0.1");//I communicate with this machine in the program    intresult = Connect (s, (SOCKADDR *) &serveraddr,sizeof(SERVERADDR));//making a connection request to the server    if(Result = =0){//result==0 indicates successful connectioncout <<"Client has connected to server!";    } closesocket (s);    WSACleanup (); return 0;}

Getting Started with C + + network programming under Windows

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.