Week 13 Lab - Exploring Connections & Remote Execution

Similar documents
Previous lecture: threads G51PRG: Introduction to Programming Second semester Lecture 12 URL

JAVA - NETWORKING (SOCKET PROGRAMMING)

WINDOWS SOCKET PROGRAMMING

Networking Code CSCI 201 Principles of Software Development

Object-Oriented Programming in Java

Lab 1 : Java Sockets

Sockets and RMI. CS151 Chris Pollett Dec. 5, 2005.

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

Introduction to Java.net Package. CGS 3416 Java for Non Majors

A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs.

Distributed Systems COMP 212. Lecture 8 Othon Michail

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

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

Chapter 2. Network Chat

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

Principles, Models, and Applications for Distributed Systems M

Chapter 4: Processes

Java for Interfaces and Networks

Networking with java (socket programming) a brief study

Steps for Implementing a Server

SERVER/CLIENT NETWORKING AT JAVA PLATFORM

Network Programming: Servers

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

Java Socket Workshop. July Purpose of this workshop:

Principles, Models, and Applications for Distributed Systems M

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

} } class ClientHandler extends Thread { private Socket client; private Scanner input; private PrintWriter output;

protocols September 15,

School of Informatics, University of Edinburgh

Networking and Security

CSCD 330 Network Programming Spring 2018

Java Support for developing TCP Network Based Programs

Input from Files. Buffered Reader

public static void main(string[] args) throws IOException { sock = new Socket(args[0], Integer.parseInt(args[1]));

CSCD 330 Network Programming Winter 2019

Chapter 4: Processes. Process Concept. Process State

Java for Interfaces and Networks (DT3029)

M257 Past Paper Oct 2007 Attempted Solution

Multi-threaded Web Server (Assignment 1) Georgios Georgiadis

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

JAVA - FILE CLASS. The File object represents the actual file/directory on the disk. Below given is the list of constructors to create a File object

Assignment 1. Due date February 6, 2007 at 11pm. It must be submitted using submit command.

2018/2/5 话费券企业客户接入文档 语雀

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

CSS 533 Program 2: Distributed Java Space Servers Professor: Munehiro Fukuda Due date: see the syllabus

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

THIS EXAMINATION PAPER MUST NOT BE REMOVED FROM THE EXAMINATION ROOM

Lab 11. A sample of the class is:

CSCD 330 Network Programming

Java for Interfaces and Networks (DT3010, HT11)

CSCD 330 Network Programming

Interprocess Communication

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

CS 10: Problem solving via Object Oriented Programming. Client/Server

CSCD 330 Network Programming

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Networking Basics. network communication.

Java for Interfaces and Networks (DT3029)

Process management. Desarrollo de Aplicaciones en Red. Process in memory. Concepts. Process state diagram. Process state

Advanced Java Programming. Networking

Internet Technology 2/7/2013

TCP Networking in Java. Some reminders

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

CSCD 330 Network Programming Spring 2018

JAVA Programming Language Homework VI: Threads & I/O

Topic 10: Network Programming

CS 351 Design of Large Programs Sockets Example

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

Question 1. (2 points) What is the difference between a stream and a file?

Java Socket Application. Distributed Systems IT332

Network Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

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

Multiplayer Game Programming 2/26

Generic architecture

CS 335 Lecture 02 Java Programming

Java Input/Output. 11 April 2013 OSU CSE 1

More on Objects in JAVA TM

CSCI 201L Written Exam #1 Fall % of course grade

Network Programming 1

Principles, Models and Applications for Distributed Systems M

CS Week 14 Page 1

Java Networking (sockets)

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

A Guide to Running Map Reduce Jobs in Java University of Stirling, Computing Science

CT51 WEB TECHNOLOGY ALCCS-FEB 2014

Advanced Internet Technology Lab # 4 Servlets

CS 200 File Input and Output Jim Williams, PhD

CPSC 441 Tutorial - 11 UDP Socket Programming Department of Computer Science University of Calgary

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

IT101. File Input and Output

Socket 101 Excerpt from Network Programming

Input-Output and Exception Handling

coe318 Lab 1 Introduction to Netbeans and Java

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2012

CS 2113 Software Engineering

Networking and socket communica2on

CONCURRENCY IN JAVA Course Parallel Computing

What is Serialization?

OBJECT ORIENTED PROGRAMMING

Transcription:

Week 13 Lab - Exploring Connections & Remote Execution COSC244 & TELE202 1 Assessment This lab is worth 0.5%. The marks are awarded for completing the programming exercise and answering the questions. Answers to the questions should be stored electronically in a plain text file with a.txt suffix in your /244/13 directory. Make sure you get your work signed off by a demonstrator before leaving the lab. 2 Purpose This lab has two purposes: 1. To more fully understand the concept of a network connection. 2. To explore execution on more than one machine. 3 A Network Connection A network connection is a communication path from a program running on one machine to a program running on another machine. In the previous labs, the two machines have in fact been the same machine. It was the machine you were using. It was referred to as localhost which is always the name for the machine you are sitting in front of and is denoted by the IP address, 127.0.0.1. A network connection is identified by the IP address of the local machine, the port on the local machine, the IP address of the remote machine, and the port on the remote machine. It is designated as: local_ip:local_port remote_ip:remote_port When you wish to establish a connection to a service running on a remote machine, you specify its IP address and the port number the service is listening on. You are doing this implicitly 1

when you type something like: ssh hex.otago.ac.nz What s this, no port number specified? Did I just lie to you? Actually no. The ssh server daemon, called sshd, is listening on port 22. As you know from lectures, the low numbered ports are what are called well-known ports. The /etc/services file establishes a correspondence between these ports and their programs. Therefore when you type the above command, you are explicitly invoking the ssh program on your machine and implicitly giving the port number on the remote machine (in this case, port 22). The remote machine s IP address is obtained by the DNS service. Hex s IP address is currently 139.80.32.172. In most cases, the local port number is chosen by the operating system from the available and unused high numbered ports. Logically the situation corresponds to Figure 1. Your Machine hex ssh 139.80.32.172:22 sshd 139.80.98.164:56789 Figure 1: A Network Connection Hex s end of the connection is identified by 139.80.32.172:22 indicating its IP address and port number. The IP address of your machine depends on which machine you are using. The port number used on your machine depends on the port number assigned by the operating system. In this example, I assumed you were using a machine with 139.80.98.164 for an IP address. I also assumed the operating system gave ssh port 56789 when starting up. 4 Preparation In this lab, we will alter our familiar Client.java and ChatServer.java to output information about the IP address and port number of the local and remote sockets in use. In a web browser, open the course web page: http://www.cs.otago.ac.nz/cosc244/ Click on Resources, then click on Java API Documentation. java.net. Click on the Socket class of Q1 : What does the method getlocalport() return? 2

Q2 : What does the method getinetaddress() return? Q3 : What does the method getport() return? 5 Program Exercise For the lab part, you will need two machines. So find a place in the lab with two machines facing each other. Copy the files needed by typing: cp -r /home/cshome/coursework/244/start/13. This gives you ReadWriteThread.java, Client.java, and ChatServer.java. These programs are the same as we have been using in the past labs. You will make minor modifications to both so they report some port and IP address information. Rename Client.java to Client2.java, open it in an editor, and change it so it looks like the code given below. 1 // Client2.java 2 3 import java.net.*; 4 5 public class Client2 { 6 7 public static void main(string[] args) { 8 try { 9 int port = Integer.parseInt(args[0]); 10 String host = args[1]; 11 Socket socket = new Socket(host, port); 12 System.err.println("Connected to " + socket.getinetaddress() + 13 " on port " + socket.getport() + 14 " using local port " + socket.getlocalport()); 15 new ReadWriteThread(System.in, socket.getoutputstream()).start(); 16 new ReadWriteThread(socket.getInputStream(), System.out).start(); 17 } catch (Exception e) { 18 e.printstacktrace(); 19 System.err.println("\nUsage: java Client2 <port> <host>"); 20 } 21 } 22 23 } The class name is changed from Client to Client2 in three places (One change is a comment at the beginning of the file). The other change is the insertion of the statement given in lines 12-14. Q4 : Based on your preparation for the lab, what does this new statement do? 3

Compile the program. Rename ChatServer.java to ChatServer2.java, open it in an editor, and change it so it looks like the code given below. 1 // ChatServer2.java 2 3 import java.io.*; 4 import java.net.*; 5 import java.util.*; 6 7 public class ChatServer2 { 8 9 private static List<ClientHandler> clients = new LinkedList<ClientHandler>(); 10 11 public static void main(string[] args) { 12 try { 13 new ChatServer2().startServer(Integer.parseInt(args[0])); 14 } catch (Exception e) { 15 e.printstacktrace(); 16 System.err.println("\nUsage: java ChatServer2 <port>"); 17 } 18 } 19 20 public void startserver(int port) throws Exception { 21 ServerSocket serversocket = new ServerSocket(port); 22 System.err.println("ChatServer2 started, listening on port " + 23 serversocket.getlocalport()); 24 while (true) { 25 ClientHandler ch = new ClientHandler(serverSocket.accept()); 26 System.err.println("Clients ID is " + ch); 27 synchronized (clients) { 28 clients.add(ch); 29 } 30 ch.start(); 31 } 32 } 33 34 public static void sendall(string line, ClientHandler sender) { 35 System.err.println("Sending " + line + " to : " + clients); 36 synchronized (clients) { 37 for (ClientHandler cl : clients) { 38 cl.send(sender + ": " + line); 39 } 40 } 41 } 42 43 public static class ClientHandler extends Thread { 44 45 private BufferedReader input; 46 private PrintWriter output; 47 private String id; 48 private static int count = 0; 49 50 public ClientHandler(Socket socket) throws Exception { 51 input = new BufferedReader( 52 new InputStreamReader(socket.getInputStream())); 4

53 output = new PrintWriter(socket.getOutputStream(), true); 54 id = "client_" + ++count; 55 System.err.println("Accepted connection:"); 56 System.err.println("Clients host " + socket.getinetaddress() + 57 " Clients port " + socket.getport()); 58 System.err.println("Local port " + socket.getlocalport()); 59 } 60 61 public void send(string line) { 62 output.println(line); 63 } 64 65 public String tostring() { 66 return id; 67 } 68 69 public void run() { 70 try { 71 send("welcome! You are " + this + "."); 72 String line; 73 while ((line = input.readline())!= null) { 74 sendall(line, this); 75 } 76 } catch (IOException e) { 77 e.printstacktrace(); 78 } finally { 79 synchronized (clients) { 80 clients.remove(this); 81 } 82 System.err.println(this + " closed connection!"); 83 } 84 } 85 } 86 } The class name is changed from ChatServer to ChatServer2 in five places (One of these changes is the comment at the beginning of the file). The other changes are as follows: 1. The statement following line 21 is changed to the statement shown in lines 22-23. 2. The statement in line 26 is modified. 3. In the constructor for ClientHandler, lines 55-58 are new. Q5 : Based on your preparation for the lab, what do lines 55-58 do? Compile the program. If you have not already done so, log into your second machine. Bring up a single terminal window and cd to the directory you are working in. 5

Type hostname and note the name of the machine. Type java ChatServer2 7777 to start the server. On your original machine, have three terminal windows open and cd each to the directory you are working in. In each window, start a client by typing java Client2 7777 server where server is the machine name you noted above. Q6 : Using the data displayed in the server window and each client window, draw a figure showing the server and each client. The figure should look similar to Figure 1. Your figure will have three clients instead of one. Label each socket with its IP address:port number. You get this data from the information displayed in the windows. Once you have completed your figure, leave the clients and server running so the demonstator can check your figure for correctness. Marking Hand the demonstrator a piece of paper giving the IP address of the machine running Chat- Server2 and the port number it is listening on. Once you know the demonstator has connected to the server, type a message in one of your clients to request that your lab work be marked off. Before leaving the lab, make sure you log off both machines. 6