1,首先实现服务器端创建套接字的代码实现
#ifndef _SERVER_H_
#define _SERVER_H_
#include <Winsock2.h>
class CServer
{
public:
CServer(void);
~CServer(void);
bool initSocket(); //加载套接字库
void sendMessageToClient(char* buffData,int size); //发送消息
private:
void createSocket();//创建套接字并进行绑定
void freeSocket(); //释放套接字
private:
WORD m_wVersionRequested;
WSADATA m_wsaData;
SOCKET m_sockSrv; //socket对象
sockaddr_in m_addrSrv; //套接字地址
SOCKADDR_IN m_addrClient; //用来接收客户端的地址信息
char m_sendBuf[100]; //发送的数据
char m_recvBuf[100]; //接受的数据
int m_addrLength;
};
#include "Server.h"
#include <iostream>
CServer::CServer(void):m_wVersionRequested(0)
{
memset(m_sendBuf,0,100);
m_addrLength = sizeof(SOCKADDR);
}
CServer::~CServer(void)
{
freeSocket();
}
bool CServer::initSocket()
{
m_wVersionRequested = MAKEWORD(1,1);
int err = WSAStartup(m_wVersionRequested, &m_wsaData);//错误会返回WSASYSNOTREADY
if(err != 0)
return false;
if(LOBYTE(m_wsaData.wVersion) != 1 || //低字节为主版本
HIBYTE(m_wsaData.wVersion) != 1) //高字节为副版本
{
WSACleanup();
return false;
}
createSocket();
std::cout << "服务端套接字加载成功" << std::endl;
return true;
}
void CServer::createSocket()
{
//创建用于监听的套接字
m_sockSrv = socket(AF_INET,SOCK_DGRAM,0);//失败会返回 INVALID_SOCKET
//定义sockSrv发送和接收数据包的地址
m_addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
m_addrSrv.sin_family = AF_INET;
m_addrSrv.sin_port = htons(6000);
//绑定套接字, 绑定到端口
bind(m_sockSrv,(SOCKADDR*)&m_addrSrv,sizeof(SOCKADDR));//会返回一个SOCKET_ERROR
}
void CServer::freeSocket()
{
closesocket(m_sockSrv);
WSACleanup();
}
char tempBuf[1024]; //存储中间信息数据
void CServer::sendMessageToClient(char* buffData,int size)
{
recvfrom(m_sockSrv,m_recvBuf,100,0,(SOCKADDR*)&m_addrClient,&m_addrLength);
sprintf_s(tempBuf,"%s say : %s",inet_ntoa(m_addrSrv.sin_addr),m_recvBuf);
printf("%s\n",tempBuf);
sendto(m_sockSrv,buffData,size,0,(SOCKADDR*)&m_addrClient,m_addrLength);
}
然后实现osg通过加载模型,实现模型每帧绕Z轴旋转一定的角度,产生姿态信息数据以便发送给客户端下面是服务端下osg的模型创建过程以及姿态信息的更新过程。
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Node>
#include <osg/Group>
#include <osg/MatrixTransform>
#include <osgGA/TrackballManipulator>
#include "Server.h"
class CMyNodeCallBack : public osg::NodeCallback
{
public:
explicit CMyNodeCallBack(CServer* client):m_pServer(client)
,m_angle(0.0)
{}
~CMyNodeCallBack()
{
if(m_pServer)
delete m_pServer;
m_pServer = nullptr;
}
void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osg::ref_ptr<osg::MatrixTransform> mt = dynamic_cast<osg::MatrixTransform*>(node);
if(mt)
{
if(m_angle > 360.0)
m_angle = 0.0;
mt->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(m_angle),osg::Z_AXIS));
m_angle++;
if(m_pServer)
{
osg::Matrix matrix = mt->getMatrix();
const int size = sizeof(osg::Matrix);
char buf[size];
memcpy(buf,&matrix(0,0),size);
m_pServer->sendMessageToClient(buf,size);
}
}
traverse(node,nv);
}
private:
double m_angle;
CServer* m_pServer;
};
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
{
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = 100;
traits->y = 100;
traits->width = 1024;
traits->height = 768;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->sharedContext = 0;
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(gc.get());
camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
camera->setDrawBuffer(buffer);
camera->setReadBuffer(buffer);
// add this slave camera to the viewer, with a shift left of the projection matrix
viewer->addSlave(camera.get());
}
CServer* pServer = new CServer;
pServer->initSocket();
CMyNodeCallBack* pMyNodeCallBack = new CMyNodeCallBack(pServer);
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::Node* pNode = osgDB::readNodeFile("cow.osg");
if(pNode)
{
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
mt->addChild(pNode);
mt->setUpdateCallback(pMyNodeCallBack);
root->addChild(mt);
}
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
viewer->setSceneData(root);
viewer->run();
return 0;
}
ok,下面是客户端的网络套接字的创建代码
#ifndef _CLIENT_H_
#define _CLIENT_H_
#include <Winsock2.h>
#include <osg/Matrix>
class CClient
{
public:
CClient(void);
~CClient(void);
bool initSocket(); //加载套接字库
void recvMessageFromServer(osg::Matrix& mat,int size);//收到消息
private:
void createSocket();//创建套接字并进行绑定
void freeSocket(); //释放套接字
private:
WORD m_wVersionRequested;
WSADATA m_wsaData;
SOCKET m_sockSrv; //socket对象
sockaddr_in m_addrSrv; //套接字地址
SOCKADDR_IN m_addrClient; //用来接收客户端的地址信息
char m_recvBuf[100]; //收到的数据
char m_sendBuf[100]; //发送的数据
int m_addrLength;
};
#endif //_CLIENT_H_
#include "Client.h"
#include <iostream>
CClient::CClient(void):m_wVersionRequested(0)
{
memset(m_recvBuf,0,100);
m_addrLength = sizeof(SOCKADDR);
}
CClient::~CClient(void)
{
freeSocket();
}
bool CClient::initSocket()
{
m_wVersionRequested = MAKEWORD(1,1);
int err = WSAStartup(m_wVersionRequested, &m_wsaData);//错误会返回WSASYSNOTREADY
if(err != 0)
return false;
if(LOBYTE(m_wsaData.wVersion) != 1 || //低字节为主版本
HIBYTE(m_wsaData.wVersion) != 1) //高字节为副版本
{
WSACleanup();
return false;
}
createSocket();
std::cout << "客户端套接字加载成功" << std::endl;
return true;
}
void CClient::createSocket()
{
//创建用于监听的套接字
m_sockSrv = socket(AF_INET,SOCK_DGRAM,0);//失败会返回 INVALID_SOCKET
//定义sockSrv发送和接收数据包的地址
m_addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
m_addrSrv.sin_family = AF_INET;
m_addrSrv.sin_port = htons(6000);
}
void CClient::freeSocket()
{
closesocket(m_sockSrv);
WSACleanup();
}
char tempBuf[100]; //存储中间信息数据
void CClient::recvMessageFromServer(osg::Matrix& mat,int size)
{
sendto(m_sockSrv,m_sendBuf,strlen(m_sendBuf)+1,0,(SOCKADDR*)&m_addrSrv,m_addrLength);
//等待并接受数据
recvfrom(m_sockSrv,m_recvBuf,size,0,(SOCKADDR*)&m_addrSrv,&m_addrLength);
memcpy(&mat(0,0),m_recvBuf,sizeof(mat));
sprintf_s(tempBuf,"%s say : %s",inet_ntoa(m_addrSrv.sin_addr),m_recvBuf);
printf("%s\n",tempBuf);
}
下面就是通过接受由服务端发过来的数据作用于客户端创建的模型的姿态上即可,代码如下:
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Node>
#include <osg/Group>
#include <osg/MatrixTransform>
#include <osgGA/TrackballManipulator>
#include <osg/NodeCallback>
#include "Client.h"
class CMyNodeCallBack : public osg::NodeCallback
{
public:
explicit CMyNodeCallBack(CClient* client):m_pClient(client){}
~CMyNodeCallBack()
{
if(m_pClient)
delete m_pClient;
m_pClient = nullptr;
}
void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osg::ref_ptr<osg::MatrixTransform> mt = dynamic_cast<osg::MatrixTransform*>(node);
if(mt)
{
if(m_pClient)
{
osg::Matrix mat;
//接受服务端发来的姿态信息更新给现在的模型上。
m_pClient->recvMessageFromServer(mat,sizeof(osg::Matrix));
mt->setMatrix(mat);
}
}
traverse(node,nv);
}
private:
CClient* m_pClient;
};
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
{
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = 100;
traits->y = 100;
traits->width = 1024;
traits->height = 768;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->sharedContext = 0;
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(gc.get());
camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
camera->setDrawBuffer(buffer);
camera->setReadBuffer(buffer);
// add this slave camera to the viewer, with a shift left of the projection matrix
viewer->addSlave(camera.get());
}
osg::ref_ptr<osg::Group> root = new osg::Group;
CClient* pClient = new CClient;
pClient->initSocket();
CMyNodeCallBack* pMyNodeCallBack = new CMyNodeCallBack(pClient);
osg::Node* pNode = osgDB::readNodeFile("cow.osg");
if(pNode)
{
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
mt->addChild(pNode);
mt->setUpdateCallback(pMyNodeCallBack);
root->addChild(mt);
}
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
viewer->setSceneData(root);
viewer->run();
return 0;
}
效果图如下: