Windows Socket Basics

Source: Internet
Author: User
Tags strcmp htons

Originally wanted to write a chat system, temporary level is limited, do not waste time, then fill it up again

Server code:

#include <Winsock2.h> #include <stdio.h> #include <iostream> #pragma comment (lib, "Ws2_32.lib") using namespace Std;void Main () {//windows unique uses Wsastarup () to initialize the socket font. Equivalent to allocating network resources wsadata Wsadata;int err = WSAStartup (0x0101, &wsadata); if (err! = 0) {return;} Windows-specific settings for the version of the network library if (Lobyte (wsadata.wversion)! = 1 | | Hibyte (wsadata.wversion)! = 1) {//windows unique corresponding network resource release wsacleanup (); return;}  Create socket af_inet represents IPV4 sock_stream socket type 0 Specify protocol type 0 default IPPROTO_TCP TCP protocol Socket SOCKSERV = socket (af_inet, Sock_stream, 0) if (invalid_socket = = Sockserv) {wsacleanup ();/////////This time just set the SOCKET parameter and it is not created so closesocket (sock_id) is not required; return;} Above with Linux SOCKET_FD = socket (af_inet, sock_stream, 0)) = = -1sockaddr_in ADDRSERV;//IP address set to Inaddr_any, let system automatically get native address Addrs Erv.sin_addr. S_un. S_ADDR = htonl (Inaddr_any); HTONL convert IP address format function Addrserv.sin_port = htons (6000);//Set port number 6000addrserv.sin_family = Af_inet; ipv4//assign a specific address in the address group to the socket err = bind (Sockserv, (sockaddr*) &addrserv, sizeof (SOCKADDR)); if (0! = Err) {WSacleanup ();//This time there is no real creation of the socket is also set the necessary parameters return;} At this time really created a socket to listen to the socket set maximum number of connections listen (Sockserv, somaxconn);//Loop Listener client Request This will store information for each client such as IP port sockaddr_in Addrclient;int len = sizeof (SOCKADDR); while (1) {//Accept client request client information placed in Addrclient if you do not care that a parameter can be set to null//block until there is a client connection,  Otherwise, it wastes CPU resources more. SOCKET Sockconn = Accept (Sockserv, (sockaddr*) &addrclient, &len); char sendbuf[255];cin >> sendbuf;//send Send data to the client send (Sockconn, SendBuf, strlen (sendbuf) + 1, 0); Char recvbuf[255];//accept data from the client to accept data written to recvbuf int nrecv = recv (s Ockconn, Recvbuf, 255, 0); if (Nrecv > 0) {recvbuf[nrecv] = ' ';p rintf ("%s\n", Recvbuf);} if (strcmp (Recvbuf, "") = = 0) closesocket (sockconn);} Closesocket (Sockserv);}

Client code:

#include <Winsock2.h> #include <stdio.h> #include <iostream> #pragma comment (lib, "Ws2_32.lib") using namespace Std;void Main () {wsadata wsadata; WORD wversionrequested = Makeword (1, 1); Set the request socket 1.1 version int err = WSAStartup (wversionrequested, &wsadata); if (err! = 0) return;//Check whether the 1.1 version is not returned if (Lobyte ( wsadata.wversion)! = 1 | | Hibyte (wsadata.wversion)! = 1) {wsacleanup (); return;} Create socket IPV4 Socket Type Sock_stream 0 represents the default TCP protocol Socket SOCKCONN = socket (af_inet, sock_stream, 0); Sockaddr_in addrserv;addrserv.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1");//Set the address of the connection addrserv.sin_family = Af_inet; Ipv4addrserv.sin_port = htons (6000); Set the port number of the connection while (1) {//Connection Settings server Connect (sockconn, (SOCKADDR *) &addrserv, sizeof (SOCKADDR)); char recvbuf[255] = "+" ;//Accept server-sent data recv (Sockconn, recvbuf, 255, 0); if (strcmp (Recvbuf, "")! = 0) cout << recvbuf << Endl;char sendbu F[255];cin >> sendbuf;//sending data to the server send (Sockconn, SendBuf, strlen (SENDBUF), 0); Sendbuf[strlen (sendbuf)] = ' \0 '; if (strcmp (SendBuf, "n") = = 0) break; Close socket closesocket (sockconn);//Release Assigned network resource WSACleanup ();}




Windows Socket Basics

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.