Transcription of First Program: Simp - Computer Science & Engineering
1 Java Network programming Introduction CSCE 515: Computer Network Java Socket programming programming InetAddress ------ Java Network programming Socket reference: Dave Hollinger ServerSocket Wenyuan Xu DatagramSocket Department of Computer Science and Engineering University of South Carolina Multicast Socket 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 Java notes for C++ programmers First Program: Everything is an object.
2 No code outside of class definition! public public class class Simp Simp {{. Single inheritance public public static static void void main(String main(String args[]). args[]) {{. an additional kind of inheritance: interfaces ("Hello, Netprog");. ("Hello, Netprog");. All classes are defined in .java files }}. one top level public class per file }}. To print to stdout: ();. 5 6. Netprog: Java Intro Netprog: Java Intro Compiling and Running Java bytecode and interpreter bytecode is an intermediate representation of the program (class). javac compile The Java interpreter starts up a new source code run Virtual Machine . The VM starts executing the users class java Simp by running it's main() method.
3 Bytecode 7 8. Netprog: Java Intro Netprog: Java Intro Java Data Types Other Data Types an int! Primitive Data Types: not 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 int signed 32 bit integer named String long signed 64 bit integer 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 typicallyend up with many.
4 Java files for a within classes . single program. Java classes are very similar to C++ One (at least) has a static public main(). classes (same concepts). method. Instead of a standard library , Java Class name must match the file name! provides a lot of Class implementations. compiler/interpreter use class names to figure out what file name is. 11 12. Netprog: Java Intro Netprog: Java Intro Sample Class Objects and new (from Java in a Nutshell). public class Point {. You can declare a variable that can hold an public double x,y; object: public Point(double x, double y) { Point p;. = x; ; but this doesn't create the object! You } have to use new: public double distanceFromOrigin(){ Point p = new Point( , ).}}
5 Return (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 () Primitive types are stored as values. 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;. There are two copies of the value the value. Changes won't show up in the 3 in memory caller.
6 Point p = new Point( , ); Reference types: the method gets a copy Point t = p; of the reference, the method accesses the There is only one Point object in same object! memory! 17 18. Netprog: Java Intro Netprog: Java Intro Packages Importing classes and packages You can organize a bunch of classes into Instead of #include, you use import 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 you can use your programs File objects. , If not you need to use objects. 19 20. Netprog: Java Intro Netprog: Java Intro Exceptions Try/Catch/Finally Terminology: try {.}
7 Throw an exception: signal that some // some code that can throw condition (possibly an error) has occurred. // an exception catch an exception: deal with the error (or } catch (ExceptionType1 e1) {. whatever). // code to handle the exception } 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 calls will throw an exception. Socket programming A little hard to get used to, but forces the programmer to be aware of what errors can occur and to deal with them.
8 23. Netprog: Java Intro Java sockets programming Classes The package provides support for InetAddress sockets programming (and more). Socket Typically you import everything defined in ServerSocket this package with: DatagramSocket DatagramPacket import *;. 25 26. Netprog: Java sockets Netprog: Java sockets class Sample Code: 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 public static InetAdress[] getAllByName(String hostName). InetAddress x = (. );. 27 28. Netprog: Java sockets Netprog: Java sockets Sample Code: try try {{.}}
9 Uses InetAddress class to lookup InetAddress InetAddress aa == (hostname);. (hostname); localhost (hostname (hostname ++ ":". ":" ++. ());. ()); java getLocalhost }} catch catch (UnknownHostException (UnknownHostException e). e) {{. ("No ("No address address found found for for "" ++. hostname);. hostname);. }}. 29 30. Netprog: Java sockets Netprog: Java sockets getLocalhost() getAllByName(). try try {{. InetAddress InetAddress address address ==. ();. (); try try {{. InetAddress[]. InetAddress[] addresses addresses ==. (address);. (address); (" ");. (" ");. for for (int (int ii == 0;. 0; ii << ;. ; i++). i++). }} catch catch (UnknownHostException (UnknownHostException e). e) {{ {{ (addresses[i]).}}}}
10 (addresses[i]); }}. ("Could ("Could not not find find this this }} catch catch (UnknownHostException (UnknownHostException e) e) {{. Computer 's Computer 's address.");. address."); }} ("Could ("Could not not find find ");. ");. }} }}. 31. Netprog: Java sockets 2007 CSCE515 Computer Network programming How to open a socket? Client Socket Constructors Constructor creates a TCP connection to a Socket class: corresponds to active TCP sockets named TCP server. only! There are a number of constructors: client sockets Socket(InetAddress server, int port);. socket returned by accept();. Socket(InetAddress server, int port, Passive sockets are supported by a different InetAddress local, int localport).