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(, ), &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(); //指定端口
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(slisten, (struct sockaddr *)&my_addr, sizeof(my_addr));
//启动监听
listen(slisten, ); //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[];
while (true) {
Datablock client_send; cout << "输入(start)开始更改,输入(end)结束服务:" << endl;
cout << "输入发送内容:";
while (cin.getline(buf, ))
{ string rcover = buf;
if (rcover == "end")break;
if (rcover == "start") {
cout << "请以m1-m2-m3-m_roll-m_pitch-m_rotate格式输入数据:" << endl;
char transmit[];
cin.getline(transmit, );
send(sconnect, transmit, strlen(transmit) + , ); //字符串还有个结束标志
memset(transmit, , sizeof(transmit)); } }
return ;
}
客户端:(非阻塞式的)
首先要有宏(一般放在.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 = , index = -;
while (- != (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(, ), &wsaD);
//创建客户端socket
SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//连接服务器
u_long iMode = ;//如果设置为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(); //指定端口
cout << endl<<"OK" << endl; connect(sclient, (struct sockaddr*)&servaddr, sizeof(servaddr)); //通过套接字连接主机 char buf[]; Datablock resu_data;
while (true) {
//存储接收的字符
int len = recv(sclient, buf, sizeof(buf), ); //通过连接套接字接收数据 if (len > )
{
vector<string>result;
cout << "客户说:" << buf << endl;
string test = buf;
cout << test << endl;
splitEx(result, test, "-");
resu_data.m1 = str_float(result[]);
resu_data.m2 = str_float(result[]);
resu_data.m3 = str_float(result[]);
resu_data.m_roll = str_float(result[]);
resu_data.m_pitch = str_float(result[]);
resu_data.m_rotate = str_float(result[]);
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 ;
}
这是一个点对点的,先开server,后开客户端。