Java Network Programming � Introduction CSCE 515: � Java Socket Programming Computer Network Programming � InetAddress � Socket ------ Java Network Programming � ServerSocket reference: Dave Hollinger Wenyuan Xu � DatagramSocket Department of Computer Science and Engineering � Multicast Socket University of South Carolina 2007 CSCE515 – Computer Network Programming Why Java? � Network Programming in Java is very different than in C/C++ � much more language support Crash Course in Java � error handling � no pointers! (garbage collection) � Threads are part of the language. � some support for common application level protocols (HTTP). 4 Netprog: Java Intro First Program: Simp.java Java notes for C++ programmers � Everything is an object. public class Simp { � No code outside of class definition! public class Simp { � Single inheritance public static void main(String args[]) { public static void main(String args[]) { � an additional kind of inheritance: interfaces System.out.println("Hello, Netprog"); System.out.println("Hello, Netprog"); � All classes are defined in .java files } } � one top level public class per file } } � To print to stdout: � System.out.println(); 5 6 Netprog: Java Intro Netprog: Java Intro
Java bytecode and interpreter Compiling and Running � bytecode is an intermediate representation of the program (class). javac Simp.java Simp.java � The Java interpreter starts up a new compile “Virtual Machine”. run source code � The VM starts executing the users class Simp.class java Simp by running it’s main() method. bytecode 7 8 Netprog: Java Intro Netprog: Java Intro Java Data Types Other Data Types not an int! � Primitive Data Types: � Reference types (composite) � boolean true or false � classes � char unicode! (16 bits) � arrays � byte signed 8 bit integer � short signed 16 bit integer � strings are supported by a built-in class signed 32 bit integer named String � int signed 64 bit integer � long � Not an array of chars! � float , double IEEE 754 floating point � string literals are supported by the language (as a special case). 9 10 Netprog: Java Intro Netprog: Java Intro Classes and Objects Defining a Class � “All Java statements appear within � One top level public class per .java file. methods, and all methods are defined � typically end up with many .java files for a within classes”. single program. � One (at least) has a static public main() � Java classes are very similar to C++ method. classes (same concepts). � Class name must match the file name! � Instead of a “standard library”, Java � compiler/interpreter use class names to figure provides a lot of Class implementations. out what file name is. 11 12 Netprog: Java Intro Netprog: Java Intro
Sample Class Objects and new (from Java in a Nutshell) You can declare a variable that can hold an public class Point { object: public double x,y; Point p; public Point(double x, double y) { but this doesn’t create the object! You this.x = x; this.y=y; have to use new: } public double distanceFromOrigin(){ Point p = new Point(3.1,2.4); return Math.sqrt(x*x+y*y); there are other ways to create objects… } } 13 14 Netprog: Java Intro Netprog: Java Intro Using objects Reference Types � Just like C++: � Objects and Arrays are reference types � object.method() � Primitive types are stored as values. � object.field � Reference type variables are stored as references (pointers that we can’t mess � BUT, never like this (no pointers!) with). � object->method() � There are significant differences! � object->field 15 16 Netprog: Java Intro Netprog: Java Intro Primitive vs. Reference Types Passing arguments to methods int x=3; � Primitive types: the method gets a copy of int y=x; the value. Changes won’t show up in the There are two copies of the value caller. 3 in memory Point p = new Point(2.3,4.2); � Reference types: the method gets a copy Point t = p; of the reference, the method accesses the same object! There is only one Point object in memory! 17 18 Netprog: Java Intro Netprog: Java Intro
Importing classes and packages Packages � Instead of #include , you use import � You can organize a bunch of classes into a package . � You don’t have to import anything, but � defines a namespace that contains all the then you need to know the complete name classes. (not just the class, the package). � You need to use some java packages in � if you import java.io.File you can use your programs File objects. � java.lang java.io, java.util � If not – you need to use java.io.File objects. 19 20 Netprog: Java Intro Netprog: Java Intro Exceptions Try/Catch/Finally � Terminology: try { // some code that can throw � throw an exception : signal that some // an exception condition (possibly an error) has occurred. } catch (ExceptionType1 e1) { � catch an exception : deal with the error (or // code to handle the exception whatever). } catch (ExceptionType2 e2) { // code to handle the exception � In Java, exception handling is necessary } finally { (forced by the compiler)! // code to run after the stuff in try // can handle other exception types } 21 22 Netprog: Java Intro Netprog: Java Intro Exceptions and Network Programming � Exceptions take care of handling errors � instead of returning an error, some method Socket Programming calls will throw an exception. � A little hard to get used to, but forces the programmer to be aware of what errors can occur and to deal with them. 23 Netprog: Java Intro
Java Sockets Programming Classes InetAddress � The package java.net provides support for sockets programming (and more). Socket � Typically you import everything defined in ServerSocket this package with: DatagramSocket DatagramPacket import java.net.*; 25 26 Netprog: Java Sockets Netprog: Java Sockets java.net.InetAddress class Sample Code: Lookup.java � static methods you can use to create new � Uses InetAddress class to lookup InetAddress objects. hostnames found on command line. public static InetAdress getByName(String host) public static InetAdress getLocalHost() � java Lookup www.yahoo.com public static InetAdress[] getAllByName(String hostName) www.yahoo.com:209.191.93.52 InetAddress x = InetAddress.getByName( “cse.sc.edu”); 27 28 Netprog: Java Sockets Netprog: Java Sockets Sample Code: getLocalhost.java try { try { � Uses InetAddress class to lookup InetAddress a = InetAddress.getByName(hostname); InetAddress a = InetAddress.getByName(hostname); localhost System.out.println(hostname + ":" + System.out.println(hostname + ":" + a.getHostAddress()); a.getHostAddress()); � java getLocalhost } catch (UnknownHostException e) { } catch (UnknownHostException e) { broad/129.252.130.139 System.out.println("No address found for " + System.out.println("No address found for " + hostname); hostname); } } 29 30 Netprog: Java Sockets Netprog: Java Sockets
Recommend
More recommend