1 / 16

Network Programming in Java

Network Programming in Java. 1.0. source files: *.java Java source files contain the source code in readable form, as typed in by the programmer. Standard Java files.

bond
Download Presentation

Network Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Network Programming in Java 1.0

  2. source files: *.javaJava source files contain the source code in readable form, as typed in by the programmer. Standard Java files • class files: *.classJava class files contain byte code (a machine readable version of the class). They are generated by the compiler from the source file.

  3. The edit-compile-execute cycle 011010 110101 1001 10 source file class file 011010 110101 010001 1 1 1 0111 0110110 compiler (javac) virtual machine(java) editor

  4. A file can be edited in any text editor Notepad, emacs, PFE, ... careful with using Word: by default, Word does not save in text format Make sure to save before compiling! Editing

  5. compilation and execution of Java in JDK are done from a command line On Microsoft systems: DOS shell On Unix: Unix shell Must make sure that the commands for compiler and runtime are in the command path. Command line invocation

  6. Name of the JDK compiler: javac To invoke:javac <source name> compiles <source name> and all classes it depends on Example:cd C:\bluej\zuuljavac Game.java Compiling

  7. C:\bluej\zuul> javac Game.java Game.java:22: ';' expected. private Parser parser ^ 1 error C:\bluej\zuul> Error messages The programmer has to open the file in the editor, find the line number, fix the error and recompile.

  8. C:\bluej\zuul> java Game “java” starts the Java virtual machine. The named class is loaded and execution is started. Other classes are loaded as needed. Only possible if class has been compiled. Execution

  9. If we try:C:\bluej\zuul> java Game.javaException in thread "main" java.lang.NoSuchMethodError: main The problem: how does the system know which method to execute? Problem: Execute what?

  10. The answer: The java system always executes a method called main with a certain signature:public static void main(String[] args){ ...} For this to work, such a method must exist! The main method

  11. “main” must exist “main” must be public “main” must be static (class method) “main” must have a String array parameter Only “main” can be invoked The main method (2)

  12. Main method - example • The main method should • create an object • call the first method public static void main(String[] args) { Game game = new Game(); game.play(); }

  13. Java platform • Comprises of various packages, each containing related classes • Some key packages are • java.util • java.io • java.lang • java.net • …

  14. Java Network Programming • Main package is java.net • Contains various classes including • Socket • DatagramPacket • URLConnection • HTTPURLConnection • ...

  15. Socket class • Socket s = new Socket(hostname, port); • Java abstraction of network socket • Used to connect to machine/ip address with specified port number • Depending on port number you connect on, you need to send more information

  16. ServerSocket class • ServerSocket srv = new ServerSocket(port) • Used to create a Server socket • Key method here is accept() of type Socket • Makes server wait for connection and opens socket on valid request

More Related