Sunteți pe pagina 1din 25

Fun With Sockets

Background Basics of Socket Programming ZMQ (Zero Message Queues) Serialization with Google Protocol Buffers

03/01/13

Mandar Kulkarni

Background

Ref: www.jimdevelopment.com

What is a computer network?


Mandar Kulkarni 2

03/01/13

Background

A collection of computers interconnected by a single technology. Two computers are said to be interconnected if they are able to exchange information

C1

Flow of information

C2

03/01/13

Mandar Kulkarni

Background

Ref: Lecture notes of EE 633 by Prof. Bhattacherjee, IIT Guwahati

03/01/13

Mandar Kulkarni

Background

Communication as transport of Data bits Frames Packets

When we transmit a packet into and across the network, we want to make sure that it is transmitted with no errors so we insert the packet in a frame.

Ref: http://www.teracomtraining. com/tutorials/teracomtutorial-packet-frame.htm

03/01/13

Mandar Kulkarni

Background

TCP/IP : A family of protocols TCP Responsible for connection establishment and management and reliable data transport between software processes on devices IP Envelopes and addresses the data Enables the network to read the envelope and forward the data to its destination Defines how much data can fit in a single packet. Other protocols added to TCP/IP suite UDP, SMTP, FTP, HTTP
Mandar Kulkarni 6

03/01/13

Sockets

A socket is the mechanism that most popular operating systems provide to give programs access to the network Endpoint of a communication system

Maybe inter process or over the Internet

Analogy: Electrical socket Network socket Wire Communication channel Device Network application (say your web browser)

03/01/13

Mandar Kulkarni

Sockets

Client Server model

03/01/13

Mandar Kulkarni

Sockets

Applications

Web browsers LAN gaming

Dota, AOE, CS...

Online file transfers And many more...

03/01/13

Mandar Kulkarni

Creating Sockets

In this talk we will have a look at two ways to create sockets


Simple socket.h header in C/C++ ZMQ (multiple language support)

Another popular implementation is with Boost asio library in C++

03/01/13

Mandar Kulkarni

10

Creating Sockets With socket.h


Server Routines socket() bind() listen() accept() read()/write() close() Telephone Analogy Installing a telephone line Assign a phone number to your handset Continuously monitor if there is any incoming call Pick up the phone talk hangup

03/01/13

Mandar Kulkarni

11

Creating Sockets With socket.h


Client Routines Telephony Analogy

socket()

Installing a phone line

connect()

dial

read()/write()

talk

close()

hangup

03/01/13

Mandar Kulkarni

12

TCP and UDP Sockets

Ref: learn-networking.com

03/01/13

Mandar Kulkarni

13

TCP sockets at work

Ref: http://www.tenouk.com/Module39a.html 03/01/13 Mandar Kulkarni 14

ZMQ (Zero Message Queues)

Intelligent transport layer with zmq sockets having internal message queuing capability Supports inter process communication as well Supports N to N connection via PUB-SUB, REQ-REP, PUSH-PULL Supported by 30+ languages including C, C+ +, Java, .NET, Python

03/01/13

Mandar Kulkarni

15

ZMQ (Zero Message Queues)


Request-Reply Pattern

Ref: www.zeromq.org

03/01/13

Mandar Kulkarni

16

ZMQ (Zero Message Queues)


Publish-Subscribe Pattern

Ref: www.zeromq.org

03/01/13

Mandar Kulkarni

17

ZMQ (Zero Message Queues)


Push-Pull Pattern

Ref: www.zeromq.org

03/01/13

Mandar Kulkarni

18

When you send data through sockets it must be serialized first Serialization of objects! (An issue in C++)

Again boost libraries can help

Problem: Language dependent for encoding and decoding Advantage: No language barrier, supported by large number of languages including C++, java, python

All Google applications use Protocol Buffers

03/01/13

Mandar Kulkarni

19

So what's the procedure to serialize


Create a .proto file Compile using protocol buffer compiler It will generate .pb.cc and .pb.h files (for C++) for you where the all functions required for you to serialize/de-serialize the message are automatically declared and defined The following example is taken from https://developers.google.com/protocolbuffers/docs/overview
Mandar Kulkarni 20

03/01/13

Example of a simple .proto file


message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4; }
03/01/13 Mandar Kulkarni 21

Example of a serialization
Person person; person.set_name("John Doe"); person.set_id(1234); person.set_email("jdoe@example.com"); fstream output("myfile", ios::out | ios::binary); person.SerializeToOstream(&output);

Example of a deserialization
fstream input("myfile", ios::in | ios::binary); Person person; person.ParseFromIstream(&input); cout << "Name: " << person.name() << endl; cout << "E-mail: " << person.email() << endl;

03/01/13

Mandar Kulkarni

22

Simple TCP Socket Demo

03/01/13

Mandar Kulkarni

23

Hope you enjoyed!

03/01/13

Mandar Kulkarni

24

Further Reading

http://www.zeromq.org/ https://developers.google.com/protocol-buffers/ http://www.cs.rutgers.edu/~pxk/rutgers/notes/sockets/index.html www.stackoverflow.com/questions/5791860/beginners-socket-programming-in-c

03/01/13

Mandar Kulkarni

25

S-ar putea să vă placă și