Java sockets. From waiting.

Slides:



Advertisements
Similar presentations
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Advertisements

Java Sockets Source:
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
Moshe Fresko Bar-Ilan University
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
1 TCP socket application Architecture of Client-Server Applications Java Socket Programming Client Application Server Application.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
Java Networking -- Socket Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
System Programming Practical session 10 Java sockets.
Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 12, Distributed Computing Ajay Bajaj,
1 Fall 2005 Socket Programming Qutaibah Malluhi Computer Science and Engineering Qatar University.
1 Socket-based Client- Server Application Client-Server application architecture Choosing services – Connectionless atau Connection-oriented.
Welcome to CIS 235 Computer Networks Fall, 2007 Prof Peterson.
CSE 341, S. Tanimoto Java networking- 1 Java Networking Motivation Network Layers Using Sockets A Tiny Server Applets URLs Downloading Images, MediaTracker.
Networking Basics Computers running on the Internet communicate to each other using either the Transmission Control Protocol (TCP) or the User Datagram.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Servlet Communication Other Servlets, HTML pages, objects shared among servlets on same server Servlets on another server with HTTP request of the other.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Java Sockets Brad Vander Zanden. Agenda What are sockets Client/Server interaction Single client/server example Synchronized data objects Multi-threaded.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Peter Lee April 8, 2004.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Computer Networks & PROTOCOLS (CSI 4118) FALL 2005 Professor Robert L. Probert.
Java Server Programming Web Interface for Java Programs.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
1 Socket-based Client- Server Application Client-Server application architecture Choosing services – Connectionless atau Connection-oriented.
第十一讲 分布式编程 Socket and RMI 李庆旭. 2 本章内容提要  Socket TCP /IP protocol suit Socket A TCP Server and a TCP Client Supporting Multiple Concurrent Clients A UDP.
Java Networking I IS Outline  Quiz #3  Network architecture  Protocols  Sockets  Server Sockets  Multi-threaded Servers.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Java 13. Networking public class SumTest {
Echo Networking COMP
Threads in Java Two ways to start a thread
Source: Java Sockets Source:
Network Programming in Java CS 1111 Ryan Layer May 3, 2010
CSE 341, S. Tanimoto Java networking-
Block 15 Developing the Calculator application
Socket-based Client-Server Application
Client-server Programming
„Networking”.
CSI 4118 – UNIVERSITY OF OTTAWA
CSCD 330 Network Programming
Socket-based Client-Server Application
Socket Programming 2: Application Layer.
CS18000: Problem Solving and Object-Oriented Programming
Presentation transcript:

Java sockets

From waiting

Main Differences from c Library call sequencing is not nearly as important I/O uses standard streams hooked to the socket Basic activity looks more like other java streams.

Clients

import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket("taranis", 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: taranis."); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } Client Server machine Server socket

// create an input device similar to the socket reader but for reading stdin BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; // read from the keyboard to send to the echo server while ((userInput = stdIn.readLine()) != null) { // write it out to the server through the socket out.println(userInput); // read the echo from the echo server and display it System.out.println("echo: " + in.readLine()); } out.close(); // close the output stream associated with the socket in.close(); // close the input stream associated with the socket stdIn.close(); // close standard input (keyboard) echoSocket.close(); // close the socket

Servers

What’s different about servers? Open a socket for service requests try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.out.println("Could not listen on port: 4444"); System.exit(-1); } Accept connection from the client Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); } Talk with client on “clientsocket”

Talk to the client on the clientsocket outputLine = …….. //define first output.. Server goes first!! out.println(outputLine); while ((inputLine = in.readLine()) != null) { outputLine = …….. //get next output.. (response) out.println(outputLine); if outputLine.equals("Bye.")) break; } PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); String inputLine, outputLine;

Client-Server Interaction Maintaining State

State Clients and server interaction requires knowledge of previous activity to properly interpret and process current activity. Example –Client requests file in ftp –Has the request be properly preceded by a successful login?

State diagram disconnected Send pw Waiting response connected Send acceptance connected client server Process is in a particular state. Stimulus causes state change. Stimulus caused by other end of socket client for server, server for client

Example Kermit

A typical interaction Station AStation B S Y F Z B Y Y Y S - Send Initiation Y - ACK F - File Header D - Data Frame Z - End of File B - End of transmission DATA

Interactions are complex Certain message types only have relevance in response to the previous stimulus Tracking this interaction requires knowledge of state A simple example follows in the knock- knock server

Knock-Knock Server An example of state management

On the surface Server: Knock! Knock! Who's there? (Client) Server: Turnip Turnip who? (Client) Server: Turnip the heat, it's cold in here! Want another? (y/n)

What is the client doing first? while ((fromServer = in.readLine()) != null) { System.out.println("Server: " + fromServer); if (fromServer.equals("Bye.")) break; fromUser = stdIn.readLine(); if (fromUser != null) { System.out.println("Client: " + fromUser); out.println(fromUser); }

And the protocol defining interaction public class KnockKnockProtocol { private static final int WAITING = 0; private static final int SENTKNOCKKNOCK = 1; private static final int SENTCLUE = 2; private static final int ANOTHER = 3; private static final int NUMJOKES = 5; private int state = WAITING; private int currentJoke = 0; private String[] clues = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" }; private 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?" };

public String processInput(String theInput) { String theOutput = null; if (state == WAITING) { theOutput = "Knock! Knock!"; state = SENTKNOCKKNOCK; } else if (state == SENTKNOCKKNOCK) { if (theInput.equalsIgnoreCase("Who's there?")) { theOutput = clues[currentJoke]; state = SENTCLUE; } else { theOutput = "You're supposed to say \"Who's there?\"! " + "Try again. Knock! Knock!"; } } else if (state == SENTCLUE) { if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) { theOutput = answers[currentJoke] + " Want another? (y/n)"; state = ANOTHER; } else { theOutput = "You're supposed to say \"" + clues[currentJoke] + " who?\"" + "! Try again. Knock! Knock!"; state = SENTKNOCKKNOCK;} } else if (state == ANOTHER) { if (theInput.equalsIgnoreCase("y")) { theOutput = "Knock! Knock!"; if (currentJoke == (NUMJOKES - 1)) currentJoke = 0; else currentJoke++; state = SENTKNOCKKNOCK; } else { theOutput = "Bye."; state = WAITING;} } return theOutput; }

Lastly the server itself PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); String inputLine, outputLine; // initiate conversation with client KnockKnockProtocol kkp = new KnockKnockProtocol(); outputLine = kkp.processInput(null); out.println(outputLine); while ((inputLine = in.readLine()) != null) { outputLine = kkp.processInput(inputLine); out.println(outputLine); if outputLine.equals("Bye.")) break; }