Upload
iitmmk1981
View
230
Download
0
Embed Size (px)
Citation preview
7/27/2019 Fun With Sockets
1/25
03/01/13 Mandar Kulkarni 1
BackgroundBasics of Socket ProgrammingZMQ (Zero Message Queues)
Serialization with Google Protocol Buffers
Fun With Sockets
7/27/2019 Fun With Sockets
2/25
03/01/13 Mandar Kulkarni 2
Background
What is a computer network?
Ref: www.jimdevelopment.com
7/27/2019 Fun With Sockets
3/25
03/01/13 Mandar Kulkarni 3
Background
A collection of computers interconnected by asingle technology.
Two computers are said to be interconnected
if they are able to exchange information
C1 C2Flow of information
7/27/2019 Fun With Sockets
4/25
03/01/13 Mandar Kulkarni 4
Background
Ref: Lecture notes of EE 633 by Prof. Bhattacherjee, IIT Guwahati
7/27/2019 Fun With Sockets
5/25
03/01/13 Mandar Kulkarni 5
Background
Communication as transport of Data bitsFrames Packets
When we transmit a packet into and across the
network, we want to make sure that it istransmitted with no errors so we insert thepacket in a frame.
Ref:http://www.teracomtraining.com/tutorials/teracom-tutorial-packet-frame.htm
7/27/2019 Fun With Sockets
6/25
03/01/13 Mandar Kulkarni 6
Background
TCP/IP : A family of protocols TCP
Responsible for connection establishment andmanagement 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
7/27/2019 Fun With Sockets
7/25
03/01/13 Mandar Kulkarni 7
Sockets
A socket is the mechanism that most popular operatingsystems 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)
7/27/2019 Fun With Sockets
8/25
03/01/13 Mandar Kulkarni 8
Sockets
Client Server model
7/27/2019 Fun With Sockets
9/25
03/01/13 Mandar Kulkarni 9
Sockets
Applications
Web browsers
LAN gaming
Dota, AOE, CS...
Online file transfers
And many more...
7/27/2019 Fun With Sockets
10/25
03/01/13 Mandar Kulkarni 10
Creating Sockets
In this talk we will have a look at two ways tocreate sockets
Simple socket.h header in C/C++
ZMQ (multiple language support) Another popular implementation is with Boost
asio library in C++
7/27/2019 Fun With Sockets
11/25
03/01/13 Mandar Kulkarni 11
Creating Sockets With socket.h
Server Routines Telephone Analogy
socket() Installing a telephone line
bind() Assign a phone number to your handset
listen() Continuously monitor if there is anyincoming call
accept() Pick up the phone
read()/write() talk
close() hangup
7/27/2019 Fun With Sockets
12/25
03/01/13 Mandar Kulkarni 12
Creating Sockets With socket.h
Client Routines Telephony Analogy
socket() Installing a phone line
connect() dial
read()/write() talk
close() hangup
7/27/2019 Fun With Sockets
13/25
03/01/13 Mandar Kulkarni 13
TCP and UDP Sockets
Ref: learn-networking.com
7/27/2019 Fun With Sockets
14/25
03/01/13 Mandar Kulkarni 14
TCP sockets at work
Ref: http://www.tenouk.com/Module39a.html
7/27/2019 Fun With Sockets
15/25
03/01/13 Mandar Kulkarni 15
Intelligent transport layer with zmq socketshaving 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
ZMQ (Zero Message Queues)
7/27/2019 Fun With Sockets
16/25
03/01/13 Mandar Kulkarni 16
ZMQ (Zero Message Queues)
Request-Reply Pattern
Ref: www.zeromq.org
7/27/2019 Fun With Sockets
17/25
03/01/13 Mandar Kulkarni 17
ZMQ (Zero Message Queues)
Publish-Subscribe Pattern
Ref: www.zeromq.org
7/27/2019 Fun With Sockets
18/25
03/01/13 Mandar Kulkarni 18
ZMQ (Zero Message Queues)
Push-Pull Pattern
Ref: www.zeromq.org
7/27/2019 Fun With Sockets
19/25
03/01/13 Mandar Kulkarni 19
When you send data through sockets it mustbe serialized first
Serialization of objects! (An issue in C++)
Again boost libraries can help Problem: Language dependent for encoding and
decoding
All Google applications use Protocol Buffers
Advantage: No language barrier, supported by largenumber of languages including C++, java, python
7/27/2019 Fun With Sockets
20/25
03/01/13 Mandar Kulkarni 20
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++) foryou where the all functions required for you toserialize/de-serialize the message areautomatically declared and defined
The following example is taken fromhttps://developers.google.com/protocol-buffers/docs/overview
7/27/2019 Fun With Sockets
21/25
03/01/13 Mandar Kulkarni 21
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;
}
Example of a simple .proto file
7/27/2019 Fun With Sockets
22/25
03/01/13 Mandar Kulkarni 22
Example of a serialization
Person person;person.set_name("John Doe");person.set_id(1234);person.set_email("[email protected]");
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
7/27/2019 Fun With Sockets
23/25
03/01/13 Mandar Kulkarni 23
Simple TCP Socket Demo
7/27/2019 Fun With Sockets
24/25
03/01/13 Mandar Kulkarni 24
Hope you enjoyed!
7/27/2019 Fun With Sockets
25/25
03/01/13 Mandar Kulkarni 25
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
http://www.zeromq.org/https://developers.google.com/protocol-buffers/http://www.cs.rutgers.edu/~pxk/rutgers/notes/sockets/index.htmlhttp://www.stackoverflow.com/questions/5791860/beginners-socket-programming-in-chttp://www.stackoverflow.com/questions/5791860/beginners-socket-programming-in-chttp://www.cs.rutgers.edu/~pxk/rutgers/notes/sockets/index.htmlhttps://developers.google.com/protocol-buffers/http://www.zeromq.org/