<p>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 using old code that no longer works for some reason. </p>

<p>Since I'm on Linux, all I need is an explanation on how to write raw Berkeley sockets. Can someone give me a quick run down?</p>

[{'answer_id': 78204, 'author': 'Tom Leys', 'author_id': 11440, 'author_profile': 'https://Stackoverflow.com/users/11440', 'pm_score': 2, 'selected': False, 'text': '<p>A good place to start would be to use <a href="http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/overview.html" rel="nofollow noreferrer">Asio</a> which is a great cross-platform (incl Linux) library for network communication.</p>\n'}, {'answer_id': 78206, 'author': 'X-Istence', 'author_id': 13986, 'author_profile': 'https://Stackoverflow.com/users/13986', 'pm_score': 4, 'selected': False, 'text': '<p>Start by reading <a href="http://beej.us/guide/bgnet/output/html/multipage/index.html" rel="noreferrer">Beej\'s guide on socket programming</a> . 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.</p>\n\n<p>Most of it will consist of reading the documentation for your favourite library, and a basic understanding of man pages.</p>\n'}, {'answer_id': 78208, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 1, 'selected': False, 'text': '<p>There are tons of references on this (of course, Stevens\' book comes to mind), but I found the <a href="http://beej.us/guide/bgnet/output/html/multipage/index.html" rel="nofollow noreferrer">Beej guide</a> 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.</p>\n'}, {'answer_id': 78209, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>easy:</p>\n\n<pre><code>#include &lt;sys/socket.h&gt;\n#include &lt;sys/types.h&gt;\n\nint socket(int protocolFamily, int Type, int Protocol)\n// returns a socket descriptor\n\nint bind(int socketDescriptor, struct sockaddr* localAddress, unsigned int addressLength)\n// returns 0 \n</code></pre>\n\n<p>...etc.</p>\n\n<p>it's all in sys/socket.h</p>\n"}, {'answer_id': 78211, 'author': 'HappySmileMan', 'author_id': 14073, 'author_profile': 'https://Stackoverflow.com/users/14073', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>The tutorial I used for learning this was <a href="http://beej.us/guide/bgnet/" rel="nofollow noreferrer">http://beej.us/guide/bgnet/</a>. It was the best tutorial I found after looking around, it gave clear examples and good explanations of all functions it describes.</p>\n'}, {'answer_id': 78251, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 3, 'selected': False, 'text': '<p><strong>For TCP client side:</strong></p>\n\n<p>Use gethostbyname to lookup dns name to IP, it will return a hostent structure.  Let\'s call this returned value host. </p>\n\n<pre><code>hostent *host = gethostbyname(HOSTNAME_CSTR);\n</code></pre>\n\n<p>Fill the socket address structure:</p>\n\n<pre><code>sockaddr_in sock;\nsock.sin_family = AF_INET;\nsock.sin_port = htons(REMOTE_PORT);\nsock.sin_addr.s_addr = ((struct in_addr *)(host-&gt;h_addr))-&gt;s_addr;\n</code></pre>\n\n<p>Create a socket and call connect:</p>\n\n<pre><code>s = socket(AF_INET, SOCK_STREAM, 0); \nconnect(s, (struct sockaddr *)&amp;sock, sizeof(sock))\n</code></pre>\n\n<p><strong>For TCP server side:</strong></p>\n\n<p>Setup a socket</p>\n\n<p>Bind your address to that socket using bind.</p>\n\n<p>Start listening on that socket with listen</p>\n\n<p>Call accept to get a connected client.  &lt;-- at this point you spawn a new thread to handle the connection while you make another call to accept to get the next connected client. </p>\n\n<p><strong>General communication:</strong></p>\n\n<p>Use send and recv to read and write between the client and server.</p>\n\n<p><strong>Source code example of BSD sockets:</strong></p>\n\n<p>You can find some good example code of this at <a href="http://en.wikipedia.org/wiki/Berkeley_sockets" rel="nofollow noreferrer">wikipedia</a>.</p>\n\n<p><strong>Further reading:</strong></p>\n\n<p>I highly recommend <a href="https://rads.stackoverflow.com/amzn/click/com/0131411551" rel="nofollow noreferrer" rel="nofollow noreferrer">this book</a> and <a href="http://beej.us/guide/bgnet/output/html/multipage/index.html" rel="nofollow noreferrer">this online tutorial</a>:</p>\n\n<p><img src="https://i.stack.imgur.com/CIP1b.jpg" alt="alt text"></p>\n\n<p><a href="https://i.stack.imgur.com/CIP1b.jpg" rel="nofollow noreferrer">4</a>: </p>\n'}, {'answer_id': 78273, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Poster, please clarify your question.</p>\n\n<p>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.\nAs I said in my previous answer, some OSes restrict the use of raw sockets.</p>\n\n<p><a href="http://linux.die.net/man/7/raw" rel="nofollow noreferrer">http://linux.die.net/man/7/raw</a>\n"Only processes with an effective user ID of 0 or the CAP_NET_RAW capability are allowed to open raw sockets."</p>\n'}, {'answer_id': 78294, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>For starters, it would be helpful if you clarify what you mean by "raw".  Traditionally this means that you want to craft all of the layer 4 header (TCP/UDP/ICMP... header), and perhaps some of the IP header on your own.  For this you will need more than beej\'s tutorial which has been mentioned my many here already.  In this case you want raw IP sockets obtained using the SOCK_RAW argument in your call to socket (see <a href="http://mixter.void.ru/rawip.html" rel="noreferrer">http://mixter.void.ru/rawip.html</a> for some basics).</p>\n\n<p>If what you really want is just to be able to establish a TCP connection to some remote host such as port 80 on stackoverflow.com, then you probably don\'t need "raw" sockets and beej\'s guide will serve you well.</p>\n\n<p>For an authoritative reference on the subject, you should really pick up Unix Network Programming, Volume 1: The Sockets Networking API (3rd Edition) by Stevens et al.</p>\n'}, {'answer_id': 78349, 'author': 'Charles Graham', 'author_id': 7705, 'author_profile': 'https://Stackoverflow.com/users/7705', 'pm_score': 1, 'selected': False, 'text': '<p>Read <a href="https://rads.stackoverflow.com/amzn/click/com/0131411551" rel="nofollow noreferrer" rel="nofollow noreferrer">Unix Network Programming</a> by Richard Stevens.  It\'s a must.  It explains how it all works, gives you code, and even gives you helper methods.  You might want to check out some of his other books.  <a href="https://rads.stackoverflow.com/amzn/click/com/0201433079" rel="nofollow noreferrer" rel="nofollow noreferrer">Advanced Programming In The Unix Enviernment</a> is a must for lower level programming in Unix is general.  I don\'t even do stuff on the Unix stack anymore, and the stuf from these books still helps how I code.</p>\n'}, {'answer_id': 595529, 'author': 'jdizzle', 'author_id': 70603, 'author_profile': 'https://Stackoverflow.com/users/70603', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://www.tcpdump.org/" rel="nofollow noreferrer">Libpcap</a> will let you craft complete packets (layer2 through layer 7) and send them out over the wire.  As fun as that sounds, there\'s some caveats with it.  You need to create all the appropriate headers and do all the checksumming yourself.  <a href="http://libnet.sourceforge.net/" rel="nofollow noreferrer">Libnet</a> can help with that, though.</p>\n\n<p>If you want to get out of the C++ programming pool, there is <a href="http://www.secdev.org/projects/scapy/" rel="nofollow noreferrer">scapy</a> for python.  It makes it trivial to craft and transmit TCP/IP packets.</p>\n'}, {'answer_id': 14752331, 'author': 'mfontanini', 'author_id': 525217, 'author_profile': 'https://Stackoverflow.com/users/525217', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve been developing <a href="http://libtins.github.io/" rel="nofollow">libtins</a> for the past year. It\'s a high level C++ packet crafting and sniffing library. </p>\n\n<p>Unless you want to reinvent the wheel and implement every protocol\'s internals, I\'d recommend you to use some higher level library which already does that for you.</p>\n'}, {'answer_id': 69956983, 'author': 'Arseny Semenov', 'author_id': 16639307, 'author_profile': 'https://Stackoverflow.com/users/16639307', 'pm_score': 1, 'selected': False, 'text': '<p>in any case, if you want to use the tcp/ip stack, their headers will be added to your packet. If you want to control the packet down to the byte, I advise you npcap. Libpcap, unfortunately, is already outdated, although it is much easier to use. However, I have personally used npcap and it provides full control over the package</p>\n'}]
['https://Stackoverflow.com/questions/78184', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']