From 60e3dd008445b4f08a8d67a0ce8dfb3091763cd1 Mon Sep 17 00:00:00 2001 From: zcn1123 <2363211205@qq.com> Date: Sat, 29 Aug 2020 14:50:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=BE=93=E5=85=A5IP=E5=92=8C?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=EF=BC=8C=E8=BF=9E=E6=8E=A5=E4=BB=8E=E7=AB=99?= =?UTF-8?q?=EF=BC=8C=E5=92=8C=E4=BB=8E=E7=AB=99=E9=80=9A=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modbus_TCP/TCP_client.cpp | 31 ++++---- Modbus_communication/Modbus_TCP/TCP_client.h | 2 +- Modbus_communication/Modbus_TCP/common.cpp | 72 +++++++++++++------ Modbus_communication/Modbus_TCP/common.h | 5 +- Modbus_communication/Modbus_TCP/main.cpp | 13 +++- 5 files changed, 81 insertions(+), 42 deletions(-) diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp index 419b5ad..2c11a15 100644 --- a/Modbus_communication/Modbus_TCP/TCP_client.cpp +++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp @@ -1,34 +1,33 @@ #include "TCP_client.h" -SOCKET Init_client(void) +SOCKET Init_client(string IP, unsigned int Port_number) { if (InitSocket_Version() == 0) return INVALID_SOCKET; - SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0); - sockaddr_in serversock_in; - serversock_in.sin_addr.S_un.S_addr = inet_addr("10.10.0.105"); - serversock_in.sin_family = AF_INET; - serversock_in.sin_port = htons(3000); - if (SOCKET_ERROR == connect(clientSocket, (SOCKADDR*)&serversock_in, sizeof(SOCKADDR))) + SOCKET ClientSocket = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in Serversock_in; + Serversock_in.sin_addr.S_un.S_addr = inet_addr(IP.c_str()); + Serversock_in.sin_family = AF_INET; + Serversock_in.sin_port = htons(Port_number); + while (SOCKET_ERROR == connect(ClientSocket, (SOCKADDR*)&Serversock_in, sizeof(SOCKADDR))) { - cout << "连接从站失败" << endl; - return INVALID_SOCKET; + cout << "尝试连接TCP从站失败" << endl; } - return clientSocket; + cout << "连接TCP从站成功" << endl; + return ClientSocket; } -int Tcp_client(void) +int Tcp_client(string IP, unsigned int Port_number) { - - SOCKET clientSocket = Init_client(); + SOCKET clientSocket = Init_client(IP, Port_number); if (INVALID_SOCKET == clientSocket) { cout << "初始化主站失败" << endl; - return 0; + return false; } while (true) { - char receiveBuf[100]; + char receiveBuf[260]; memset(receiveBuf, 0, 100); recv(clientSocket, receiveBuf, 100, 0); printf("%s\n", receiveBuf); @@ -38,5 +37,5 @@ int Tcp_client(void) closesocket(clientSocket); //关闭服务 WSACleanup(); - return 0; + return false; } diff --git a/Modbus_communication/Modbus_TCP/TCP_client.h b/Modbus_communication/Modbus_TCP/TCP_client.h index e65650c..8e40027 100644 --- a/Modbus_communication/Modbus_TCP/TCP_client.h +++ b/Modbus_communication/Modbus_TCP/TCP_client.h @@ -3,6 +3,6 @@ #include "common.h" -int Tcp_client(void); +int Tcp_client(string IP, unsigned int Port_number); #endif \ No newline at end of file diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp index 4ce27e0..d35802b 100644 --- a/Modbus_communication/Modbus_TCP/common.cpp +++ b/Modbus_communication/Modbus_TCP/common.cpp @@ -1,9 +1,15 @@ -#include "common.h" - +锘#include "common.h" +/********************************************************************************************* +*聽鍔熻兘聽聽聽聽锛 妫娴媠ocket鐗堟湰 +*聽鎻忚堪 聽聽锛 妫娴媠ocket鐗堟湰鏄惁涓鑷 +* 杈撳叆 锛 鏃 +* 杈撳嚭 锛 true socket鐗堟湰涓鑷 +* false socket鐗堟湰涓嶄竴鑷 +*********************************************************************************************/ bool InitSocket_Version(void) { - WORD sockVersion = MAKEWORD(2, 2);//使用winsocket2.2版本 + WORD sockVersion = MAKEWORD(2, 2);//浣跨敤winsocket2.2鐗堟湰 WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) { @@ -12,41 +18,63 @@ bool InitSocket_Version(void) return true; } -bool Check_IP(char* IP) +/********************************************************************************************* +*聽鍔熻兘聽聽聽聽锛 IP鍦板潃鏈夋晥鎬ф娴 +*聽鎻忚堪 聽聽锛 妫娴嬭緭鍏ョ殑IP鍦板潃鏄惁鍚堟硶 +* 杈撳叆 锛 IP 杈撳叆鐨処P鍦板潃 +* 杈撳嚭 锛 true IP鍦板潃鍚堟硶 +* false IP鍦板潃闈炴硶 +*********************************************************************************************/ +bool Check_IP(string IP) { int s[4]; string ip = IP; - if (ip.length() < 7 || ip.length() > 15) //长度判定 + if (ip.length() < 7 || ip.length() > 15) //闀垮害鍒ゅ畾 return false; - if (sscanf_s(IP, "%d.%d.%d.%d", &s[0], &s[1], &s[2], &s[3]) != 4) //IPV4格式正确 + if (sscanf_s(IP.c_str(), "%d.%d.%d.%d", &s[0], &s[1], &s[2], &s[3]) != 4) //IPV4鏍煎紡姝g‘ { return false; } string newip = to_string(s[0]) + "." + to_string(s[1]) + "." + to_string(s[2]) + "." + to_string(s[3]); - if (ip != newip) //前导0 + if (ip != newip) //鍓嶅0 return false; - if ((s[0] & 0xffffff00) || (s[1] & 0xffffff00) || (s[2] & 0xffffff00) || (s[3] & 0xffffff00)) //判断每一段大小是否符合要求 + if ((s[0] & 0xffffff00) || (s[1] & 0xffffff00) || (s[2] & 0xffffff00) || (s[3] & 0xffffff00)) //鍒ゆ柇姣忎竴娈靛ぇ灏忔槸鍚︾鍚堣姹 { return false; } return true; } -void Input_IP(vector & a) +/********************************************************************************************* +*聽鍔熻兘聽聽聽聽锛 鑾峰彇浠庣珯IP鍦板潃鍜岀鍙e彿 +*聽鎻忚堪 聽聽锛 缁堢杈撳叆浠庣珯IP鍦板潃鍜岀鍙e彿 +* 杈撳叆 锛 IP 鍦板潃 锛圛PV4锛 +* *Port_number 绔彛鍙凤紙1-65535锛 +* 杈撳嚭 锛 鏃 +*********************************************************************************************/ +void Input_IP(string& IP, unsigned int *Port_number) { - cout << "请输入从站IP:" << endl; - char IP[100]; - while (cin >> IP) // ctrl + Z + Enter 结束输入 + int i = 1; + do { - if (Check_IP(IP)) - { - a.push_back(IP); - memset(IP, 0, 100); - } - else + if (i == 1) { - cout << "输入IP格式错误,请重新输入" << endl; - memset(IP, 0, 100); + cout << "璇疯緭鍏ヤ粠绔橧P锛"; + i = 0; } - } -} \ No newline at end of file + else + cout << "IP鍦板潃鏍煎紡涓嶆纭紝璇烽噸鏂拌緭鍏ヤ粠绔橧P锛"; + cin >> IP; + } while (!Check_IP(IP)); + + i = 1; + cout << "IP鍦板潃杈撴牸寮忓叆姝g‘锛岃杈撳叆浠庣珯绔彛鍙凤細"; + do + { + if (i != 1) + cout << "璇烽噸鏂拌緭鍏ヤ粠绔欑鍙e彿锛"; + cin >> *Port_number; + i = 0; + } while (*Port_number == 0 || *Port_number > 65535); //绔彛涓嶈兘涓0 锛岀鍙e彿鑼冨洿0---65535 + +} diff --git a/Modbus_communication/Modbus_TCP/common.h b/Modbus_communication/Modbus_TCP/common.h index f163f25..8d7c125 100644 --- a/Modbus_communication/Modbus_TCP/common.h +++ b/Modbus_communication/Modbus_TCP/common.h @@ -10,8 +10,9 @@ #pragma comment(lib,"ws2_32.lib") using namespace std; -bool InitSocket_Version(void); + bool Check_IP(char* IP); -void Input_IP(vector & a); +bool InitSocket_Version(void); +void Input_IP(string& IP, unsigned int *Port_number); #endif \ No newline at end of file diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp index f82d79a..bf05931 100644 --- a/Modbus_communication/Modbus_TCP/main.cpp +++ b/Modbus_communication/Modbus_TCP/main.cpp @@ -1,9 +1,20 @@ #include "main.h" +string input() +{ + string str; + cout << "请输入string内容:" << endl; + getline(cin, str, '\n'); + cout << str << endl; + return str; +} int main() { - + string IP; + unsigned int Port_number; + Input_IP(IP, &Port_number); + Tcp_client(IP, Port_number); getchar(); return 0; } \ No newline at end of file