Lab 10: Sockets 12:00 PM, Apr 4, 2018

Similar documents
Lab 5: Java IO 12:00 PM, Feb 21, 2018

Lecture 11.1 I/O Streams

Lab 2: File Input and Output

CS18000: Programming I

Internet Technology 2/7/2013

Java Support for developing TCP Network Based Programs

Byte and Character Streams. Reading and Writing Console input and output

CS September 2017

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

Distributed Systems Recitation 2. Tamim Jabban

Pieter van den Hombergh Richard van den Ham. March 17, 2018

Simple Java Input/Output

Lab 1: Setup 12:00 PM, Sep 10, 2017

Java Input/Output. 11 April 2013 OSU CSE 1

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

Programming Languages and Techniques (CIS120)

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

CS 351 Design of Large Programs Sockets Example

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

Exceptions and Design

PIC 20A Streams and I/O

Good Coding Practices Spring 2018

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Info 408 Distributed Applications programming 2 nd semester of 2017/2018 Credits: 5 Lecturer: Dr. Antoun Yaacoub

CS193j, Stanford Handout #26. Files and Streams

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Lab 4: Imperative & Debugging 12:00 PM, Feb 14, 2018

Lecture 8: Iterators and More Mutation

Introduction to Sockets

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Distributed Programming - Sockets

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

Unit 9: Network Programming

TCP connections. Fundamentals of Internet Connections Objectives. Connect to an Echo port. java.net.socket

CS2 Advanced Programming in Java note 8

Writing a Protocol Handler

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Scala Style Guide Spring 2018

Principles of Software Construction. Introduction to networks and distributed systems School of Computer Science

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Lecture 5: Implementing Lists, Version 1

CS 355. Computer Networking. Wei Lu, Ph.D., P.Eng.

Programming Languages and Techniques (CIS120)

Tips from the experts: How to waste a lot of time on this assignment

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

ICOM 4015-Advanced Programming. Spring Instructor: Dr. Amir H. Chinaei. TAs: Hector Franqui, Jose Garcia, and Antonio Tapia. Reference: Big Java

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

File Processing in Java

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Distributed Systems Recitation 2. Tamim Jabban

3. Remote Procedure Call

Multi-threaded Web Server (Assignment 1) Georgios Georgiadis

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

Socket 101 Excerpt from Network Programming

Homework 5: Fun with Scala Due: 5:00 PM, Mar 16, 2018

CS 2113 Software Engineering

Programming Languages and Techniques (CIS120)

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

Files and Streams

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

HST 952. Computing for Biomedical Scientists Lecture 8

Software Practice 1 - File I/O

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

Lab 8: Introduction to Scala 12:00 PM, Mar 14, 2018

Lesson 3: Accepting User Input and Using Different Methods for Output

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Exceptions and Working with Files

protocols September 15,

Lecture 17: Implementing HashTables 10:00 AM, Mar 5, 2018

Principles of Computer Science I

Sri Vidya College of Engineering & Technology Question Bank

needs to be reliable, easy to change, retarget efficiency is secondary implemented as interpreter, with virtual machine

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

10/7/15. MediaItem tostring Method. Objec,ves. Using booleans in if statements. Review. Javadoc Guidelines

CSCD 330 Network Programming Spring 2018

Homework 4: Hash Tables Due: 5:00 PM, Mar 9, 2018

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

Intro. Scheme Basics. scm> 5 5. scm>

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CS 251 Intermediate Programming Java I/O Streams

Transcription:

