Presentation is loading. Please wait.

Presentation is loading. Please wait.

University of Pittsburgh Computer Science 1 Week 5: Introduction Last week we discussedLast week we discussed èDifference between executing sequentially.

Similar presentations


Presentation on theme: "University of Pittsburgh Computer Science 1 Week 5: Introduction Last week we discussedLast week we discussed èDifference between executing sequentially."— Presentation transcript:

1

2 University of Pittsburgh Computer Science 1 Week 5: Introduction Last week we discussedLast week we discussed èDifference between executing sequentially and in parallel èHow multi-tasking allows computers to appear to do things in parallel, even with only one CPU èHow multi-tasking is done with Threads in Java  Thread objects execute run() subprogram  When run() finishes, thread is dead  Moving.java and Moving1.java with one thread èUsing more than one thread  Independent threads – no data sharing

3 University of Pittsburgh Computer Science 2 Week 5: Introduction –Bounce.java demonstration  Threads share data –Data consistency issues –Using monitors to restrict access to critical data  Other multi-thread issues –Deadlock – no thread can execute due to circular dependency of resources –Starvation – some thread never gets to execute

4 University of Pittsburgh Computer Science 3 Week 5: Communicating with Other Computers èSend and receive email Computers need to “talk” to each other for various reasonsComputers need to “talk” to each other for various reasons èUpload and download files èAllow multiple users to access information at the same time  Ex. Games How can we do this?How can we do this?

5 University of Pittsburgh Computer Science 4 Week 5: Servers and Clients Typically, communicating computers take on one of two roles:Typically, communicating computers take on one of two roles: èClient: computer makes requests to server and utilizes the results  Usually has an interface with user èServer: computer takes requests from other computers and supplies them with files, data, etc  Often does not deal directly with end users

6 University of Pittsburgh Computer Science 5 Week 5: Types of Communication Computers can communicate in different waysComputers can communicate in different ways èConnection-oriented communication  Computers connect to each other and transmit data back and forth  Computers remain connected even if no data is sent  Similar in idea to a phone line èConnectionless communication  Computers send data packets to each other  No continuous connection between them  Similar in idea to U.S. Mail

7 University of Pittsburgh Computer Science 6 Week 5: Types of Communication Connection-oriented: all data follows the same path between the client and the server Connectionless: each data packet may follow a different route between client and server – no hard “connection” exists CLIENT SERVER NETWORK

8 University of Pittsburgh Computer Science 7 Week 5: Types of Communication Advantages of connection-oriented:Advantages of connection-oriented: èData flow is consistent and reliable  Order sent is always order received èAfter set-up is complete, can be very fast Disadvantages of connection-oriented:Disadvantages of connection-oriented: èEach link in the connection is a vulnerable point  If a link goes down, connection must be reestablished èInitial set-up can be time-consuming  For small amounts of data, overhead incurred is too high

9 University of Pittsburgh Computer Science 8 Week 5: Types of Communication Advantages of connectionless:Advantages of connectionless: èNo initial setup time  Short messages can be sent very quickly èIf a network link goes down, messages can easily be rerouted Disadvantages of connectionless:Disadvantages of connectionless: èEach packet must be routed individually  For long messages, this is time-consuming èNo guarantee that messages will arrive in order, or even at all  Up to server-client to handle this

10 University of Pittsburgh Computer Science 9 Week 5: Java Sockets We’ll look at connection-oriented communication in JavaWe’ll look at connection-oriented communication in Java èUtilizes SOCKETS  Think of these as a two-way connection between the client and the server èServer starts up a èServer starts up a ServerSocket  This waits to accept a connection from a client èClient creates a new by connecting to Server èClient creates a new Socket by connecting to Server  Server creates a Socket on its end simultaneously

11 University of Pittsburgh Computer Science 10 Week 5: Java Sockets Socket CLIENT SERVER Client Output Server Input Server Output Client Input

12 University of Pittsburgh Computer Science 11 Week 5: Java Sockets Let’s look at a simple exampleLet’s look at a simple example èServer will wait for connection from clients èAt each connection, client will send a single message to the server, which the server will print èServer sends no data back to the client  Download SendToMe.java and ISend.java from the Web site  Start up two command prompts  Compile and run SendToMe.java in the first  Wait for instructions about ISend.java

13 University of Pittsburgh Computer Science 12 Week 5: Java Sockets Computer addressesComputer addresses èComputers connected directly to the internet have IP (Internet Protocol) Addresses  Technically a sequence of numbers –Ex: 123.45.67  Usually accessed by name –Ex: unixs1.cis.pitt.edu, nomad.cs.pitt.edu èWhen connecting to a server, you must supply the IP Address and the port number  A single machine may be running many server programs, each on a different port

14 University of Pittsburgh Computer Science 13 Week 5: Java Sockets Each computer in this lab has an IP addressEach computer in this lab has an IP address  Names are written on the computer box and in the CS department are hostname.cs.pitt.edu èLook around and find out the addresses of some computers in the lab – just the first part up to the period èTo send a message to someone else in the lab, type the following at the command prompt: java ISend hostname yourname “your message” èTry it a few times with various computers  Try to guess who is sending you a message  But be nice!

