开发者

C++ network programming [closed]

开发者 https://www.devze.com 2023-02-28 05:08 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 10 months ago.

Improve this question

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开发者_JAVA技巧 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


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,


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


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消