CS18 Integrated Introduction to Computer Science Fisler, Nelson Lab 10: Sockets 12:00 PM, Apr 4, 2018 Contents 1 The Client-Server Model 1 1.1 Constructing Java Sockets................................. 2 1.2 Communicating via Java Sockets............................. 2 2 Client 3 3 Server 5 Objectives By the end of this lab, you will know: ˆ How the client-server model interacts ˆ All about Java s built-in sockets By the end of this lab, you will be able to: ˆ Write a client that connects to the TAs server ˆ Write your own server, and connect your client to it Running From The Command-Line In this lab you ll be creating and running both a client and a server. These have to respond to CTRL-D, but Eclipse can be a little funky with CTRL-D, so we encourage you to run this from the command line instead, should you encounter an issue. To compile your code cd into the directory containing your bin, sol, and src directories. Run the following: scalac -d bin */lab10/*/*.scala This will create binaries in your bin file. To run this code, cd into the bin directory and run the following: scala lab10.sol.[name OF OBJECT/CLASS HERE] [ARGUMENTS HERE] When you get to testing your client and server, you should be running these from separate terminals, and you should run your server first so that it can accept your client s connection!

1 The Client-Server Model So far in CS 18, you ve explored with how different parts of one single program can communicate with one another. In this lab, you ll learn how different programs, possibly running on different machines, can communicate with one another, through the client-server model. Most modern programming languages support this model, or some similar abstraction. To understand the client-server model, imagine placing a telephone call to a customer service center. The client is the person placing the call. The server is the customer service center. The client asks questions or requests information, and is the active entity. The server responds with information, thus serving the client. The server is a passive entity the customer service agent doesn t offer to help with something the client didn t ask for. Computer programs can also communicate in this fashion, with some more active entities (clients), and other more passive entities (servers). In this lab, you will learn about Java s support for the client-server model, namely sockets. Scala does not have its own implementation of sockets, so we ll be relying on Java sockets for both this lab and your two remaining projects. 1.1 Constructing Java Sockets Sockets are endpoints in a two-way communication channel. The idea is this: you connect two sockets together, and each end of the connection can both send and receive data. In this lab, you ll be using the Java classes Socket and ServerSocket. There are many Socket constructors. The one you will be using today is Socket(host: String, port: Int). This will: ˆ Construct a Socket and ˆ Connect that Socket to a ServerSocket running on the specified host and port. The word host in this context refers to the name of the computer the server is running on. So, if you re running a server on the machine cslab1a, then you would specify cslab1a as the host in the Socket constructor. Or, if you are running a server on the same machine as your client, you would use either your machine s name, or localhost. A host provides ports, each of which is a communication endpoint corresponding to a unique client. No two clients can have the same port number on the same server. A server listens on a port to wait for an incoming connection on that port number. To service multiple clients, a server listens on multiple port numbers. On the other end, client Sockets can connect to the same server on different ports. Together, this allows multiple clients to interact with a single server without interfering with each other. Following the earlier call center analogy, a port could be represented by a single customer service agent: each agent can only listen to one Client at a time. To construct a ServerSocket, you will use the ServerSocket(port: Int) constructor. For a ServerSocket to connect with a client Socket, both must specify the same port number. Today in lab, you will write two programs: a client and a server. 2

1.2 Communicating via Java Sockets As previously mentioned, one can both send and receive data from either a Socket and a ServerSocket. To service this, every Socket has an InputStream and an OutputStream. You have already worked with these before! System.in, which reads from the keyboard, is an InputStream, and System.out, which prints to the console, is an OutputStream. Imagine two programs, A and B, that are communicating via a socket. Program A can write to its OutputStream anything it wants to send to program B. Furthermore, program A can read on its InputStream anything that program B writes to program B s own OutputStream. A output/b input A B A input/b output To obtain these streams from the Socket, use the methods getinputstream and getoutputstream. For example, if socket is a Socket, then: val istream = socket.getinputstream val ostream = socket.getoutputstream When you re done sending data, you must call the shutdownoutput method on ostream to close the OutputStream and send an EOF (end of file). Similarly, when you re done listening for data, you must call shutdowninput on istream. Now, you might be saying, how exactly can one send and read this data? To send data through an OutputStream, you should create a BufferedWriter, then use the write method, then flush the writer. Suppose ostream is an OutputStream we want to write to: BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(oStream)); writer.write(<something>); writer.flush(); To receive data from InputStream, you can equivalently use a BufferedReader. Suppose istream is an InputStream we are reading from: BufferedReader reader = new BufferedReader(new InputStreamReader(iStream)); reader.read(); Tip: Think of reading and writing to a stream exactly like reading and writing to a File, as seen in your previous labs and assignments! 3

2 Client Now that you know all about the client-server model, how to connect two programs through Sockets, and how to communicate through these Sockets, it s time to write a client! Aquaman NEEDS YOUR HELP! He s lost his ability to talk with his fish. He needs you to help him setup a communication channel with his fish. Start by making a client for Aquaman. Task: Create a file named Client.scala, containing a class called Client(host: String), where host is the hostname. Your file should contain an object Client with a main method that runs your client. Your class should also contain a run method which contains the full functionality, specified below. Its signature should be: @throws(classof[ioexception]) def run() { //your code here } Note: Because this method throws an Exception, you will need to catch it in your main method. Task: Write an interactive client that performs the following functionality: 1. Connects to our provided server, then does the following until the user types a null (CTRL-D). ˆ Note that null is not a type of String. In fact, null is not instanceof anything. (CTRL- D) will send null through to the user if passed through the command line (doesn t always work with Eclipse!). 2. Reads user input on System.in. 3. Sends this input to the server. Once the user sends a null the client should: 4. Read in the server s response until it reaches EOF. 5. Print the server s response to System.out (the console). Note: Unlike in other assignments, you should not use readline. Instead, use BufferedReader s read 1 method. This reads one character at a time, and returns -1 when CTRL + D is pressed or at the end of the stream. Tips/Roadmap: ˆ Create a Socket using the Socket(host: String, port: Int) constructor to connect your client to the server. You should specify the port as a constant, like in the example below. Note that 1818 is only an example - you should use the port numbers that the TAs provide you in the beginning of lab. private val Port =1818 1 You can find the Javadocs for readhere. 4

ˆ You ll need to import java.io._ and java.net._. ˆ Create all your necessary readers and writers: A writer for your client to write to the Socket A reader for your client to read from the Socket ˆ You should use System.out.write to print to the console! Because you are using read, you will be reading everything as an int, so using println will read the characters as integers, not as characters. ˆ Similarly, use System.in.read() to read from the console. ˆ In your first loop: Read data input from the user on System.in Send this input to the server through the socket s OutputStream (using write 2 and flush) ˆ Once you read a null from the user, the client should disconnect! You should shutdown output at this point, since you will not be writing anything else to the server. When the read() method returns -1, this means you have read a null from the user. ˆ After the client has disconnected, your second loop should: Read the server s response from the socket s InputStream Print this response to the console ˆ After your second loop, you should flush System.out and shutdown input, since there s nothing left for you to read from the server. ˆ Once the server s response has been fully printed back to the user, the program should terminate! Don t forget to close all of your various readers and writers and your socket! Task: During this lab, the TAs will run an echo server that reads whatever input is sent, then sends the same input back. At the beginning of the lab, the TAs will announce their server s host and port number. Test your client by connecting to this server! Note: The server won t echo the input until you send an EOF (ctrl + d). Task: The TAs will also be running a Racket server, which evaluates Racket code passed to it. Connect to this server and try evaluating some Racket: (+ 1 2) (+ (* 18 18) 4) 2 You can find the Javadocs for writehere. 5

Note: The Racket server will only evaluate one line of code for one client. Don t worry if you type multiple lines of code and the server only responds to the first one! Also note this server takes a couple seconds to start up the Racket evaluator, so don t worry if it takes a little bit. You ve reached a checkpoint! Please call over a lab TA to review your work. 3 Server We need some way for the fish to hear Aquaman. Create a server so that all of the fish of the sea can listen in on Aquaman s commands. Now that you ve written your first client, it s time to write your own server! As mentioned before, a server can service multiple clients. This means that the server shouldn t terminate when a client terminates or disconnects it should keep going. This is done through a deliberate infinite loop. During each iteration of this loop, the following should happen: 1. Wait for a Socket to connect, using the ServerSocket s accept method. Once a Socket connects, the accept method will return that Socket. Save it. The server will use this Socket to send and receive data to (and from) the client. 2. Once you ve established a connection, your server will need to receive all the data sent to it by the client, and send a response back. So, you should read data on the Socket s InputStream, and write a response to the Socket s OutputStream. As when creating your client, we suggest you use a BufferedReader and BufferedWriter to accomplish this. 3. Flush the output, then call the shutdowninput and shutdownoutput methods on the Socket. Note that these are not called on the ServerSocket, but rather, the Socket representing the connection between the server and a particular client. In summary, this loop will accept a client s request, respond to it, then close the associated Socket and wait for another client. Task: Make a new file Server.scala. In it, create a class called Server(). You should store your port number and ServerSocket as constants at the top of your class. Your file should also contain an object Server with a main method that runs your server. Your class should have a run method that runs all the necessary steps. Its signature should be: @throws(classof[ioexception]) def run() { //your code here } Note: Like before, you ll need to catch the exception potentially thrown by this method inside your main! Task: Write a server that does the following: 1. Receives text from a client. 2. Prints that text to System.out using the method System.out.write(). 6

3. Sends that same text back to the client. Tips/Roadmap: ˆ Create a ServerSocket using the ServerSocket(port: Int) constructor with the same port number that you used for your client. Put this at the top of your class. ˆ In your run method, be sure to write the infinite loop described above, but with the additional step that the received input is also printed to System.out. ˆ Don t forget to make your necessary readers and writers! One to read from the input stream, and one to write to the output stream. ˆ As always, don t forget to close any readers or writers you open, as well as your sockets and streams! Task: Use your client to test your server. To do this, start running your server (see the section near the beginning of the PDF on how to do this from the command line). Once that is running, run your client (being sure to connect it to the right host and port) and start typing into the client s console. Just for Fun: Have your friends and neighbors run their servers and try to connect to them as well. (But don t worry: your server does not need to handle multiple clients simultaneously. Multithreading is required for that functionality stay tuned for CS 33!) Note: To do this, your client will need to know your friends hostnames. On all computers in our department, this can be found on a nametag somewhere either on your monitor or your computer. Pass that in as the hostname, and don t forget to use the same port number on the client and server! Note: You should run the server before you run your client, like you did before! Note: This Just for Fun task is not required to exit the lab early. Once a lab TA signs off on your work, you ve finished the lab! Congratulations! Before you leave, make sure both partners have access to the code you ve just written. Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS18 document by filling out the anonymous feedback form: http://cs.brown.edu/ courses/cs018/feedback. 7