15 University of Pittsburgh Computer Science 14 Week 5: Java Sockets Let’s look at the codeLet’s look at the code èSendToMe.java  ServerSocket created, then goes into a loop  Socket connection accepted  Reads a single line from client and prints it to console èISend.java  Gets host name, user name and message from command line  Connects to correct host with a new socket  Writes message to server  Closes connection

16 University of Pittsburgh Computer Science 15 Week 5: Two-Way Communication In the first exampleIn the first example èClient sends, server receives èServer has simple loop, client just a few lines What if both client and server want to send and receive data?What if both client and server want to send and receive data? èIf sending and receiving are synchronized, we do not have to complicate things much èWhat if server and client want to be able to send to each other at any time?  We need to use Threads

17 University of Pittsburgh Computer Science 16 Week 5: Two-Way Communication Both sides of the communication must be ready to send or receive a message at any timeBoth sides of the communication must be ready to send or receive a message at any time èWithout Threads, they could end up deadlocked CLIENT SERVER Client trying to send message to server Server trying to send message to client

18 University of Pittsburgh Computer Science 17 Week 5: Two-Way Communication We need a way for each side to accept and receive messages at any timeWe need a way for each side to accept and receive messages at any time èOne Thread for reading èOne Thread for writing SERVER Client Thread waiting to accept messages Server Thread sending messages Server Thread waiting to accept messages Client Thread sending messages CLIENT

19 University of Pittsburgh Computer Science 18 Week 5: Two-Way Communication Look at next exampleLook at next example èTwoWayServer.java èTwoWayClient.java èCompile TwoWayServer.java  Run with your name on command line –java TwoWayServer yourname –Now your computer will be a server that some other student can connect to in order to talk èCompile TwoWayClient.java  Run with server name and your name –java TwoWayClient servername yourname –Where servername is the computer of another student that you want to talk to

20 University of Pittsburgh Computer Science 19 Week 5: Two-Way Communication Let’s look at the codeLet’s look at the code èNote that both programs look similar  Threads are created for the input stream and for the output stream  Each runs until the client/server has quit èThe threads are NOT synchronized (no monitors used)  Could cause a problem, but in this simple example it is not absolutely necessary  If we have more complicated communication, monitors will be definitely required –Could lead to data consistency problems otherwise

21 University of Pittsburgh Computer Science 20 Week 5: More Complex Communication Consider now a single server that serves many clientsConsider now a single server that serves many clients èEach client connects to the server èClients interact with server èClients also interact with each other through server èServer needs to divide its time amongst clients èServer must coordinate interaction with clients such that data is always consistent

22 University of Pittsburgh Computer Science 21 Week 5: More Complex Communication SERVER CLIENT Additional links not shown

23 University of Pittsburgh Computer Science 22 Week 5: More Complex Communication Server's job is quite complicatedServer's job is quite complicated èAllows clients to log in and out and keeps track of them èTakes messages from each client and passes them on to the other clients èSees to it that clients' messages do not interfere with one another  "If you all talk at once, no one will understand what you are saying!"  Server has to regulate how messages are passed

24 University of Pittsburgh Computer Science 23 Week 5: More Complex Communication SERVER CLIENT Hey, listen to me! One at a time and you'll all get your turn! No, listen to me!

25 University of Pittsburgh Computer Science 24 Week 5: More Complex Communication How can we do this in Java?How can we do this in Java? èThe server needs to have a thread for each client  Each thread will in a sense serve the needs of one client èThe threads MUST be synchronized  There is a lot of critical data in the server that must only be accessed by one thread at a time –Updating the number of clients online –Sending a message to the clients online èLet’s look at an example – a Chat Server

26 University of Pittsburgh Computer Science 25 Week 5: More Complex Communication General idea:General idea: èClient logs into server and is added to list of clients  Client is assigned a thread in the server program  Server synchronizes client threads for data consistency èClient also has a thread for receiving messages  However, no thread used to send messages – a Button and ActionEvent are used to process them  They are then sent to the server èClient also has a GUI so that users will be comfortable

27 University of Pittsburgh Computer Science 26 Week 5: More Complex Communication Let’s try the programLet’s try the program èDownload JRChatServer.java èDownload JRChatClient.java èCompile and run JRChatClient.java èUse it for a while … log in and out … send messages … see what is going on èNow look at the code  Discuss

28 University of Pittsburgh Computer Science 27 Week 5: Summary This week we discussedThis week we discussed èIdea of servers and clients èCommunication between server and clients  Connection-oriented communication with sockets  Connectionless communication with packets  If two-way, need for threads èUsing a server with multiple clients  How to coordinate the clients with threads and monitors


Download ppt "University of Pittsburgh Computer Science 1 Week 5: Introduction Last week we discussedLast week we discussed èDifference between executing sequentially."

Similar presentations


Ads by Google