ser321 principles of distributed software systems 3...
TRANSCRIPT
![Page 1: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/1.jpg)
Page 1
cnSockets.fm Ser321 Class Notes
e Systems
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
Ser321 Principles of Distributed Softwar
3. Programming with Sockets
![Page 2: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/2.jpg)
Page 2
cnSockets.fm Ser321 Class Notes
ple Index
l servers/clients and ocols and how are ns?
he internet, naming,
olved with sockets.TCP/IP sockets to
mmunicate and the sed TCP/IP sockets.
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.a Motivation, References and Exam3.a.1 Motivation
• How do client-server applications such as ftp, telnet, maiWeb browsers and servers communicate? What are protthey used to govern communication for these applicatio
• Outcomes- To understand the basics of client-server computing; t
internet resources and socket connections.- To be able to use the Java API classes and methods inv- To be able to design and implement systems that use
realize distributed processing.- To understand how socket-based clients and servers co
importance of a communication protocol to stream-ba
![Page 3: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/3.jpg)
Page 3
cnSockets.fm Ser321 Class Notes
ormation
ing
putingText:ssing, User ol
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.a.2 References, Readings and Sources of Inf• References
- The Java Tutorial, linked below, see Custom Network- http://java.sun.com/docs/books/tutorial/
• In Java Network Programming and Distributed Com- Chapters 1,3,5,6: Networking Theory, Internet Addre
Datagram Protocol, and Transmission Control Protoc• In Java Network Programming 4th Edition Text:
- Chapter 1: Basic Network Concepts
![Page 4: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/4.jpg)
Page 4
cnSockets.fm Ser321 Class Notes
kets (in java.net)ckets in Java
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.a.3 Index of Examples in this Section• Java simple: Section 3.c.3 Java Classes Related to Soc• Java threaded: Section 3.c.4 Example of Threaded So
![Page 5: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/5.jpg)
Page 5
cnSockets.fm Ser321 Class Notes
f Sockets
onents operating on
er)
a communications rranged protocol.t) via a browser,
r. These objects can server.ed objects, objects RMI and EJB.
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.b Network Addressing and Types o3.b.1 Basics of Distributed Programming
• The task of developing a software system that has compmore than one computer.
• Multiple users (clients) sharing the same resource (serv• Many Paradigms for distributed systems
- Application software resides on client and server, datover the internet through streams according to a pre-a
- Client may download application specific code (applewhich embodies a majority of the client’s logic.
- Client may download application objects from a servereceive messages and have state as established on the
- Client may send messages to server objects. Distributcreated in frameworks such as DCOM, CORBA, Java
![Page 6: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/6.jpg)
Page 6
cnSockets.fm Ser321 Class Notes
)*
some differentiate).ee:
chapter1
h?query#fragment
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.b.2 Network Addressing• Internet (Network) Address
- IP = 4 byte numeric address (example, 129.219.38.79- Domain Address = machineName { . domainName }- Example domain address: roo.eas.asu.edu
• Universal Resource Locator (URL) as opposed to URI (- URL specifies what and where: for a full definition s
http://www.w3.org/Addressing/- example,
http://quay.poly.asu.edu:8072/Ser321/index.html#- protocol, as http:// (http, ftp, mailto, file, orb)- internet address, as quay.poly.asu.edu- port number, as 8072- file, /Ser321/index.html- anchor #chapter1- The general form is: protocol://user@host:port/pat
![Page 7: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/7.jpg)
Page 7
cnSockets.fm Ser321 Class Notes
ide or sent to the
r the system
a client and server. th directions.municate between
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.b.3 Network Addressing• Port - a place where things are brought in from the outs
outside- 16 bit integer- port numbers less than 1024 are typically reserved fo
• Socket - A plug. Sockets have two components- Internet address- port (roo.east.asu.edu:8080)
• Connection - establishing a linkage via a socket betweenConnections may last for several communications in bo
• Protocol - an agreement of what, when and how to comthe client and server, examples include:- http for web connections- smtp for transmitting mail messages- ftp file transfer protocol- telnet establishing remote logins to a unix system
![Page 8: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/8.jpg)
Page 8
cnSockets.fm Ser321 Class Notes
process
m:
libraries java.net)
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.b.4 Sockets Context and History• First appeared in Berkeley Unix in 1981 (BSD 4.2 inter
communications - IPC)• Sockets are supported on virtually every operating syste
- Windows socket API is WinSock- BSD Unix systems have sockets as part of the kernel- MS-DOS, Windows, Mac-OS and OS/2 have socket - Java provides sockets as part of core classes (package
![Page 9: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/9.jpg)
Page 9
cnSockets.fm Ser321 Class Notes
and received) in
rotocol
yer)ls
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.b.5 Three flavors of Sockets• Datagram sockets
- not session oriented - independent transmissions, - no sequencing assured or checksums provided,- no acknowledgement provided,- limit data transfer to 32K Bytes.- fastest form of communication
• Stream Sockets (also called TCP/IP)- Session (or connection) based service- guarantees that packets are sent without errors, sent (
sequence and without duplication,- unlimited data size in packets- TCP/IP = Transmission Control Protocol / Internet P
• Raw Sockets- interface to low level protocols (such as IP network la- no peer-to-peer services, but used to develop protoco
![Page 10: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/10.jpg)
Page 10
cnSockets.fm Ser321 Class Notes
berct queueg its socket. In Java, on is established
establish a session
et object.nts are waiting, the ts. The main thread e socket.s for passing ut streams perform
allow resources to
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.b.6 Scenario for Stream Sockets• Sockets get created by both client and server,
- Server creates a ServerSocket specifying a port num- Server may also specify the length of the client conne- Client specifies the internet address and port in creatin
when the client creates its Socket instance, a connectiwith the SocketServer
• Server listens for arriving requests (accept method) to (connection) with a client.- Client requests a connection by creating a new Sock- If no connections are pending, the server blocks. If clie
server creates new threads (Stay tuned) to service clienre-issues the accept to service other clients on the sam
• The client and server identify input and output streaminformation according to a protocol. The input and outpthe work of the application.
• The client and server must both close the connection tobe used in another connection.
![Page 11: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/11.jpg)
Page 11
cnSockets.fm Ser321 Class Notes
a
InputStream bytes) to the output ernal buffer, which stream buffering.
treamta types (int, ble (machine-
tStreame types to/from a le objects) are
that support the t streams.
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.c Socket Connections in Jav3.c.1 Sockets have Input and Output Streams
• Buffered Streamjava.io.BufferedOutputStream, and java.io.Buffered- Buffered writing and reading single bytes (or arrays of
stream or from the input stream. Data goes into an intcan be marked or reset. The implementation handles
• Data Streamjava.io.DataOutputStream, and java.io.DataOutputS- Lets an application write and read primitive Java da
boolean, double, char, ...) to/from a stream in a portaindependent) way.
• Object Streamjava.io.ObjectOutputStream, and java.io.ObjectInpu- Serializes and deserializes Java Objects and primitiv
stream. Object graphs (supports nesting of serializabplaced onto or obtained from a Stream. Only objects java.io.Serializable interface can be written to objec
![Page 12: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/12.jpg)
Page 12
cnSockets.fm Ser321 Class Notes
ationd reading to/from
lls its accept ket object.e host and port in on, the constructor
eams from the
eam, and the client
n read the data from ta streams, or object match the other
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.c.2 Java Sockets Use Steams for Communic• TCP/IP Clients and servers communicate by writing an
streams.
• Java TCP/IP (stream-based) socketsjava.net.ServerSocket, and java.net.Socket- Server creates a java.net.ServerSocket object and ca
method. When a client connects, accept returns a Soc- Client creates a java.net.Socket object, specifying th
the constructor. When the server accepts the connecticompletes.
- Server and client can obtain both input and output strconnected Socket objects.
- The server has both an input stream and an output stralso has both an input stream and an output stream.
• When the server writes to its output stream, the client caits input stream, and vice versa. So, buffered streams, dastreams can be used in communication. But, they shouldside.
![Page 13: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/13.jpg)
Page 13
cnSockets.fm Ser321 Class Notes
.net)leSock.jar domain addresses.
.poly.asu.edu.219.40.47
provides the ability
p://www.google.com
a connectionless ramSend.java
. Execute below rver.java
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.c.3 Java Classes Related to Sockets (in java• For examples corresponding to this slide see: javaSimp• InetAddress - class encapsulates relation between IP and
See: GetIP.java and GetNameForIP.java- Run as: java -cp classes ser321.sockets.GetIP quay- java -cp classes ser321.sockets.GetNameForIP 129
• URL - the URL class encapsulates web resources. URLto get the content of a URL. See: SimpleBrowser.java- Run as: java -cp classes ser321.sockets.SimpleBrowser htt
• Datagrams - Datagram packets are used to implement packet delivery service: DatagramReceive.java, Datag- java -cp classes ser321.sockets.DatagramReceive- java -cp classes ser321.sockets.DatagramSend
• Socket and ServerSocket - Support for Stream Socketsfrom different shells. See: SockClient.java and SockSe- java -cp classes ser321.sockets.SockServer- java -cp classes ser321.sockets.SockClient
![Page 14: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/14.jpg)
Page 14
cnSockets.fm Ser321 Class Notes
ad the server. Here a client where bi-t Streams.
anguage ject) streams can be uage environments, to read from a Java . But, at the level of ty between
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.c.4 Example of Threaded Sockets in Java• The first step toward handling multiple clients is to thre
is an example of a threaded socket server together with directional communication is accomplished using Objec- See: javaThreadSock.jar
• Sockets are platform independent and to a large extent lindependent. We can’t expect that language-specific (Obused to communicate over sockets to/from different langsuch as Java to/from C++. For example, we can’t expectObjectInputStream, something that was written in C++communicating with Byte arrays, there is interoperabililanguages.- See: simpleInterop.jar
![Page 15: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/15.jpg)
Page 15
cnSockets.fm Ser321 Class Notes
ET
and received) in
gh streamseam.r serialized objects.ockets.UDPClient
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.d Socket Connections in C# .N3.d.1 Stream Sockets in C# .NET
• Stream Sockets (TCP)- Session (or connection) based service- guarantees that packets are sent without errors, sent (
sequence and without duplication,- unlimited data size in packets
• Communication between server and client occurs throu- The base class for this communication is NetworkStr- Streams can be used for binary, text, and string data o
• Datagram sockets are supported by class System.Net.S
![Page 16: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/16.jpg)
Page 16
cnSockets.fm Ser321 Class Notes
ueue, etc)g its socket. In C#,
nection is
cceptTcpClient t. or more clients are
s a new thread (stay ues the accept to
s for passing he input and output
allow resources to
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.d.2 Scenario for Stream Sockets• Sockets are created by both client and server,
- Server specifies a port number- Server may customize aspects of connections (wait q- Client specifies the internet address and port in creatin
when the client creates its TCPClient instance, a conestablished with the TCPListener
• Server listens for arriving requests (AcceptSocket or Amethod) to establish a session (connection) with a clien- If no connections are pending, the server blocks. If one
waiting, they are queued and in turn, the server createtuned) to service each client. The parent thread re-issservice another client on the same socket.
• The client and server identify input and output streaminformation according to a protocol they both agree to. Tstreams perform the work of the application.
• The client and server must both close the connection tobe used in another connection.
![Page 17: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/17.jpg)
Page 17
cnSockets.fm Ser321 Class Notes
er
");
ver.exe portNum
ing.ASCII);
ocketClient.exeebRequest and
a web page.hen prompted.
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.d.3 C# .NET Sample Socket Client and Serv• See the example: csSockets.jar In the server:
TcpListener tcpl = new TcpListener(9090);tcpl.Start();Console.Write("TCPSocketServer up and waiting for connections on 9090TcpClient sock = tcpl.AcceptTcpClient();...tcpl.Stop();sock.Close();
- Server (TCPSocketServer.cs): ./bin/TCPSocketSer
• In the client:TcpClient tcpc = new TcpClient("localhost", 9090);Stream tcpStream = tcpc.GetStream();StreamReader reader = new StreamReader(tcpStream, System.Text.Encod...reader.Close();tcpc.Close();
- Run the client (TCPSocketClient.cs) as: ./bin/TCPS• Among other supporting classes, .NET includes HttpW
WebResponse classes to support http requests.- The example: WebRequest.cs downloads and prints - Run as: ./bin/WebRequest.exe and provide a URL w
![Page 18: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/18.jpg)
Page 18
cnSockets.fm Ser321 Class Notes
d Servern C#.
d build the project
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.d.4 .NET Sample Threaded Socket Client an• Here is an example of a threaded socket server written i
- See: csThreadSock.jar- Extract the jar file, change to the project directory an
with: ant build
![Page 19: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/19.jpg)
Page 19
cnSockets.fm Ser321 Class Notes
nge the same object.
hronization.is necessary.nges the object?
state, for example:
ack pointer.tep 2, then another = stack.top()).e
(item)(item) completes
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.d.5 Sharing Objects: Synchronization• What is a Shared Object?
- Multiple threads of single program may access or cha• When should access to shared objects be controlled?
- When multiple threads only read the object - no sync- If any thread modifies the object - synchronization
• Why must access be controlled when some thread cha- consider: stack.push(item), for example- multiple actions are necessary to achieve consistent- (1) increment stack pointer;- (2) place the new item into the collection using the st- If one thread is interrupted after step 1, but before s
thread may see an inconsistent stack state (visualize a• Monitor - a lock used to protect a critical section of cod
- Each thread calls Enter() to get the lock before push- Each thread calls Exit() to release the lock after push
![Page 20: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/20.jpg)
Page 20
cnSockets.fm Ser321 Class Notes
Span) lock the r’s before first Exit)
to unlock the object
struct:
d Exit methods
ush or pop
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.d.6 C# .NET Monitors• A Monitor protects access to the Object
- See System.Threading.Monitor class
- All Monitor methods are static
• Basic Monitor methods to lock and unlock an object
- void Enter(object) and bool TryEnter(object, Timeobject. A thread may be recursive (call multiple Ente
- void Exit(object) indicate the calling thread is ready
- Alternatively, a Thread may use the C# language conlock (object){ ... }
• Recall that Java uses monitors without explicit Enter an
- public synchronized void push(String item)
- The stack object’s monitor (lock) is acquired before p
- See example: csmonitor.jar
![Page 21: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/21.jpg)
Page 21
cnSockets.fm Ser321 Class Notes
ck an object based
tack isn’t full.ck has storage.
ass
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.d.7 Conditional locking• Some shared objects need the ability to conditionally lo
on the state of the shared structure.• Consider a Stack that is implemented with finite storage
- Want to accept (and lock) for a push only when the s- Want to accept (and lock) for a pop only when the sta
• C# handles this with Wait(), Pulse() and PulseAll()- Static methods in the System.Threading.Monitor cl
![Page 22: Ser321 Principles of Distributed Software Systems 3 ...pooh.poly.asu.edu/Ser321/ClassNotes/cnSockets.pdfSer321 Principles of Distributed Software Systems 3. Programming with Sockets](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9f2a727f8b9a0d158c7bb3/html5/thumbnails/22.jpg)
Page 22
cnSockets.fm Ser321 Class Notes
after an Enter)
d the thread blocks a Pulse (or time es.
Pulse(this); }
Item; Pulse(this); }
Principles of Distributed Software Systems © T. Lindquist 2015 August 2015
3.e These are only used only within a lock (or• Wait, Pulse and PulseAll
- When a thread executes a Wait, the lock is released an(optionally, for up to a timespan). When awakened byexpiration), the thread reacquires the lock and continu
• In Push:- lock { if (stack_is_full) {Wait(this);} addTheItem;
• Correspondingly in Pop- lock { if (stack_is_empty) {Wait(this);} removeThe