Network Programming in Qt. TCP/IP Protocol Stack



Similar documents
Surface and Volumetric Data Rendering and Visualisation

New Qt APIs for Mobile Development. Mobility I Alex Blasche

Lecture 28: Internet Protocols

Qt in Education. The Qt object model and the signal slot concept

1 Data information is sent onto the network cable using which of the following? A Communication protocol B Data packet

First Workshop on Open Source and Internet Technology for Scientific Environment: with case studies from Environmental Monitoring

[PRAKTISCHE ASPEKTE DER INFORMATIK SS 2014]

LESSON Networking Fundamentals. Understand TCP/IP

Chapter 11. User Datagram Protocol (UDP)

Introdução aos Sistemas Distribuídos

Network-Oriented Software Development. Course: CSc4360/CSc6360 Instructor: Dr. Beyah Sessions: M-W, 3:00 4:40pm Lecture 2

Note! The problem set consists of two parts: Part I: The problem specifications pages Part II: The answer pages

Cape Girardeau Career Center CISCO Networking Academy Bill Link, Instructor. 2.,,,, and are key services that ISPs can provide to all customers.

Network Communication

Virtual Server and DDNS. Virtual Server and DDNS. For BIPAC 741/743GE

Network Configuration Settings

DNS: Domain Names. DNS: Domain Name System. DNS: Root name servers. DNS name servers

Introduction to Socket programming using C

Course Overview: Learn the essential skills needed to set up, configure, support, and troubleshoot your TCP/IP-based network.

EXPLORER. TFT Filter CONFIGURATION

Overview of Computer Networks

The TCP/IP Reference Model

A host-based firewall can be used in addition to a network-based firewall to provide multiple layers of protection.

Network Layers. CSC358 - Introduction to Computer Networks

Overview of TCP/IP. TCP/IP and Internet

Cisco Configuring Commonly Used IP ACLs

Introduction to Computer Networks

TCP/IP Fundamentals. OSI Seven Layer Model & Seminar Outline

Understanding TCP/IP. Introduction. What is an Architectural Model? APPENDIX

Basic Networking Concepts. 1. Introduction 2. Protocols 3. Protocol Layers 4. Network Interconnection/Internet

Transport and Network Layer

Andreas Burghart 6 October 2014 v1.0

How To Understand The Internet Of S (Netware)

Connecting to and Setting Up a Network

Generalised Socket Addresses for Unix Squeak

URI and UUID. Identifying things on the Web.

Network Programming TDC 561

The OSI and TCP/IP Models. Lesson 2

Guide to Network Defense and Countermeasures Third Edition. Chapter 2 TCP/IP

Internet Concepts. What is a Network?

Java Network. Slides prepared by : Farzana Rahman

Application. Transport. Network. Data Link. Physical. Network Layers. Goal

Basic Network Configuration

Unix System Administration

Note! The problem set consists of two parts: Part I: The problem specifications pages Part II: The answer pages

Computer Networks & Security 2014/2015

Network Programming. Writing network and internet applications.

Protocols. Packets. What's in an IP packet

How do I get to

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Connecting with Computer Science, 2e. Chapter 5 The Internet

CONNECTING WINDOWS XP PROFESSIONAL TO A NETWORK

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

IP address format: Dotted decimal notation:

IP Network Layer. Datagram ID FLAG Fragment Offset. IP Datagrams. IP Addresses. IP Addresses. CSCE 515: Computer Network Programming TCP/IP

Flow Analysis Versus Packet Analysis. What Should You Choose?

Overview. Securing TCP/IP. Introduction to TCP/IP (cont d) Introduction to TCP/IP

Algemene Theorie en Vaardigheden 2. April 2014 Theaterschool OTT-2 1

Lecture 2-ter. 2. A communication example Managing a HTTP v1.0 connection. G.Bianchi, G.Neglia, V.Mancuso

TCP/IP Network Essentials. Linux System Administration and IP Services

Firewall VPN Router. Quick Installation Guide M73-APO09-380

1 Introduction: Network Applications

Data Communication & Networks G

Introduction To Computer Networking

Technical Support Information Belkin internal use only

How To Design A Layered Network In A Computer Network

Internetworking Microsoft TCP/IP on Microsoft Windows NT 4.0

