[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] opfangning af UDP pakker på bestemt port!



Kim Schulz wrote:

> Hejsa
> jeg skal til at lave et lille program til Linux i C++, og skal i den
> forbindelse kunne opfange DUP pakker på en bestemt port!
> Er der nogen der har et lille eksempel eller evt. et andet (ikke alt for
> avanceret) program der kan dette???

Hejsan!
Jeg tror at dette C++ eksempel ikke kan gøres meget mindre.
Eksemplet er en såkaldt "popular greeting"
(strengt taget er svaret WWWorld! forkert -- wwworld! havde været mere
korrekt -- men da jeg samtidigt ønsker at "pay homage" til den moderne
"greeting": "Hello World!" (i modsætning til den traditonelle: "hello
world!") er WWWorld! bibeholdt).
Feinschmeckere vil naturligvis kunne argumentere at den eneste "greeting"
der i virkeligheden sker er "Hello LLLoopback".

Bemærk at eksemplet ikke belaster netværket ved at medsende det \0 der
afslutter en extern "C" streng
(doesn't contaminate your bodily fluids with a trailing NULL (ISO/IEC C++
definition assumed)):

// -*- C++ -*- source file
// $Id$
// datagram_server.cc:  implementation file

#include <ostream.h>

extern "C" {
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
}

extern "C++" {
  int main() {
    int sock= socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    const sockaddr_in
      server_addr= { AF_INET, htons(5000), { htonl(INADDR_ANY) } };
    bind(sock,
  reinterpret_cast<const sockaddr*>(&server_addr), sizeof(server_addr));

    sockaddr_in client_addr;
    socklen_t client_addr_len;

    const size_t max_msg_size= 512;
    char buf[max_msg_size];
    cout.write(buf,
        recvfrom(sock, buf, max_msg_size, 0,
   reinterpret_cast<sockaddr*>(&client_addr),
   &client_addr_len));

    const char* msg= "WWWorld!";
    sendto(sock, msg, strlen(msg), 0,
    reinterpret_cast<const sockaddr*>(&client_addr),
    sizeof(sockaddr_in));

    close(sock);
  }

} // extern "C++"
// datagram_server.cc


// -*- C++ -*- source file
// $Id$
// datagram_client.cc:  implementation file

#include <ostream.h>

extern "C" {
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
}

extern "C++" {
  int main() {
    int sock= socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);

    const sockaddr_in
      server_addr= { AF_INET, htons(5000), { htonl(INADDR_LOOPBACK) } };

    const char* msg= "Hello";
    sendto(sock, msg, strlen(msg), 0,
    reinterpret_cast<const sockaddr*>(&server_addr),
    sizeof(sockaddr_in));

    const size_t max_msg_len= 512;
    char buf[max_msg_len];
    cout.write(buf,
        recvfrom(sock, buf, max_msg_len, 0, 0, 0));

    close(sock);
  }

} // extern "C++"
// datagram_client.cc



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *