CS 351 Design of Large Programs Sockets Example

Similar documents
CS 5010: PDP. Lecture 11: Networks CS 5010 Fall 2017 Seattle. Adrienne Slaughter, Ph.D.

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

Distributed Systems Recitation 2. Tamim Jabban

Distributed Systems Recitation 2. Tamim Jabban

Unit 9: Network Programming

Sockets: Network io HOM DOS HVD HEE. Sockets, Object Streams and Serialization. Sockets. Sockets: Network io HOM DOS HVD HEE

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

Internet Technology 2/7/2013

Sockets: Network io HOM HVD. Sockets, Object Streams and Serialization. Sockets. Sockets: Network io HOM HVD

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

CS 251 Intermediate Programming Java I/O Streams

Lab 1 : Java Sockets

See What Your Robot Sees

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

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

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

Networking Basics. network communication.

CS September 2017

protocols September 15,

CS 351 Design of Large Programs Threads and Concurrency

Socket 101 Excerpt from Network Programming

IT101. File Input and Output

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

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

CS 2113 Software Engineering

Networking and Security

Distributed Programming - Sockets

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

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

Advanced Java Programming. Networking

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

Topic 10: Network Programming

World Scientific Research Journal (WSRJ) ISSN: The Implementation of Tcp Socket Programming based on Java

CSCD 330 Network Programming Spring 2018

JAVA - NETWORKING (SOCKET PROGRAMMING)

Lab 2: File Input and Output

Java Socket Application. Distributed Systems IT332

CSCD 330 Network Programming

CSCD 330 Network Programming

School of Informatics, University of Edinburgh

Network Programming Benoît Garbinato

CSCD 330 Network Programming

Java.net Package and Classes(Url, UrlConnection, HttpUrlConnection)

Object-Oriented Programming in Java

Java Support for developing TCP Network Based Programs

Basic Java Network Programming. CS211 July 30 th, 2001

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

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

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

Lecture 11.1 I/O Streams

Steps for Implementing a Server

Network Programming: Servers

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

Java A.1 TCP/IP TCP. TCP_RO.java import java.net.*; import java.io.*;

Chapter 2. Network Chat

Input from Files. Buffered Reader

CS18000: Programming I

SOCKETS. COMP750 Distributed Systems

pre-emptive non pre-emptive

CS2307 NETWORKS LAB 1. Programs using TCP Sockets (like date and time server & client, echo server & client, etc.) 2. Programs using UDP Sockets

CSC Java Programming, Fall Java Data Types and Control Constructs

Principles, Models, and Applications for Distributed Systems M

PIC 20A Streams and I/O

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

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

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

Distributed Systems COMP 212. Lecture 8 Othon Michail

URL Kullanımı Get URL

CS 251 Intermediate Programming Java I/O File I/O

Java Basics 5 - Sockets. Manuel Oriol - May 4th, 2006

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.

Network Programming 1

Introduction to Sockets

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

M257 Past Paper Oct 2007 Attempted Solution

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Object-Oriented Programming Design. Topic : Streams and Files

Redesde Computadores(RCOMP)

Redes de Computadores (RCOMP)

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

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

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Lecture 4: Exceptions. I/O

CSCD 330 Network Programming Spring 2018

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

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

Networking Code CSCI 201 Principles of Software Development

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

A Guide Illustrating the Core Java Equivalent to Selected Tasks Done Using the HSA Class Library

CS 251 Intermediate Programming Java Basics

Objec&ves. Review. Standard Error Streams

Lecture 3: Socket Programming

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

Java Input/Output. 11 April 2013 OSU CSE 1

4. Finding & Displaying Record of Salesman with minimum net income. 5. Finding & Displaying Record of Salesman with maximum net income.

Darshan Institute of Engineering & Technology for Diploma Studies

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

CS 251 Intermediate Programming Methods and More

CSPP : Introduction to Object-Oriented Programming

Transcription:

CS 351 Design of Large Programs Sockets Example Brooke Chenoweth University of New Mexico Spring 2019