Data Communication Networks and Converged Networks

Computer Networks CS321

Computer Networks/DV2 Lab

Multi-Homing Dual WAN Firewall Router

Socket programming. Complement for the programming assignment INFO-0010

Network Technologies

Overview - Using ADAMS With a Firewall

Basic Vulnerability Issues for SIP Security

Introduction to TCP/IP

Firewall Firewall August, 2003

Overview - Using ADAMS With a Firewall

Using IPM to Measure Network Performance

User Datagram Protocol - Wikipedia, the free encyclopedia

E-Commerce Security. The Client-Side Vulnerabilities. Securing the Data Transaction LECTURE 7 (SECURITY)

Improved Digital Media Delivery with Telestream HyperLaunch

Chapter 6 Configuring the SSL VPN Tunnel Client and Port Forwarding

Raritan Valley Community College Academic Course Outline. CISY Advanced Computer Networking

The Internet. Chapter 10. Learning Objectives. Chapter Outline. After reading this chapter, you should be able to:

Domain Name System (DNS)

CSE331: Introduction to Networks and Security. Lecture 12 Fall 2006

Elementary Name and Address. Conversions

Web Browsing Examples. How Web Browsing and HTTP Works

EITF25 Internet Techniques and Applications L5: Wide Area Networks (WAN) Stefan Höst

Distributed Systems. 2. Application Layer

Indian Institute of Technology Kharagpur. TCP/IP Part I. Prof Indranil Sengupta Computer Science and Engineering Indian Institute of Technology

SSC - Communication and Networking Java Socket Programming (II)

TCP/IP Basis. OSI Model

How To Use A Network Over The Internet (Networking) With A Network (Netware) And A Network On A Computer (Network)

Firewalls. Chapter 3

Objectives of Lecture. Network Architecture. Protocols. Contents

The exam has 110 possible points, 10 of which are extra credit. There is a Word Bank on Page 8. Pages 7-8 can be removed from the exam.

Transcription:

Network Programming in Qt Prof. Tiago Garcia de Senna Carneiro DECOM UFOP 2007 TCP/IP Protocol Stack Roteador UFOP Roteador UFMG DECOM DEFIS DCC DEMAT Internet 1

Domain Name Server (DNS) A maioria dos seres humanos conseguem memorizar seqüências de até sete números DNS associa endereços IP a hostname s para simplificar a memorização do ser humano Note que um endereço é diferente de um nome de host As pessoas chamam hostname s de endereço... Nesse curso será feita uma clara (e necessária) distinção Domain Name Server (DNS) world Fapesp (*.br) other another dns dns uol aol bol sol mol rol 2

Network Classes QAbstractSocket QFtp QHostAddress QHostInfo QHttp QHttpHeader QHttpRequestHeader QHttpResponseHeader QNetworkAddressEntry QNetworkInterface QNetworkProxy QTcpServer QTcpSocket QUdpSocket QUrlInfo The base functionality common to all socket types Implementation of the client side of FTP protocol IP address Static functions for host name lookups Implementation of the HTTP protocol Header information for HTTP Request header information for HTTP Response header information for HTTP Stores one IP address supported by a network interface, along with its associated netmask and broadcast address Listing of the host's IP addresses and network interfaces Network layer proxy TCP-based server TCP socket UDP socket Stores information about URLs java.net.inetaddress Representa um endereço IP Pode ter 32 ou 128 bits Veja as subclasses: Inet4Address e Inet6Address Endereços da forma: ddd.ddd.ddd.ddd Usando o padrão atual: IPv4 Quadra pontuada (dotted quad) 3

Soquetes Abstração que permite ao programador tratar uma conexão de rede como um stream de dados. Libera do programador dos detalhes de baixo nível da rede Um socket conecta dois hosts Stream IN Stream OUT Portas notáveis De 1 a 1023 Breve lista 07 echo 13 daytime 21 FTP 23 Telnet 25 SMTP 80 HTTP 110 POP3 1099 RMI Registry Lista detalhada em: http://pt.wikipedia.org/wiki/lista_de_portas_de_protocolos 4

Operações Básicas dos Sockets Conectar a máquinas remotas Enviar dados Receber dados Encerrar conexão Cliente Conectar-se a portas lógicas Aguardar chegada de dados Aceitar conexão de máquinas remotas Servidor Transmission Control Protocol TCP Protocol: Reliable Stream-oriented Connection-oriented 5

