C++Socket通信(客户端和服务器)
win32控制台版
获取本机IP:
cmd: ipconfig/all
我写的是一个服务器和客户端点对点的信息传递(实现相互的可以适当更改代码):
服务器发送给客户端的是一个结构体数据
typedef struct Date { float m1; float m2; float m3; float m_roll; float m_pitch; float m_rotate; }Datablock;
上服务器代码(阻塞式的):
#include "stdafx.h" #include <iostream> #include <string> #include <winsock2.h> #pragma comment(lib,"ws2_32.lib") using std::cout; using std::cin; using std::string; using std::endl; typedef struct Date { float m1; float m2; float m3; float m_roll; float m_pitch; float m_rotate; }Datablock; int main() { cout << "服务器启动!....." << endl; //初始化winsock WSADATA wsaD; WSAStartup(MAKEWORD(1, 1), &wsaD); //创建监听套接字 SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // 绑定主机地址和监听套接字 struct sockaddr_in my_addr; //主机地址 my_addr.sin_family = AF_INET; my_addr.sin_port = htons(8088); //指定端口 my_addr.sin_addr.s_addr = htonl(INADDR_ANY); bind(slisten, (struct sockaddr *)&my_addr, sizeof(my_addr)); //启动监听 listen(slisten, 5); //5是最大连接数 //开始接收数据 struct sockaddr_in c_addr; //客户地址 int addr_len = sizeof(c_addr); SOCKET sconnect = accept(slisten, (struct sockaddr*)&c_addr, &addr_len); //通过监听套接字创建连接套接字 cout << "连接已建立......" << endl; char buf[500]; while (true) { Datablock client_send; cout << "输入(start)开始更改,输入(end)结束服务:" << endl; cout << "输入发送内容:"; while (cin.getline(buf, 500)) { string rcover = buf; if (rcover == "end")break; if (rcover == "start") { cout << "请以m1-m2-m3-m_roll-m_pitch-m_rotate格式输入数据:" << endl; char transmit[100]; cin.getline(transmit, 100); send(sconnect, transmit, strlen(transmit) + 1, 0); //字符串还有个结束标志 memset(transmit, 0, sizeof(transmit)); } } return 0; }
客户端:(非阻塞式的)
首先要有宏(一般放在.h文件的头文件区域)
#include "stdafx.h" #include <iostream> #include <string> #include<cstring> #include <winsock2.h> #include <WS2tcpip.h> #include<vector> #include<sstream> #pragma comment(lib,"ws2_32.lib") using namespace std; typedef struct Date { float m1; float m2; float m3; float m_roll; float m_pitch; float m_rotate; }Datablock; void splitEx(vector<string>&strs, const string& src, string separate_character) { int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符 int lastPosition = 0, index = -1; while (-1 != (index = src.find(separate_character, lastPosition))) { strs.push_back(src.substr(lastPosition, index - lastPosition)); lastPosition = index + separate_characterLen; } string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容 if (!lastString.empty()) strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队 } float str_float(string a) { float b; stringstream ss; //string 转 int ss << a; ss >> b; return b; } int main() { cout << "客户端启动" << endl; //初始化winsock WSADATA wsaD; WSAStartup(MAKEWORD(1, 1), &wsaD); //创建客户端socket SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //连接服务器 u_long iMode = 1;//如果设置为0,为阻塞式的 ioctlsocket(sclient, FIONBIO, &iMode); struct sockaddr_in servaddr; servaddr.sin_family = AF_INET; cout << "请输入局域网IP地址:" << endl; string ipaddress; cin >> ipaddress; cout << ipaddress; servaddr.sin_addr.s_addr = inet_addr(ipaddress.c_str()); //指定服务器 servaddr.sin_port = htons(8088); //指定端口 cout << endl<<"OK" << endl; connect(sclient, (struct sockaddr*)&servaddr, sizeof(servaddr)); //通过套接字连接主机 char buf[500]; Datablock resu_data; while (true) { //存储接收的字符 int len = recv(sclient, buf, sizeof(buf), 0); //通过连接套接字接收数据 if (len > 0) { vector<string>result; cout << "客户说:" << buf << endl; string test = buf; cout << test << endl; splitEx(result, test, "-"); resu_data.m1 = str_float(result[0]); resu_data.m2 = str_float(result[1]); resu_data.m3 = str_float(result[2]); resu_data.m_roll = str_float(result[3]); resu_data.m_pitch = str_float(result[4]); resu_data.m_rotate = str_float(result[5]); cout << resu_data.m1 << endl; cout << resu_data.m2 << endl; cout << resu_data.m3 << endl; cout << resu_data.m_roll << endl; cout << resu_data.m_pitch<< endl; cout << resu_data.m_rotate << endl; } } return 0; }
这是一个点对点的,先开server,后开客户端。