本文整理汇总了C++中UdpSocket类的典型用法代码示例。如果您正苦于以下问题:C++ UdpSocket类的具体用法?C++ UdpSocket怎么用?C++ UdpSocket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UdpSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: FD_ZERO
int SnmpRouted::setup(fd_set& set)
{
FD_ZERO(&set);
int maxfd = listener.getfd();
FD_SET(maxfd, &set);
for(int i = 0; i < MaxRequest; i++)
{
UdpSocket *dest = reqTable[i].dest;
if(dest)
{
FD_SET(dest->getfd(), &set);
if(i > maxfd) maxfd = i;
}
}
return maxfd+1;
}
示例2: main
int main()
{
UdpSocket sock;
char msg[100];
int msgLen;
string reply="Server msg";
struct sockaddr_in clientSockAddr;
socklen_t sockLen;
sock.initServer(5000);
cout<<"Initialized the UDP server"<<endl;
msgLen = sock.serverRecv(msg,sizeof(msg),&clientSockAddr,&sockLen);
msg[msgLen] = 0;
cout<<"Client reply:"<<endl;
cout<<msg<<endl;
sock.serverSend(reply.c_str(),reply.length(),&clientSockAddr,sizeof(clientSockAddr));
}
示例3: serverUnreliable
// Test1: server unreliable message receive -----------------------------------
void serverUnreliable( UdpSocket &sock, const int max, int message[] ) {
cerr << "server unreliable test:" << endl;
// receive message[] max times
for ( int i = 0; i < max; i++ ) {
sock.recvFrom( ( char * ) message, MSGSIZE ); // udp message receive
cerr << message[0] << endl; // print out message
}
}
示例4: 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;
}
示例5: my_send
int my_send()
{
printf("==== Send\n");
char * msg = "hello";
UdpSocket sender;
DataBuffer buffer(1024);
buffer.set_data_size(1024);
buffer.set_data((YETI_Byte *)msg, strlen(msg));
IpAddress address;
address.resolve_name("localhost");
SocketAddress socket_address(address, 9123);
YETI_Result result = sender.send(buffer, &socket_address);
if (YETI_FAILED(result)) {
fprintf(stderr, "send() failed (%d)\n", result);
return result;
}
return 0;
}
示例6: clientUnreliable
// Test 1: client unreliable message send -------------------------------------
void clientUnreliable( UdpSocket &sock, const int max, int message[] ) {
cerr << "client: unreliable test:" << endl;
// transfer message[] max times
for ( int i = 0; i < max; i++ ) {
message[0] = i; // message[0] has a sequence #
sock.sendTo( ( char * )message, MSGSIZE ); // udp message send
cerr << "message = " << message[0] << endl;
}
}
示例7: serverEarlyRetrans
void serverEarlyRetrans(UdpSocket &sock, const int max, int message[],
int windowSize) {
cerr << "Server early retrans test:" << endl;
int ack; // Holds the acknowledgment
bool serverArray[max]; // Holds messages that server needs from client
int sequence = 0; // The sequence # counter
// Initialize all values in serverArray to false
for (int i = 0; i < max; i++)
serverArray[i] = false;
while (sequence < max) {
// Read the message from the client
sock.recvFrom((char *)message, MSGSIZE);
// Got needed message from client
if (message[0] == sequence) {
// Set the serverArray at sequence
serverArray[sequence] = true;
// Find the lowest number the server needs
while (sequence < max) {
if (serverArray[sequence] == false) {
break;
}
sequence++;
}
// Send an ACK for previous seq #
ack = sequence - 1;
}
else {
// Set the serverArray at message
serverArray[message[0]] = true;
// Send the number that is still needed
ack = sequence;
}
// Send the acknowledgment
sock.ackTo((char *)&ack, sizeof(ack));
}
}
示例8: serverReliable
void serverReliable( UdpSocket &sock, const int max, int message[] ) {
cerr << "server reliable test:" << endl;
// receive message[] max times
for ( int i = 0; i < max; ) {
sock.recvFrom( ( char * ) message, MSGSIZE ); // udp message receive
if ( message[0] >= max ) {
cerr << "ERROR: " << message[0] << " is larger than max\n";
break;
}
// Drops the message[] if its a duplicate
if ( message[0] == i ) {
sock.ackTo( ( char * ) message, MSGSIZE); // send ACK to client
//cerr << " ACKed: " << message[0] << endl;
i++;
}
}
}
示例9: sizeof
/*
* obj UdpSocket类型的指针
*/
void *UdpSocket::_runRecving(void* obj) {
UdpSocket *udpSocket = (UdpSocket *)obj;
struct sockaddr_in addr;
socklen_t addrLen = sizeof(struct sockaddr_in);
int errorNum;
char data[MAX_BUF_SIZE];
while (udpSocket->m_running) {
bzero(&addr, sizeof(struct sockaddr_in));
if((errorNum = recvfrom(udpSocket->m_socket, data,
MAX_BUF_SIZE, 0,
(struct sockaddr*)&addr, &addrLen)) < 0) {
printf("error num %d\n", errorNum);
continue;
}
data[errorNum] = '\0';
udpSocket->recvHandler(&addr, data);
}
return((void*)0);
}
示例10: Main
void UdpSendThread::Main() {
SocketThreadOperation *op = mSocketThreadOperation;
UdpSocket *s = (UdpSocket*)op->mSocket;
ErrorType error;
StopWatch sw;
sw.Start();
while (!ShouldEnd()) {
error = s->Send(op->mIPAddress, op->mByteStream, Math::ClampLong(
op->mTimeoutMS - sw.GetElapsedMilliseconds(), 0L, 16L));
if (0L == op->mTimeoutMS ||
(error && kErrorTimedOut != error) ||
(0L < op->mTimeoutMS &&
sw.GetElapsedMilliseconds() >= op->mTimeoutMS))
{
if (error)
Error::Throw(error, String("[%s(%p, %p)]",
FastFunctionName, this, op));
s->OnSend(op->mID, op->mIPAddress, error);
break;
}
}
}
示例11: main
int main(int argc, char* argv[])
{
/* Concept:
1) UdpSocket listens for input data.
2) Data is passed to H264Decoder which than parses
the NALU files and passes everything to ffmpeg.
The decoded data is than passed to the SdlViewer instance;
3) The SdlViewer uses SDL with OpenGL acceleration to show the received frames.
*/
for(int i = 1; i < argc; ++i) {
std::string value(argv[i]);
if (value == "--fullscreen") {
fullscreen = true;
}else if(value == "--oculus"){
viewer = new OculusViewer(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}else if (value == "--help" || value == "-h") {
cout << "--help no idea what exactly this parameter does" << endl
<< "--fullscreen open fullscreen opengl context instead of vga window" << endl
<< "--oculus adjust screen window for Oculus Rift DK2" << endl;
return 0;
}
}
if(viewer == nullptr){
viewer = new SdlViewer(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
input.initClient(cfg.getValueOfKey<string>("TARGET_IP").c_str(), TARGET_PORT);
input.setInitCallback(&init);
input.send(PROTOCOL_TYPE_INIT, nullptr, 0);
viewer->setInputCallback(&inputCallback);
viewer->setPositionCallback(&positionCallback);
viewer->show(fullscreen);
input.send(PROTOCOL_TYPE_CLOSE, nullptr, 0);
input.close();
return 0;
}
示例12: UdpSocket
void SnmpRouted::routeRequest()
{
try
{
Request req;
req.dest = new UdpSocket(destAddr);
req.expTime = time(0)+RequestTimeout;
if(req.dest)
{
req.dest->connect();
int nbytes = listener.bytesToRead(sizeof(dataGram));
listener.read(dataGram, nbytes);
req.origAddr = listener.getSender();
req.dest->write(dataGram, nbytes);
int fd = req.dest->getfd();
reqTable[fd] = req;
}
}
catch(NetErr& e)
{
syslog(LOG_ERR, "Request not routed: %s", e.errm);
}
}
示例13: clientStopWait
int clientStopWait( UdpSocket &sock, const int max, int message[] ) {
cerr << "Stop-and-wait test:" << endl;
Timer t;
int retransmits = 0;
for ( int i = 0; i < max; i++ ) { // transfer message[] max times
message[0] = i; // message[0] has a sequence #
sock.sendTo((char*) message, MSGSIZE); // send message
t.start(); // start timer
long lap;
while ((lap = t.lap()) < 1500) {
if(sock.pollRecvFrom() > 0) { // if ack received before timer ends
sock.recvFrom((char*)message, MSGSIZE);
break;
}
}
if (lap >= 1500) { // timeout, so retransmit
i--;
retransmits++;
cerr << retransmits << endl;
}
}
return retransmits;
}
示例14: serverEarlyRetrans
void serverEarlyRetrans( UdpSocket &sock, const int max, int message[], int windowSize ) {
cerr << "server sliding window:" << endl;
int seq = 0;
int lastSeq = -1;
int windowLoc = 0;
vector<int> window(windowSize);
for(int i = 0; i < windowSize; i++) { //init window
window[i] = -1;
}
while(seq < max) {
while( sock.pollRecvFrom() <=0 ){} // pause until message
sock.recvFrom( ( char * ) message, MSGSIZE ); // udp message receive
seq = *message; // get the seq # from beginning of msg
int index = seq % windowSize;
if (index < windowLoc || index >= windowSize) { // seq is outside window
ackPkt(lastSeq, sock); // ack last contiguous packet.
continue; // drop packet
} else if(index == windowLoc) { // seq is next contiguous packet
window[index] = seq; // store seq in 1st slot
lastSeq = seq;
while(window[windowLoc] > -1) { // while contiguous
lastSeq = window[windowLoc]; // store value in lastSeq
window[windowLoc] = -1; // clean up value to -1
windowLoc++; // increment window
windowLoc = windowLoc % windowSize; // mod window if needed
if (window[windowLoc] == -1) { // if next slot is empty
ackPkt(lastSeq, sock); // ack last contiguous packet
}
}
} else { // seq is within window, so store
window[index] = seq; // store seq in correct slot
ackPkt(lastSeq, sock); // ack last contiguous packet
}
if (seq == max-1)
break;
}
}
示例15: main
int main(){
if(!winSockDll.Init()){
std::cout<<"Couldn't load winsock"<<std::endl;
system("pause");
return 1;
}
connections = (Address*)calloc(64, sizeof(Address));
addr = Address("127.0.0.1", 2222);
sConnect = UdpSocket();
sConnect.Bind(addr);
std::cout << "Server started..." << std::endl;
char* buffer = new char[512];
bool clientexist = false;
while(true){
clientexist = false;
ZeroMemory(buffer, 512);
sConnect.Receive(buffer, 512, &storeAddr);
for(int i = 0; i < Counter; i++){
if(storeAddr.Equals(connections[i])){
std::cout << "Client["<<i<<"]: "<<buffer<<std::endl;
clientexist = true;
}
}
if(!clientexist){
connections[Counter] = storeAddr;
std::cout<<"New Client: "<<Counter<<std::endl;
std::cout << "Client["<<Counter<<"]: "<< buffer << std::endl;
Counter++;
}
}
winSockDll.Cleanup();
return 0;
}