Java Networking -- Socket A Simple Echo Server A Simple Echo ...
Recommend Documents
The term network programming refers to writing programs that execute across ...
The java.net package of the J2SE APIs contains a collection of classes and ...
Windows driver version 7.3. Table of .... This driver supports the following
versions of Windows: ... Windows Vista features a completely new audio
architecture.
0 Ignition switch to "stop" position. [Notel When engine does not stop, shil't choke shutter to close position. Check an
adaptation of call rate to the degree of habitat clutter parallels bat echolocation and suggests that shrews may use the echoes and reverbera- tions of their calls ...
rice and produced GABBA using SRI for over four years (overnight; additional fees apply). ⢠Vermiculture and mushroom farming at Mae. Jo University.
Parts Catalogue . ... Technical data subject to change without notice. ... Turn choke shutter to full RUN {open} positio
2. Contents. Package Contents ....................................... 3. Using Your Computer......
............................. 4. 1.Main Unit Setup ........................................................ 4. 1.
1Biochemistry and Biophysics, University of Pennsylvania, Philadelphia, PA, United ... in Functional MRI, edited by P. A. Bandettini (Springer, Berlin, 2000), pp.
vascular morphologic parameters from combined gradient-echo (GE) and spin-echo (SE) MR imaging ... genesis is a pathophysiologic process that has been.
Oct 17, 2011 - Imaging (EPSI) sequence to collect multiple phase encoded lines within ... (2D) image as a series of individually phase encoded free induction ...
Motivation. HTTP protocol is a client/server protocol and the foundation of the World Wide Web service. You use it daily
4/27/10. 1. Web services in Java. CS 475. 1. A Simple Example. • Echo server
example. • Java supports web services in core Java. – JAX‐WS (Java API for ...
Ying Cai. Lecture 2. CS587x. Socket Programming (C/Java). CS587x Lecture 3.
Department of Computer Science. Iowa State University. Internet Socket. Socket ...
CarGame.java package cargame; import javax.swing.*; public class CarGame {
public static void main(String[] args) {. // Create application fram and a game ...
2.7 Programming Exercises. 2.8 Beyond the Basics. This chapter applies
concepts from Chapter 1 to building simple programs in the. Java language.
You can also contact me by email: [email protected]. Thank you for ...... out . println ( "
There are numerous blogs, books and tutorials available to learn Java. A lot of ... Repetition is the key of learning an
Botrychium echo is one of four twice dissected moonwort species in the southern
Rocky Mountains. It is distinguished from B. lanceolatum by its pinnate.
Introducing the ECHO Network. The ECHO Network is a 5-year research program, funded by a Canadian Institutes of. Health
Simple Preservation of a Maxillary Extraction ... Email: george.sandor@ ... is the definitive version of this article: www.cda-adc.ca/jcda/vol-74/issue-6/523.html ...
to the call of his Maker. When the Creator says, “Seek you My face,” it is the
natural duty of the creature to reply, “Your face, Lord, will I seek.” And the more is
this ...
Echo®. Actual size: 45” wide x 26” high. (A) Intuitive User Interface. •
Touchscreen 17” monitor. • Color imaging. (B) Fluidics Module. • PBS is Echo's
only system ...
Oct 4, 2013 ... Portions of this handouts by Eric Roberts. Your job in this ... assignment page (go
to the CS106A web site and click the Assignments link). The.
Java Networking -- Socket A Simple Echo Server A Simple Echo ...
Java Networking -- Socket. Server socket class: ServerSocket wait for requests
from clients. after a request is received, a client socket is generated. Client socket
...
A Simple Echo Server Java Networking -- Socket import java.io.*; import java.net.*;
Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
Client socket class: Socket an endpoint for communication between two apps/applets. obtained by contacting a server generated by the server socket
Communication is handled by input/output streams. Socket provides an input and an output stream.
public class EchoServer { public static void main(String[] args) { try { ServerSocket s = new ServerSocket(8008); while (true) { Socket incoming = s.accept(); BufferedReader in = new BufferedReader( new InputStreamReader( incoming.getInputStream())); PrintWriter out = new PrintWriter( new OutputStreamWriter( incoming.getOutputStream()));
A Simple Echo Server (cont'd) out.println("Hello! ...."); out.println("Enter BYE to exit."); out.flush(); while (true) { String str = in.readLine(); if (str == null) { break; // client closed connection } else { out.println("Echo: " + str); out.flush(); if (str.trim().equals("BYE")) break; } } incoming.close(); } } catch (Exception e) {} } }
Test the EchoServer with Telnet Use telnet as a client. venus% telnet saturn 8008 Trying 140.192.34.63 ... Connected to saturn. Escape character is '^]'. Hello! This is the Java EchoServer. Enter BYE to exit. Hi, this is from venus Echo: Hi, this is from venus BYE Echo: BYE Connection closed by foreign host.
A Simple Client (cont'd) A Simple Client
(class EchoClient continued.) BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); PrintWriter out = new PrintWriter( new OutputStreamWriter( socket.getOutputStream())); // send data to the server for (int i = 1; i 0) { host = args[0]; } else { host = "localhost"; } Socket socket = new Socket(host, 8008);
Multi-Threaded Echo Server A Simple Client (cont'd)
To handle multiple requests simultaneously. In the main() method, spawn a thread for each request.
(class EchoClient continued.) // receive data from the server while (true) { String str = in.readLine(); if (str == null) { break; } else { System.out.println(str); } } } catch (Exception e) {}
public class MultiEchoServer { public static void main(String[] args) { try { ServerSocket s = new ServerSocket(8009); while (true) { Socket incoming = s.accept(); new ClientHandler(incoming).start(); } } catch (Exception e) {} }
} } }
Client Handler (cont'd)
Client Handler
out.println("Hello! ..."); out.println("Enter BYE to exit."); out.flush(); while (true) { String str = in.readLine(); if (str == null) { break; } else { out.println("Echo: " + str); out.flush(); if (str.trim().equals("BYE")) break; } } incoming.close(); } catch (Exception e) {}
public class ClientHandler extends Thread { protected Socket incoming; public ClientHandler(Socket incoming) { this.incoming = incoming; } public void run() { try { BufferedReader in = new BufferedReader( new InputStreamReader( incoming.getInputStream())); PrintWriter out = new PrintWriter( new OutputStreamWriter( incoming.getOutputStream()));
Visitor Counter A server and an applet. The server keeps the visitor count in a file, and sends the count to clients when requested. No need for spawning thread, since only need to transmit an integer. Read and write files.
} }
Visitor Counter Server public class CounterServer { public static void main(String[] args) { System.out.println("CounterServer started."); int i = 1; try { // read count from the file InputStream fin = new FileInputStream("Counter.dat"); DataInputStream din = new DataInputStream(fin); i = din.readInt() + 1; din.close(); } catch (IOException e) {}
Visitor Counter Server (cont'd)
Visitor Counter Server (cont'd) (class CountServer continued.)
(class CountServer continued.) OutputStream fout = new FileOutputStream("Counter.dat"); DataOutputStream dout = new DataOutputStream(fout); dout.writeInt(i); dout.close(); out.close(); i++;
try { ServerSocket s = new ServerSocket(8190); while (true) { Socket incoming = s.accept(); DataOutputStream out = new DataOutputStream( incoming.getOutputStream()); System.out.println("Count: " + i); out.writeInt(i); incoming.close();
public class Counter extends Applet { (class CountServer continued.) protected int count = 0; protected Font font = new Font("Serif", Font.BOLD, 24); public void init() { URL url = getDocumentBase(); try { Socket t = new Socket(url.getHost(), 8190); DataInputStream in = new DataInputStream(t.getInputStream()); count = in.readInt(); } catch (Exception e) {} }
public void paint(Graphics g) { int x = 0, y = font.getSize(); g.setColor(Color.green); g.setFont(font); g.drawString("You are visitor: " + count, x, y); } }
Broadcast Echo Server To handle multiple requests simultaneously. Broadcast messages received from any client to all active clients. Need to keep track of all active clients.
Broadcast Echo Server (cont'd) public class BroadcastEchoServer { static protected Set activeClients = new HashSet(); public static void main(String[] args) { int i = 1; try { ServerSocket s = new ServerSocket(8010); while (true) { Socket incoming = s.accept(); BroadcastClientHandler newClient = new BroadcastClientHandler(incoming, i++); activeClients.add(newClient); newClient.start(); } } catch (Exception e) {} } }
Broadcast Client Handler (cont'd)
Broadcast Client Handler public class BroadcastClientHandler extends Thread { protected protected protected protected
Socket incoming; int id; BufferedReader in; PrintWriter out;
public synchronized void sendMessage(String msg) { if (out != null) { out.println(msg); out.flush(); } }
public BroadcastClientHandler(Socket incoming, int id) { this.incoming = incoming; this.id = id; try { if (incoming != null) { in = new BufferedReader( new InputStreamReader( incoming.getInputStream())); out = new PrintWriter( new OutputStreamWriter( incoming.getOutputStream())); } } catch (Exception e) {} }
Broadcast Client Handler (cont'd) public void run() { if (in != null && out != null) { sendMessage("Hello! ..."); sendMessage("Enter BYE to exit."); try { while (true) { String str = in.readLine(); if (str == null) { break; } else { // echo back to this client sendMessage("Echo: " + str ); if (str.trim().equals("BYE")) { break; } else {
Broadcast Client Handler (cont'd) // broadcast to other active clients Iterator iter = BroadcastEchoServer.activeClients.iterator(); while (iter.hasNext()) { BroadcastClientHandler t = (BroadcastClientHandler) iter.next(); if (t != this) t.sendMessage("Broadcast(" + id + "): " + str); }}}} incoming.close(); // this client is no longer active BroadcastEchoServer.activeClients.remove(this); } catch (IOException e) {} } } }