Visual C + + network programming--TCP Communication

Source: Internet
Author: User
Tags htons

TCP Service Side

1#include <WinSock2.h>//Include header file2#include <stdio.h>3#include <Windows.h>4#include <iostream>5 #pragmaComment (lib, "Ws2_32.lib")//Display Connection Socket font6 intMain ()//main function Start7 {8Wsadata data;//defining Wsadata Structure Objects9WORD W=makeword (2,0);//Define version numberTen     Charsztext[]="welcome you \ r \ n";//defines and initializes an array of characters sent to the client OneWSAStartup (W,&data);//initializing the socket font ASOCKET s,s1;//define a connection socket and a data transceiver socket handle -S=socket (Af_inet,sock_stream,0);//Create a TCP socket -Sockaddr_in ADDR,ADDR2;//defining a socket address structure the     intn=sizeof(ADDR2);//Get socket address structure size -Addr.sin_family=af_inet;//Initialize the address structure -Addr.sin_port=htons ( the); -Addr.sin_addr. S_un. S_addr=Inaddr_any; +Bind (S, (sockaddr*) &addr,sizeof(addr));//binding Sockets -Listen (s),5);//Listening sockets +std::cout<<"server already started \ r"<<std::endl;//Output hint Information A      while(true) at     { -S1=accept (S, (sockaddr*) &addr2,&n);//Accept connection Request -         if(s1!=NULL) -         { -std::cout<<"already connected to the \ r"<<std::endl<<Inet_ntoa (addr2.sin_addr); -Send (S1,sztext,sizeof(Sztext),0);//send a character array to the client in         } - closesocket (s); toClosesocket (S1);//Close Socket handle +WSACleanup ();//Release Socket Font -         if(GetChar ())//If there is an input, close the program the         { *             return 0;//Normal shutdown program $         }Panax Notoginseng          Else -          { theSleep ( -);//application sleep 0.1 seconds +         } A  the     } +}

Run results

After the server program starts, if no client sends a connection request to it, the server waits until the client program is known to connect.

TCP Client

1#include <WinSock2.h>//Include header file2#include <stdio.h>3#include <Windows.h>4#include <iostream>5 #pragmaComment (lib, "Ws2_32.lib")//Display Connection Socket font6 intMain ()//main function Start7 {8Wsadata data;//defining Wsadata Structure Objects9WORD W=makeword (2,0);//Define version numberTen     Charsztext[Ten]={0};//defines and initializes an array of characters sent to the client OneWSAStartup (W,&data);//initializing the socket font ASOCKET s;//define a connection socket and a data transceiver socket handle -S=socket (Af_inet,sock_stream,0);//Create a TCP socket -Sockaddr_in addr;//defining a socket address structure theAddr.sin_family=af_inet;//Initialize the address structure -Addr.sin_port=htons ( the); -Addr.sin_addr. S_un. S_ADDR=INET_ADDR ("192.168.7.6"); -std::cout<<"client has started \ r"<<std::endl;//Output hint Information +Connect (S, (sockaddr*) &addr,sizeof(addr)); -Recv (S,sztext,sizeof(Sztext),0); +std::cout<<sztext<<Std::endl; AClosesocket (s);//Close Socket handle atWSACleanup ();//Release Socket Font -         if(GetChar ())//If there is an input, close the program -         { -             return 0;//Normal shutdown program -         } -         Else in         { -Sleep ( -);//application sleep 0.1 seconds to         } +  -      the}

Compile and run the program, open the server, and then open the client, the server will accept the client's request, the client will also display the welcome message.

Visual C + + network programming--TCP Communication

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.