Socket Socket(String host, int port) InputStream getinputstream() OutputStream getoutputstream() void close()

ServerSocket ServerSocket(int port) Socket accept() blocks until connection is made void close()

Knock Knock Example Adapted from sockets tutorial on Oracle site. KnockKnockServer is running on particular machine and port. KnockKnockClient programs connect to server and are told a joke.

KnockKnockClient: main public static void main ( String [] args ) throws IOException { String hostname = args [0]; int portnumber = Integer. parseint ( args [1]); try ( Socket socket = new Socket ( hostname, portnumber ); PrintWriter out = new PrintWriter ( socket. getoutputstream (), true ); BufferedReader in = new BufferedReader ( new InputStreamReader ( socket. getinputstream ())); ) { BufferedReader stdin = new BufferedReader ( new InputStreamReader ( System. in )); String fromserver = in. readline (); while ( fromserver!= null ) { System. out. println (" Server : " + fromserver ); if( fromserver. equals (" Bye.")) { break ; String fromuser = stdin. readline (); if( fromuser!= null ) { System. out. println (" Client : " + fromuser ); out. println ( fromuser ); fromserver = in. readline ();

KnockKnockServer: main public static void main ( String [] args ) throws IOException { int portnumber = Integer. parseint ( args [0]); ServerSocket serversocket = new ServerSocket ( portnumber ); // Listen for new clients forever while ( true ) { // Create new thread to handle each client Socket clientsocket = serversocket. accept (); KnockKnock kk = new KnockKnock ( clientsocket ); Thread t = new Thread ( kk ); t. start ();

KnockKnockServer: constants private static String BYE = " Bye."; private enum State { WAITING, SENT_ KNOCK_ KNOCK, SENT_CLUE, ANOTHER private static String [] clues = { " Turnip ", " Little Old Lady ", " Atch ", " Who ", " Who " ; private static String [] answers = { " Turnip the heat, it s cold in here!", " I didn t know you could yodel!", " Bless you!", " Is there an owl in here?", "Is there an echo in here?" ;

KnockKnock: init public static class KnockKnock implements Runnable { private final Socket clientsocket ; private PrintWriter out ; private BufferedReader in; private State state = State. WAITING ; private int currentjoke = 0; public KnockKnock ( Socket clientsocket ) throws IOException { this. clientsocket = clientsocket ; out = new PrintWriter ( clientsocket. getoutputstream (), true ); in = new BufferedReader ( new InputStreamReader ( clientsocket. getinputstream ()));

KnockKnock: run public void run () { String inputline = null ; String outputline ; do { outputline = processinput ( inputline ); out. println ( outputline ); if( outputline. equals ( BYE )) { break ; try { inputline = in. readline (); catch ( IOException ex) { inputline = null ; while ( inputline!= null );

KnockKnock: processinput 1 private String processinput ( String input ) { String output = null ; switch ( state ) { case WAITING : output = " Knock! Knock!"; state = State. SENT_ KNOCK_ KNOCK ; break ; case SENT_ KNOCK_ KNOCK : if( input. equalsignorecase (" Who s there?")) { output = clues [ currentjoke ]; state = State. SENT_ CLUE ; else { output = " You re supposed to say \" Who s there?\"! " + " Try again. Knock! Knock!"; break ;

KnockKnock: processinput 2 case SENT_CLUE : if( input. equalsignorecase ( clues [ currentjoke ] + " who?")) { output = answers [ currentjoke ] + " Want another? (y/n)"; state = State. ANOTHER ; else { output = " You re supposed to say \"" + clues [ currentjoke ] + " who?\"! " + " Try again. Knock! Knock!"; state = State. SENT_KNOCK_KNOCK ; break ; case ANOTHER : if( input. equalsignorecase ("y")) { output = " Knock! Knock!"; currentjoke = ( currentjoke + 1) % clues. length ; state = State. SENT_KNOCK_KNOCK ; else { output = BYE ; state = State. WAITING ; break ; return output ;

Knock Knock Protocol SENT KNOCK KNOCK WAITING SENT CLUE ANOTHER