Socket-window Communications

Source: Internet
Author: User

Tag:rtu   erro   ted   send    Escape      Equipment    std   tcp   comm   

#define _WINSOCK_DEPRECATED_NO_WARNINGS#INCLUDE&LT;WINSOCK2. H> #include <stdio. h> #include <iostream> #include <string> #include <thread> #include <mutex>using namespace std; #pragma comment (lib, "Ws2_32.lib") int main () {WORD sockversion = Makeword (2, 2); Wsadata data;if (WSAStartup (sockversion, &data)! = 0) {return 0;} int i = 0;while (true) {std::cout << ++i << Std::endl; Socket sclient = socket (af_inet, sock_stream, ipproto_tcp); if (sclient = = Invalid_socket) {printf ("INVALID socket!"); return 0;} sockaddr_in seraddr;seraddr.sin_family = Af_inet;seraddr.sin_port = Htons (2115); seraddr.sin_addr. S_un. S_ADDR = inet_addr (""), if (Connect (sclient, (SOCKADDR *) &seraddr, sizeof (seraddr)) = = Socket_error) {/ /Connection Failed printf ("Connect error!"); Closesocket (sclient); return 0;} When communicating with the device, note that the start/end character//\X2 is the Universal escape format of Askii std::string sdata ("\x002set parameter 1\x003"); const char * senddata;senddata = SDATA.C_STR (); int sta = Send (Sclient, SendData, strlen (SenddaTA), 0); if (sta = =-1) {std::cout << "send failed!" << Std::endl;} char recdata[255];int ret = recv (sclient, Recdata, 255, 0); if (ret>0) {Recdata[ret] = 0x00;std::string result (recdata); }closesocket (sclient);} WSACleanup (); return 0;}


Socket-window Communications

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: 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.