User Datagram Protocol UDP Protocol: Lightweight Unreliable Datagram-oriented Connectionless TCP Single Server Application #include <QDialog> class QLabel; class QPushButton; class QTcpServer; class Server : public QDialog { Q_OBJECT public: Server(QWidget *parent = 0); private slots: void sendfortune(); private: QLabel *statuslabel; QPushButton *quitbutton; QTcpServer *tcpserver; QStringList fortunes; ; Server.h 6

TCP Single Server Application Server::Server(QWidget *parent) : QDialog(parent) { statuslabel = new QLabel; quitbutton = new QPushButton(tr("Quit")); quitbutton->setautodefault(false); tcpserver = new QTcpServer(this); if (!tcpserver->listen()) { QMessageBox::critical(this, tr("fortune Server"), tr("unable to start the server: %1.").arg(tcpServer->errorString())); close(); return;... Server.cpp TCP Single Server Application... statuslabel->settext(tr("the server is running on port %1.\n" "Run the Fortune Client example now.").arg(tcpserver->serverport())); fortunes << tr("you've been leading a dog's life. Stay off the furniture.") << tr("you've got to think about tomorrow.") << tr("you will be surprised by a loud noise.") << tr("you will feel hungry again in another hour.") << tr("you might have mail.") << tr("you cannot kill time without injuring eternity.") << tr("computers are not intelligent. They only think they are."); connect(quitbutton, SIGNAL(clicked()), this, SLOT(close())); connect(tcpserver, SIGNAL(newConnection()), this, SLOT(sendFortune()));... Server.cpp 7

TCP Single Server Application... QHBoxLayout *buttonlayout = new QHBoxLayout; buttonlayout->addstretch(1); buttonlayout->addwidget(quitbutton); buttonlayout->addstretch(1); QVBoxLayout *mainlayout = new QVBoxLayout; mainlayout->addwidget(statuslabel); mainlayout->addlayout(buttonlayout); setlayout(mainlayout); setwindowtitle(tr("fortune Server")); Server.cpp TCP Single Server Application void Server::sendFortune() { QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setversion(qdatastream::qt_4_0); out << (quint16)0; out << fortunes.at(qrand() % fortunes.size()); out.device()->seek(0); out << (quint16)(block.size() - sizeof(quint16)); QTcpSocket *clientconnection = tcpserver->nextpendingconnection(); connect(clientconnection, SIGNAL(disconnected()), clientconnection, SLOT(deleteLater())); clientconnection->write(block); clientconnection->disconnectfromhost(); Server.cpp 8

TCP Single Server Application #include <QApplication> #include <QtCore> #include <stdlib.h> #include "server.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Server server; server.show(); qsrand(qtime(0,0,0).secsto(qtime::currenttime())); return server.exec(); main.cpp TCP Single Client Application class Client : public QDialog { Q_OBJECT public: Client(QWidget *parent = 0); private slots: void requestnewfortune(); void readfortune(); void displayerror(qabstractsocket::socketerror socketerror); void enablegetfortunebutton(); private: QLabel *hostlabel; QLabel *portlabel; QLineEdit *hostlineedit; QLineEdit *portlineedit; QLabel *statuslabel; QPushButton *getfortunebutton; QPushButton *quitbutton; QDialogButtonBox *buttonbox; QTcpSocket *tcpsocket; QString currentfortune; quint16 blocksize; ; DECOM -UFOP Client.h 9

TCP Single Client Application Client::Client(QWidget *parent) : QDialog(parent) {... tcpsocket = new QTcpSocket(this); connect(hostlineedit, SIGNAL(textChanged(const QString &)), this, SLOT(enableGetFortuneButton())); connect(portlineedit, SIGNAL(textChanged(const QString &)), this, SLOT(enableGetFortuneButton())); connect(getfortunebutton, SIGNAL(clicked()), this, SLOT(requestNewFortune())); connect(quitbutton, SIGNAL(clicked()), this, SLOT(close())); connect(tcpsocket, SIGNAL(readyRead()), this, SLOT(readFortune())); connect(tcpsocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));... Client.cpp TCP Single Client Application void Client::requestNewFortune() { getfortunebutton->setenabled(false); blocksize = 0; tcpsocket->abort(); tcpsocket->connecttohost(hostlineedit->text(), portlineedit->text().toint()); void Client::enableGetFortuneButton() { getfortunebutton->setenabled(!hostlineedit->text().isempty() &&!portlineedit->text().isempty()); Client.cpp 10

TCP Single Client Application void Client::readFortune() { QDataStream in(tcpsocket); in.setversion(qdatastream::qt_4_0); if (blocksize == 0) { if (tcpsocket->bytesavailable() < (int)sizeof(quint16)) return; in >> blocksize; if (tcpsocket->bytesavailable() < blocksize) return; QString nextfortune; in >> nextfortune; if (nextfortune == currentfortune) { QTimer::singleShot(0, this, SLOT(requestNewFortune())); return; currentfortune = nextfortune; statuslabel->settext(currentfortune); getfortunebutton->setenabled(true); Client.cpp void Client::displayError(QAbstractSocket::SocketError socketerror) { switch (socketerror) { case QAbstractSocket::RemoteHostClosedError: break; case QAbstractSocket::HostNotFoundError: QMessageBox::information(this, tr("fortune Client"), tr("the host was not found. Please check the " "host name and port settings.")); break; case QAbstractSocket::ConnectionRefusedError: QMessageBox::information(this, tr("fortune Client"), tr("the connection was refused by the peer. "Make sure the fortune server is running, "and check that the host name and port " "settings are correct.")); break; default: QMessageBox::information(this, tr("fortune Client"), tr("the following error occurred: %1."). arg(tcpsocket->errorstring())); getfortunebutton->setenabled(true); TCP Single Client Application DECOM -UFOP main.cpp 11

TCP Threaded Server Application #include <QStringList> #include <QTcpServer> class FortuneServer : public QTcpServer { Q_OBJECT public: FortuneServer(QObject *parent = 0); protected: void incomingconnection(int socketdescriptor); private: QStringList fortunes; ; FortuneServer.h TCP Threaded Server Application FortuneServer::FortuneServer(QObject *parent) : QTcpServer(parent) { fortunes << tr("you've been leading a dog's life. Stay off the furniture.") << tr("you've got to think about tomorrow.") << tr("you will be surprised by a loud noise.") << tr("you will feel hungry again in another hour.") << tr("you might have mail.") << tr("you cannot kill time without injuring eternity.") << tr("computers are not intelligent. They only think they are."); void FortuneServer::incomingConnection(int socketdescriptor) { QString fortune = fortunes.at(qrand() % fortunes.size()); FortuneThread *thread = new FortuneThread(socketDescriptor, fortune, this); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); FortuneServer.cpp 12

TCP Threaded Server Application #include <QThread> #include <QTcpSocket> class FortuneThread : public QThread { Q_OBJECT public: FortuneThread(int socketdescriptor, const QString &fortune, QObject *parent); void run(); signals: void error(qtcpsocket::socketerror socketerror); private: int socketdescriptor; QString text; ; FortuneThread.h TCP Threaded Server Application #include "fortunethread.h #include <QtNetwork> FortuneThread::FortuneThread(int socketdescriptor, const QString &fortune, QObject *parent) : QThread(parent), socketdescriptor(socketdescriptor), text(fortune) { void FortuneThread::run() { QTcpSocket tcpsocket; if (!tcpsocket.setsocketdescriptor(socketdescriptor)) { emit error(tcpsocket.error()); return; QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setversion(qdatastream::qt_4_0); out << (quint16)0; out << text; out.device()->seek(0); out << (quint16)(block.size() -sizeof(quint16)); tcpsocket.write(block); tcpsocket.disconnectfromhost(); tcpsocket.waitfordisconnected(); DECOM -UFOP FortuneThread.cpp 13

TCP Threaded Server Application #include <QApplication> #include <QtCore> #include <stdlib.h> #include "dialog.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Dialog dialog; dialog.show(); qsrand(qtime(0,0,0).secsto(qtime::currenttime())); return dialog.exec(); Main.cpp Building Network Applications The following declaration in a qmake project file ensures that an application is compiled and linked appropriately: QT += network To include the definitions of the module's classes, use the following directive: #include <QtNetwork> 14