本文整理汇总了C++中UdpSocket::bind方法的典型用法代码示例。如果您正苦于以下问题:C++ UdpSocket::bind方法的具体用法?C++ UdpSocket::bind怎么用?C++ UdpSocket::bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UdpSocket
的用法示例。
在下文中一共展示了UdpSocket::bind方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
TEST(UdpSocket, bind) { // tests binding a socket to a port
UdpSocket server;
UdpSocket server2;
EXPECT_EQ(ClockError::SUCCESS, server.bind(12345));
EXPECT_EQ(ClockError::INVALID_USAGE, server.bind(12345));
EXPECT_EQ(ClockError::ADDRESS_INUSE, server2.bind(12345));
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
示例2: sender
void sender(std::uint16_t port, const std::string &message, bool enableLogging) {
std::vector<std::string> addresses;
getIPV4BroadcastAddresses(addresses);
if(enableLogging) {
for(std::string a : addresses) {
std::cout << a << std::endl;
}
}
UdpSocket socket;
socket.enableBroadcast();
socket.enableReuseAddr();
socket.bind();
Datagram datagram;
datagram.port = port;
std::strcpy(datagram.buffer, message.c_str());
datagram.bufferUsed = message.size() + 1;
while(true) {
for(const std::string address : addresses) {
datagram.address = address;
socket.send(datagram);
}
usleep(1000*1000);
}
}
示例3: main
int main(int argc, char** argv)
{
UdpSocket client;
client.bind(8002);
const char* message = "ahoj\n";
//client.write("224.3.29.71", 8001, message); multicast
/*int broadcastEnable=1;
int ret = setsockopt(client.get_handle(), SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
client.write("255.255.255.255", 8001, message); // broadcast
*/
printf("Sending to server: %s", message);
client.write("localhost", 8001, message);
sockaddr_in from_addr;
unsigned int from_len;
char buf[1024];
int size = client.readline(buf, sizeof(buf), (sockaddr*) &from_addr);
buf[size] = '\0';
printf("Received from server: %s", buf);
client.close();
return 0;
}
示例4: my_receive
int my_receive()
{
printf("==== Receive\n");
UdpSocket receiver;
DataBuffer buffer(4096);
buffer.set_data_size(4096);
YETI_Result result = receiver.bind(SocketAddress(IpAddress::Any, 9123));
if (YETI_FAILED(result)) {
fprintf(stderr, "bind() failed (%d)\n", result);
return result;
}
SocketAddress address;
result = receiver.receive(buffer, &address);
if (YETI_FAILED(result)) {
fprintf(stderr, "receive() failed (%d)\n", result);
return result;
}
String addr_string = address.get_ipaddress().to_string();
printf("received packet, size=%d, from %s:%d\n",
(int)buffer.get_data_size(),
(const char*)addr_string,
(int)address.get_port());
return 0;
}
示例5:
Program()
{
int r;
r = socket1.init();
socket1.setPort(10000);
socket1.bind();
socket1.setListener(this);
}
示例6: Execute
static typename T::VALUE_TYPE Execute(const typename T::VALUE_TYPE& value)
{
UdpSocket socket;
socket.bind(0, 0);
TestUdpSenderRunnable<T> testSender(socket.getSocketAddrInfo().port, value);
Thread thread;
thread.start(testSender);
UdpSocketInputStream<1450> inStream(socket);
typename T::VALUE_TYPE resultValue;
inStream >> resultValue;
thread.join();
return resultValue;
}
示例7: exec
void PTrackingBridge::exec() const
{
vector<Point32> averagedVelocities, positions, standardDeviations, velocities;
vector<int> heights, identities, widths;
UdpSocket receiverSocket;
InetAddress sender;
stringstream s;
string dataReceived, token;
float timeout;
int iterations, ret;
bool binding;
if (agentPort != -1)
{
binding = receiverSocket.bind(agentPort);
if (!binding)
{
ERR("Error during the binding operation, exiting..." << endl);
exit(-1);
}
}
else
{
ERR("Agent id not set, please check the launch file." << endl);
exit(-1);
}
INFO("PTracking bridge bound on port: " << agentPort << endl);
iterations = 0;
timeout = 0.05;
while (true)
{
ret = receiverSocket.recv(dataReceived,sender,timeout);
if (ret == -1)
{
ERR("Error in receiving message from: " << sender.toString());
continue;
}
averagedVelocities.clear();
heights.clear();
identities.clear();
positions.clear();
standardDeviations.clear();
velocities.clear();
widths.clear();
s.clear();
s.str("");
s << dataReceived;
/// Splitting message (each estimation is separated by the ';' character).
while (getline(s,token,';'))
{
Point32 averagedVelocity, position, standardDeviation, velocity;
stringstream tokenStream;
int height, identity, width;
tokenStream << token;
tokenStream >> identity >> position.x >> position.y >> standardDeviation.x >> standardDeviation.y >> width >> height >> velocity.x >> velocity.y >> averagedVelocity.x >> averagedVelocity.y;
identities.push_back(identity);
positions.push_back(position);
standardDeviations.push_back(standardDeviation);
widths.push_back(width);
heights.push_back(height);
velocities.push_back(velocity);
averagedVelocities.push_back(averagedVelocity);
}
TargetEstimations targetEstimations;
targetEstimations.header.seq = iterations++;
targetEstimations.header.stamp = ros::Time::now();
targetEstimations.identities = identities;
targetEstimations.positions = positions;
targetEstimations.standardDeviations = standardDeviations;
targetEstimations.widths = widths;
targetEstimations.heights = heights;
targetEstimations.velocities = velocities;
targetEstimations.averagedVelocities = averagedVelocities;
publisherTargetEstimations.publish(targetEstimations);
timeout = identities.size() > 0 ? 0.2 : 0.0;
}
}