本文整理汇总了C++中Net::sendData方法的典型用法代码示例。如果您正苦于以下问题:C++ Net::sendData方法的具体用法?C++ Net::sendData怎么用?C++ Net::sendData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Net
的用法示例。
在下文中一共展示了Net::sendData方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
USHORT port = DEFAULT_PORT;
USHORT remotePort = DEFAULT_PORT; // Remote port number
int newPort = DEFAULT_PORT;
int protocol; // TCP or UDP
char remoteIP[16]; // Remote IP address as dotted quad; nnn.nnn.nnn.nnn
char localIP[16]; // Local IP address as dotted quad; nnn.nnn.nnn.nnn
char netbuf[BUFSIZE]; // network receive
char keybuf[BUFSIZE]; // keyboard input
int error = netNS::NET_OK;
int lastError = netNS::NET_OK;
int sizeXmit=0; // transmit size
int sizeRecv=0; // receive size
int size=0;
Net net; // Network communication object
netbuf[0] = '\0';
// ----- create server -----
do
{
// display protocol menu
cout << "----- Chat Server -----\n"
<< "\nSelect Protocol\n"
<< " 0 = UDP\n"
<< " 1 = TCP\n\n"
<< " Choice: ";
cin >> protocol; // get character
}while(protocol != 0 && protocol != 1);
cin.ignore();
// get port number
// Port numbers 0-1023 are used for well-known services.
// Port numbers 1024-65535 may be freely used.
do
{
cout << "Enter port number (Use 0 for default 48161): ";
cin >> newPort;
}while(newPort < 0 || newPort > 65535);
if(newPort != 0)
port = newPort;
cin.ignore();
// create server
error = net.createServer(port, protocol);
if(error != netNS::NET_OK) // if error
{
cout << net.getError(error) << endl;
system("pause");
return 1;
}
// display serverIP
net.getLocalIP(localIP);
cout << "Server IP is: " << localIP << endl;
cout << "Server port is: " << port << endl;
// display incoming text and send back response
while(true) // ***** INFINITE LOOP *****
{
// check for receive
sizeRecv = BUFSIZE; // max bytes to receive
error = net.readData(netbuf,sizeRecv,remoteIP, remotePort);
if(error != netNS::NET_OK)
{
cout << net.getError(error) << endl;
error = net.closeSocket(); // close connection
// re-create server
error = net.createServer(port, protocol);
if(error != netNS::NET_OK) // if error
{
cout << net.getError(error) << endl;
return 1;
}
}
if(sizeRecv > 0) // if characters received
{
// display incomming message
cout << netbuf << endl;
}
// if keyboard input pending
if (_kbhit())
{
cin.getline(keybuf, BUFSIZE); // get input
sizeXmit += (int)cin.gcount() + 1; // get size of input + null
if(cin.fail()) // if failbit
{
cin.clear(); // clear error
cin.ignore(INT_MAX, '\n'); // flush input
}
}
// if data ready to send
if(sizeXmit > 0)
{
size = sizeXmit;
error = net.sendData(keybuf,size,remoteIP,remotePort); // send data
if(error != netNS::NET_OK)
cout << net.getError(error) << endl;
//.........这里部分代码省略.........
示例2: _tmain
//.........这里部分代码省略.........
player1circle.SetPosition(playerA.getPosition());
player2circle.SetPosition(playerB.getPosition());
try
{
//While the connection is not closed remotely
while(renderWindow.IsOpened() && !gameOver)
{
Event sfmlEvent;
while (renderWindow.PollEvent(sfmlEvent))
{
// Close window : exit
if (sfmlEvent.Type == Event::Closed)
{
renderWindow.Close();
}
else if (sfmlEvent.Type == Event::KeyPressed)
{
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::W))
{
if(player == 2)
{
playerA.update(1);
}
else if(player == 1)
{
playerB.update(1);
}
network.sendData("127.0.0.1",destPort, "W");
}
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::A))
{
if(player == 2)
{
playerA.update(2);
}
else if(player == 1)
{
playerB.update(2);
}
network.sendData("127.0.0.1",destPort, "A");
}
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::S))
{
if(player == 2)
{
playerA.update(3);
}
else if(player == 1)
{
playerB.update(3);
}
network.sendData("127.0.0.1",destPort, "S");
}
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::D))
{