Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java sockets. From waiting.

Similar presentations


Presentation on theme: "Java sockets. From waiting."— Presentation transcript:

1 Java sockets

2 From http://java.sun.com/docs/books/tutorial/networking/sockets/index.html waiting

3 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.

4 Clients

5 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

6 // 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

7 Servers

8 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”

9 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;

10 Client-Server Interaction Maintaining State

11 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?

12 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

13 Example Kermit

14 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

15 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

16 Knock-Knock Server An example of state management

17 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)

18 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); }

19 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?" };

20 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; }

21 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; }


Download ppt "Java sockets. From waiting."

Similar presentations


Ads by Google