C++ network programming [closed]
Asked Answered
S

6

19

I would like to expand my knowledge in C++, so the first thing I'm taking on is network programming.

I want to make an IRC bot (which hopefully will teach me about socket programming and networking topics), but I have no idea where to start. If anyone could explain to me how IRC bots work and how to make them, and direct me to some learning resources, that would be really great. Simple snippets as well would be awesome...

edit:

forgot to mention that I use ubuntu, so the windows way is not an option

Sinnard answered 24/4, 2011 at 21:36 Comment(2)
Reading a book about sockets and TCP/IP would be favourite.Pointtopoint
After 6 years, what worked for you? Can you share it?Zenia
D
18

To understand sockets and use them right, you need The Sockets Bible:

W. Richard Stevens, Unix Network Programming, Volume 1: The Sockets Networking API (3rd Edition)

You absolutely must have this book before you sit down to write a line of sockets code. Don't leave home without it. Really. Starting around $35 used at Amazon.

EDIT: The OP asked about other volumes. Here are two others:

  W. Richard Stevens, UNIX Network Programming, Volume 2: Interprocess Communications (2nd Edition)
  W. Richard Stevens, TCP/IP Illustrated, Vol. 1: The Protocols

They are of Stevens's usual and expected superb quality. I don't know what his plans were for integrating all these books,

Disaccord answered 24/4, 2011 at 21:53 Comment(5)
I use linux.. That book says Unix. Will that make any difference?Sinnard
@MisterSir - The networking API is virtually identical. Stevens is an excellent book for you.Linger
What about the rest of the volumes? Are they relevant?Sinnard
@MisterSir -- I've added two other books. Take a look; they might be relevant.Disaccord
@MisterSir -- I think you should also consider the boost.asio library that @ildjarn recommends. I only just learned about it, but it looks very good to me. Much as I think that each of us needs to know sockets, it seems from the docs that boost.asio would save you at least a couple of weeks of coding and testing. But you've used it and you don't like it: you must know something that I don't.Disaccord
S
11

boost.asio is (in my opinion) the de facto standard for writing platform independant networking code in modern C++.

Silveira answered 24/4, 2011 at 22:1 Comment(4)
Do you think using boost.asio obviates the need to learn the socket API? Or should we recommend that a person write traditional socket programs first, and then switch to boost.asio?Linger
@Rob Adams : Much as I think one should learn std::vector<> and std::string before raw pointers/C-arrays and C-strings, I think that one should write boost.asio programs first then learn traditional sockets afterwards.Silveira
I use some of boost's features, but I really didn't like boost::asio. Or maybe I just didn't understand it well.Sinnard
+1 I didn't know anything about boost.asio before I saw your answer but after looking at it, I think it really is the best way to get something running quickly. Thanks for that very important, useful pointer.Disaccord
C
5

My recommendations:

  1. I'd first write the bot in fast-to-write, powerful high-level language, such as python. Get used to working with net tools, the IRC protocol and stuff.

  2. Learn about sockets and networking at low-level. For Unix, I'd say take a look at Unix Network Programming.

  3. Write your bot in C++! Make mistakes, fix them, and keep at it.

Cementation answered 24/4, 2011 at 21:41 Comment(2)
I really know just a bit of Python, so if you could just show me some examples, that could be helpful. thanksSinnard
I'm really short on time right now, but found you this: osix.net/modules/article/?id=780 hope it helpsCementation
F
5

The best guide to learn socket programming in C/C++ must be Beej's Guide to Network Programming by far. It goes through all of the steps you need to know, both with examples and detailed description. As far as I know, the only information this site lacks is of IPv6 Multicasting.

Finicking answered 24/4, 2011 at 21:57 Comment(0)
M
4

Start with a simple client-server example. It's very easy with Qt framework. For example:

server.cpp:

#include <QTcpSocket>
#include <QTcpServer>

int main()
{
    QTcpServer *tcpServer = new QTcpServer(); //creates TCP-based server
    tcpServer->listen(QHostAddress("172.16.254.1"),5300); //listen on your IP adress, port 5300
    while ( tcpServer->isListening() )  //while server is listening
    {   
        QTcpSocket* tcpSocket; //define TCP-based socket
        tcpServer->waitForNewConnection(); //server waits for connection
        if ( (tcpSocket = tcpServer->nextPendingConnection()) ) //if there are connections to be processsed 
        { 
                tcpSocket->write("hello",6); //write "hello" to the socket, client is connected to
                tcpSocket->flush();    
        }
    }
}

client.cpp:

#include <QDebug>
#include <QTcpSocket>

int main()
{
    QTcpSocket *tcpSocket = new QTcpSocket(); //create TCP-based socket
    tcpSocket->connectToHost("172.16.254.1",5300); //connect socket to server
    tcpSocket->waitForConnected(); //wait 
    tcpSocket->waitForReadyRead(); 
    qDebug() << tcpSocket->readAll();    
}

All you need to do is to run the first program in one terminal window, and the second in the other.

You will find more Qt network examples here

Margarettamargarette answered 18/9, 2013 at 10:44 Comment(1)
Your program seems to have a few problems one of which is memory leakage.Discourtesy
N
0

I know this is old there's book called

"Beej's Guide to Network Programming using Internet Socket"

Everything what Beej provides is 100% free to access here's the website to go and learn the basics of Network Programming.

https://beej.us/guide/bgnet/

The books provided on here I still recommend getting because they offer a pretty solid information on sockets and TCP/IP protocols.

  • Unix Network Programming: The Sockets Networking Api Unix Network Programming: The Sockets Networking Api - by W. Richard Stevens

  • TCP/IP Illustrated, Vol. 1: The Protocols (Addison-Wesley Professional Computing Series)TCP/IP Illustrated, Vol. 1: The Protocols (Addison-Wesley Professional Computing Series) by W. Richard Stevens

  • UNIX Network Programming, Volume 2: Interprocess Communications, Second EditionUNIX Network Programming, Volume 2: Interprocess Communications, Second Edition by W. Richard Stevens

  • TCP/IP Illustrated, Volume 1: The Protocols (Addison-Wesley Professional Computing Series) 2nd Edition by Kevin Fall (Author), W. Stevens (Author)

  • The TCP/IP Guide: A Comprehensive, Illustrated Internet Protocols Reference 1st Edition by Charles M. Kozierok


I am NOT a network programmer or software developer my only interest is Networking and Replication for Unreal Engine for game-development only. Please don't send me PMs or ask questions regarding about Networking.

Nettles answered 8/5, 2022 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.