CS 4244: Internet Programming Network Programming in Java 1.0.

16
CS 4244: Internet Programming Network Programming in Java 1.0

Transcript of CS 4244: Internet Programming Network Programming in Java 1.0.

Page 1: CS 4244: Internet Programming Network Programming in Java 1.0.

CS 4244: Internet Programming

Network Programming in Java

1.0

Page 2: CS 4244: Internet Programming Network Programming in Java 1.0.

Standard Java files

source files: *.javaJava source files contain the source code in readable form, as typed in by the programmer.

source files: *.javaJava source files contain the source code in readable form, as typed in by the programmer.

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.

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.

Page 3: CS 4244: Internet Programming Network Programming in Java 1.0.

source file

011010

110101

010001

class file011010

110101

1001

10

1

0111

0110110

1

1

editorcompiler(javac)

virtual machine(java)

The edit-compile-execute cycle

Page 4: CS 4244: Internet Programming Network Programming in Java 1.0.

Editing

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!

Page 5: CS 4244: Internet Programming Network Programming in Java 1.0.

Command line invocation

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.

Page 6: CS 4244: Internet Programming Network Programming in Java 1.0.

Compiling

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

Page 7: CS 4244: Internet Programming Network Programming in Java 1.0.

Error messages

C:\bluej\zuul> javac Game.java

Game.java:22: ';' expected.

private Parser parser

^

1 error

C:\bluej\zuul>

The programmer has to open the file in the editor, find the line number, fix the error and recompile.

Page 8: CS 4244: Internet Programming Network Programming in Java 1.0.

Execution

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.

Page 9: CS 4244: Internet Programming Network Programming in Java 1.0.

Problem: Execute what?

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?

Page 10: CS 4244: Internet Programming Network Programming in Java 1.0.

The main method

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!

Page 11: CS 4244: Internet Programming Network Programming in Java 1.0.

The main method (2)

“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

Page 12: CS 4244: Internet Programming Network Programming in Java 1.0.

Main method - example

public static void main(String[] args){ Game game = new Game(); game.play();}

The main method should create an object call the first method

Page 13: CS 4244: Internet Programming Network Programming in Java 1.0.

Java platform

Comprises of various packages, each containing related classes

Some key packages are java.util java.io java.lang java.net …

Page 14: CS 4244: Internet Programming Network Programming in Java 1.0.

Java Network Programming

Main package is java.net Contains various classes including

Socket DatagramPacket URLConnection HTTPURLConnection ...

Page 15: CS 4244: Internet Programming Network Programming in Java 1.0.

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

Page 16: CS 4244: Internet Programming Network Programming in Java 1.0.

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