How do I create RAW TCP/IP packets in C++?

后端 未结 11 1351
有刺的猬
有刺的猬 2020-12-24 06:59

I\'m a beginning C++ programmer / network admin, but I figure I can learn how to do this if someone points me in the right direction. Most of the tutorials are demonstrated

相关标签:
11条回答
  • 2020-12-24 07:40

    Start by reading Beej's guide on socket programming . It will bring you up to speed on how to start writing network code. After that you should be able to pick up more and more information from reading the man pages.

    Most of it will consist of reading the documentation for your favourite library, and a basic understanding of man pages.

    0 讨论(0)
  • 2020-12-24 07:44

    A good place to start would be to use Asio which is a great cross-platform (incl Linux) library for network communication.

    0 讨论(0)
  • 2020-12-24 07:44

    You do this the exact same way you would in regular C, there is no C++ specific way to do this in the standard library.

    The tutorial I used for learning this was http://beej.us/guide/bgnet/. It was the best tutorial I found after looking around, it gave clear examples and good explanations of all functions it describes.

    0 讨论(0)
  • 2020-12-24 07:44

    easy:

    #include <sys/socket.h>
    #include <sys/types.h>
    
    int socket(int protocolFamily, int Type, int Protocol)
    // returns a socket descriptor
    
    int bind(int socketDescriptor, struct sockaddr* localAddress, unsigned int addressLength)
    // returns 0 
    

    ...etc.

    it's all in sys/socket.h

    0 讨论(0)
  • 2020-12-24 07:56

    Poster, please clarify your question.

    Almost all responses seem to think you're asking for a sockets tutorial; I read your question to mean you need to create a raw socket capable of sending arbitrary IP packets. As I said in my previous answer, some OSes restrict the use of raw sockets.

    http://linux.die.net/man/7/raw "Only processes with an effective user ID of 0 or the CAP_NET_RAW capability are allowed to open raw sockets."

    0 讨论(0)
  • 2020-12-24 08:00

    There are tons of references on this (of course, Stevens' book comes to mind), but I found the Beej guide to be incredibly useful for getting started. It's meaty enough that you can understand what's really happening, but it's simple enough that it doesn't take you several days to write a 'hello world' udp client/server.

    0 讨论(0)
提交回复
热议问题