CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.

Slides:



Advertisements
Similar presentations
Socket UDP H. Fauconnier 1-1 M2-Internet Java. UDP H. Fauconnier M2-Internet Java 2.
Advertisements

UDP Datagrams and Sockets Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
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.
Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Java Sockets Source:
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Internetworking (Contd) Chapter 4. Figure 3.26 ATM protocol layers.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Networking in Java Representation and Management of Data on the Internet.
Building an ftp client and server using sockets we now know enough to build a sophisticated client/server application!  ftp  telnet  smtp  http.
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Multicast Sockets What is a multicast socket?
CS451 - Lecture 4 1 CS451 Lecture 4: Network Programming Yugi Lee STB #555 (816) * Acknowledgement:
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
VIII. UDP Datagrams and Sockets. The User Datagram Protocol (UDP) is an alternative protocol for sending data over IP that is very quick, but not reliable:
Socket Programming Lee, Sooyong
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
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.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Java Sockets Programming
Java Socket programming. Socket programming with TCP.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
What is a Network? Computer network Computer network a set of computers using common protocols to communicate over connecting transmission media. a set.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
Slides for Chapter 4: Interprocess Communication From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 4, © Addison-Wesley.
Sockets For Clients Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
Networking Java (2/3)
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
1 Netprog 2002 TCP/IP UDP/IP in Java Based on Java Network Programming and Distributed Computing.
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.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel.
1 Socket-based Client- Server Application Client-Server application architecture Choosing services – Connectionless atau Connection-oriented.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
UDP Programming. Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007 2/86 Overview.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Network Programming Communication between processes Many approaches:
Java.net CS-328 Dick Steflik.
Object-Orientated Analysis, Design and Programming
Socket-based Client-Server Application
NETWORK PROGRAMMING CNET 441
Network Programming Introduction
Network Programming Introduction
UNIT-6.
CSCD 330 Network Programming
CSCD 330 Network Programming
NETWORK PROGRAMMING CNET 441
Java Socket Programming
Chapter 2: Application layer
Programming TCP Sockets
Based on Java Network Programming and Distributed Computing
Presentation transcript:

CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina

1/25/20052 More on Project 1 Both username and password are case- sensitive Server sends acknowledgment to client no matter the received username is good or not Make your program compilable on Unix/Linux machines Multithreaded server will get one bonus point Plagiarism will not be tolerated!

1/25/20053 UDP Sockets in Java Use class DatagramPacket and class DatagramSocket Both client and server use DatagramSocket to send and receive DatagramPacket

1/25/20054 Class DatagramPacket Constructors DatagramPacket(byte buffer[], int length) DatagramPacket(byte buffer[], int length, InetAddress address, int port) DatagramPacket(byte buffer[], int offset, int length) DatagramPacket(byte buffer[], int offset, int length, InetAddress address, int port)

1/25/20055 Class DatagramPacket Methods void setAddress(InetAddress address) InetAddress getAddress() void setPort(int port) int getPort() void setData(byte[] buffer) byte[] getData() void setLength(int length) int getLength() int getOffset()

1/25/20056 Class DatagramSocket Constructors DatagramSocket() throws SocketException DatagramSocket(int port) throws SocketException DatagramSocket(int port, InetAddress local) throws SocketException

1/25/20057 Class DatagramSocket Methods void close() throws IOException void connect(InetAddress address, int port) throws SocketException void disconnect() void send(DatagramPacket packet) throws IOException void receive(DatagramPacket packet) throws IOException

1/25/20058 Class DatagramSocket Methods InetAddress getInetAddress() int getPort() InetAddress getLocalAddress() int getLocalPort() void setSoTimeout(int timeout) throws SocketException int getSoTimeout() throws SocketException void setSendBufferSize(int size) throws SocketException int getSendBufferSize() throws SocketException void setReceiveBufferSize(int size) throws SocketException int getReceiveBufferSize() throws SocketException

1/25/20059 Class DatagramSocket Exceptions IOException SecurityException

1/25/ Receiving UDP Packets DatagramSocket socket = new DatagramSocket(port); byte buffer[] = new byte[65508]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); InetAddress fromAddress = packet.getAddress(); int fromPort = packet.getPort(); int length = packet.getLength(); byte[] data = packet.getData(); // … socket.close();

