UDP&DistributedComputing

UDP&DistributedComputing - CS 580 Client-Server...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 580 Client-Server Programming Spring Semester, 2010 April 20, 2010 Copyright ©, All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http:// www.opencontent.org/opl.shtml ) license defines the copyright on this document.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
References 2 Java Networking Programming, 3rd Ed., Harold, O'Reilly, 2005, Chapters 13 JSON - JavaScript Object Notation, Main JSON web site, http://www.json.org/ JSON in Java, http://www.json.org/java/index.html Java's Object Streams Java 6 API documentation, http://java.sun.com/javase/6/docs/api/ Object Stream Spec, http://java.sun.com/javase/6/docs/platform/serialization/spec/protocol.html
Background image of page 2
3 UDP
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
UDP Client 4 import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPClient { public static void main(String[] args) throws IOException { InetAddress serverAddress = InetAddress.getByName("localhost"); DatagramSocket socket = new DatagramSocket(); socket.connect(serverAddress, 8888); byte[] message = "Hello World".getBytes(); DatagramPacket output = new DatagramPacket(message, message.length); socket.send(output); byte[] buffer = new byte[65507]; DatagramPacket response = new DatagramPacket(buffer, buffer.length); socket.receive(response); String answer = new String(response.getData(), 0, response.getLength()); System.out.println("cleint" + answer); } }
Background image of page 4
UDPEchoServer 5 import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class UDPEchoServer extends Thread { private int bufferSize = 8192; protected DatagramSocket socket; public UDPEchoServer(int port) throws SocketException { this.socket = new DatagramSocket(port); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
UDPEchoServer 6 public void run() { byte[] buffer = new byte[bufferSize]; while (true) { DatagramPacket incoming = new DatagramPacket(buffer, buffer.length); try { socket.receive(incoming); byte[] data = incoming.getData(); System.out.println("Server " + new String(data)); DatagramPacket outgoing = new DatagramPacket( incoming.getData(), incoming.getLength(), incoming .getAddress(), incoming.getPort()); socket.send(outgoing); } catch (IOException e) { System.err.println(e); } } }
Background image of page 6
Packet Size 7 IPv4 protocol supports UDP packets up to 65,507 bytes In practice 8,192 bytes is limit Larger packets may be trucated split dropped
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Connectionless 8 DatagramSocket socket = new DatagramSocket(); socket.connect(serverAddress, 8888); byte[] message = "Hello World".getBytes(); DatagramPacket output = new DatagramPacket(message, message.length); socket.send(output); connect method restricts socket to sending only to one machine and port. However after using the disconnect method the socket can be used to connect to any machine or port.
Background image of page 8
Connectionless 9 DatagramSocket socket = new DatagramSocket(); byte[] message = "Hello World".getBytes(); DatagramPacket output = new DatagramPacket(message, message.length, serverAddress, 8888); socket.send(output); One datagram socket can send to multiple addresses
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Distributed Computing
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/16/2011 for the course CS 580 taught by Professor Rogerwhitney during the Spring '11 term at Community college of RI.

Page1 / 43

UDP&DistributedComputing - CS 580 Client-Server...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online