1/25/ Sending UDP Packets DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket (data, data.length, InetAddress.getByName(“ 1728); socket.send(packet); socket.close();

1/25/ A UDP Echo Client Example Send echo string to server Block on receive() for up to 3 seconds Retry for up to 5 times if reply is not received before timeout Terminate the client

1/25/ UDPEchoClientTimeout.java /* * TCP/IP Sockets in Java * Kenneth Calvert, Michael Donahoo * Morgan Kaufmann Publishers; ISBN * * de.html* * Copyright (c) 2002 Kenneth Calvert, Michael Donahoo ; * all rights reserved; see license.txt for details. */ import java.net.*; import java.io.*; public class UDPEchoClientTimeout { private static final int TIMEOUT = 3000; // Resend timeout private static final int MAXTRIES = 5; // Maximum retransmissions // public static void main (String args[]) … }

1/25/ Method main public static void main(String[] args) throws IOException { if ((args.length 3)) // Test for correct # of args throw new IllegalArgumentException("Parameter(s): [ ]"); InetAddress serverAddress = InetAddress.getByName(args[0]); // Server address // Convert input String to bytes using the default character encoding byte[] bytesToSend = args[1].getBytes(); int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7; DatagramSocket socket = new DatagramSocket(); socket.setSoTimeout(TIMEOUT); // Maximum receive blocking time (milliseconds) DatagramPacket sendPacket = new DatagramPacket(bytesToSend, // Sending packet bytesToSend.length, serverAddress, servPort); DatagramPacket receivePacket = // Receiving packet new DatagramPacket(new byte[bytesToSend.length], bytesToSend.length); …

1/25/ Method main (cont’d) int tries = 0; // Packets may be lost, so we have to keep trying boolean receivedResponse = false; do { socket.send(sendPacket); // Send the echo string try { socket.receive(receivePacket); // Attempt echo reply reception if (!receivePacket.getAddress().equals(serverAddress)) // Check source throw new IOException("Received packet from an unknown source"); receivedResponse = true; } catch (InterruptedIOException e) { // We did not get anything tries += 1; System.out.println("Timed out, " + (MAXTRIES-tries) + " more tries..."); } } while ((!receivedResponse) && (tries < MAXTRIES)); if (receivedResponse) System.out.println("Received: " + new String(receivePacket.getData())); else System.out.println("No response -- giving up."); socket.close(); }

1/25/ A UDP Echo Server Example Construct a DatagramSocket specifying local port and (optionally) local address Communicate with client by receiving and sending DatagramPackets Terminate server when finished

1/25/ UDPEchoServer.java /* * TCP/IP Sockets in Java * Kenneth Calvert, Michael Donahoo * Morgan Kaufmann Publishers; ISBN * * de.html* * Copyright (c) 2002 Kenneth Calvert, Michael Donahoo ; * all rights reserved; see license.txt for details. */ import java.net.*; import java.io.*; public class UDPEchoServer { private static final int ECHOMAX = 255; // Maximum size of echo datagram // public static void main (String args[]) … }

1/25/ Method main public static void main(String[] args) throws IOException { if (args.length != 1) // Test for correct argument list throw new IllegalArgumentException("Parameter(s): "); int servPort = Integer.parseInt(args[0]); DatagramSocket socket = new DatagramSocket(servPort); DatagramPacket packet = new DatagramPacket(new byte[ECHOMAX], ECHOMAX); for (;;) { // Run forever, receiving and echoing datagrams socket.receive(packet); // Receive packet from client System.out.println("Handling client at " + packet.getAddress().getHostAddress() + " on port " + packet.getPort()); socket.send(packet); // Send the same packet back to client packet.setLength(ECHOMAX); // Reset length to avoid shrinking buffer } /* NOT REACHED */ }

1/25/ Blocking Program cannot move forward until request for data is satisfied Many calls may block accept() read(), receive() write()

1/25/ Dealing with Blocking Calls Write a non-blocking server Use available() Use setSoTimeout() Write a multithreaded server

1/25/ A Non-blocking Server Example Accept two client connections Loop to echo data between two clients Use available() to simulate non-blocking

1/25/ NBServer.java /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.net.*; import java.io.*; public class NBServer { static InputStream in0, in1; static OutputStream out0, out1; // public static void main (String[] args) throws IOException … }

1/25/ Method main public static void main (String[] args) throws IOException { if (args.length != 1) throw new IllegalArgumentException ("Syntax: NBServer "); try { accept (Integer.parseInt (args[0])); int x0, x1; while (((x0 = readNB (in0)) != -1) && ((x1 = readNB (in1)) != -1)) { if (x0 >= 0) out1.write (x0); if (x1 >= 0) out0.write (x1); } } finally { System.out.println ("Closing"); close (out0); close (out1); }

1/25/ Method close static void close (OutputStream out) { if (out != null) { try { out.close (); } catch (IOException ignored) { } // static void accept (int port) throws IOException … // static int readNB (InputStream in) …

1/25/ Method accept static void accept (int port) throws IOException { System.out.println ("Starting on port " + port); ServerSocket server = new ServerSocket (port); try { System.out.println ("Waiting.."); Socket client0 = server.accept (); System.out.println ("Accepted from " + client0.getInetAddress ()); in0 = client0.getInputStream (); out0 = client0.getOutputStream (); out0.write ("Welcome. Please wait.\r\n".getBytes ("latin1")); System.out.println ("Waiting.."); Socket client1 = server.accept (); System.out.println ("Accepted from " + client1.getInetAddress ()); in1 = client1.getInputStream (); out1 = client1.getOutputStream (); out1.write ("Welcome.\r\n".getBytes ("latin1")); out0.write ("Proceed.\r\n".getBytes ("latin1")); } finally { server.close (); }

1/25/ Method readNB static int readNB (InputStream in) throws IOException { if (in.available () > 0) return in.read (); else return -2; }

1/25/ Use Socket Timeout client0.setsoTimeout (10); client1.setSoTimeout (10); static int readNB (InputStream in) throws IOException { try { return in.read (); } catch (InterruptedIOException ex) { return -2; }

1/25/ Next Class Multithreading Link layer Read JNP Ch. 16, TI Ch. 2