Presentation is loading. Please wait.

Presentation is loading. Please wait.

Misc. Java Alittle more network programming & Using jar files to share your work.

Similar presentations


Presentation on theme: "Misc. Java Alittle more network programming & Using jar files to share your work."— Presentation transcript:

1 Misc. Java Alittle more network programming & Using jar files to share your work

2 Socket: Java Network Connection Analogous to an electrical socket, the client can plug into the server This creates a connection along which information can flow When the client disconnects, the socket is free for the next client to use Input and output streams can be created to allow communication over the socket. But I want to experiment at home…

3 You know an IP number

4 Date Server Example (unchanged) import java.util.Date; import java.net.*; import java.io.*; public class DateServer { static final public int portNumber = 4291; public static void main(String [] args) { try { DateServer world = new DateServer(); } catch (IOException e) { System.out.println("IO exception " + e); } // end try-catch } // end main public DateServer ( ) throws IOException { ServerSocket server = new ServerSocket(portNumber); for (int i = 0; i < 3; i++) { System.out.println("Waiting for a client"); Socket sock = server.accept(); System.out.println("Got client #" + i); OutputStreamWriter out = new OutputStreamWriter(sock.getOutputStream()); String message = "Current date and time is " + new Date() + "\n"; out.write(message); out.close(); } // end for } // end DateServer } // end class DateServer

5 Date Client Example import java.net.*; import java.io.*; public class DateClient { static final public int portNumber = 4291; public static void main(String [] args) { try { DateClient world = new DateClient(); } catch (IOException e) { System.out.println("Received an IO exception " + e); } // end try-catch } // end main public DateClient ( ) throws IOException { // Socket sock = new Socket(InetAddress.getLocalHost(), portNumber); byte [] ipAddr = {(byte) 134, (byte) 161, (byte) 243, (byte) 240}; Socket sock = new Socket(InetAddress.getByAddress(ipAddr), portNumber); // Socket sock = new Socket(InetAddress.getByName("fienup2.cs.uni.edu"), portNumber); Reader isread = new InputStreamReader(sock.getInputStream()); BufferedReader input = new BufferedReader(isread); System.out.println("message is " + input.readLine()); } // end DateClient } // end class DateClient

6 Possible Limitations/Problems Your internet service provider might only give you one IP address. That you “share” between machines. The IP address might vary, i.e., assigned dynamically each time you connect.

7 Making a Double-Clickable.jar File Some of you have written Frogger games of which you are quite proud. You want to share your handiwork with your friends, family, potential employers. How you can you make a double-clickable application from your Java program? Many industry-grade development environments include tools for creating double-clickable apps. But we are not using one. Many of these professional tools are quite complex. Learning them just for the sake of creating a double-clickable app seems crazy. Without making a double-clickable app, your program will be hard or impossible for many others to use. Therefore, use the Java Development Kit's jar tool to create a double- clickable Java archive file. jar packages a bunch of files into a single archive file that you can give to another or offer for download on the web. –If we tell the archive which class file contains the main() method, then users will be able to double-click on the file's icon and run it like a standalone application. –The file that tells the archive this information is called a manifest file. –The manifest acts like a "read me" file for the Java virtual machine can read. When we run jar, it automatically creates a manifest file for the archive. Unfortunately, the default manifest file does not include a definition of the class that contains the main() method.

8 Making a Double-Clickable.jar File Here are the steps you can use to create a double-clickable jar file. I'll illustrate them by creating the double-clickable cannonworld.jar. Create a manifest file, manifest.mf for your application and place it in the directory with *.class files Manifest-Version: 1.0 Created-By: 1.5.0_06 Main-Class: CannonGameDriver Go to your Unix shell (or perhaps Windows DOS window). Change into the directory containing your *.class files and manifest.mf. Use jar to create your archive. The command you need is: jar cfm jarFileName manifest.mf *.class where jarFileName is what you want to call your archive. To create my archive, I entered: jar cfm cannonworld.jar manifest.mf *.class Move your jar file to whatever location you desire. It's also fun to make your own icon for the double-clickable file. That's another one of those things that industrial-strength IDEs offer do for us. jar does not. You can read more about jar by typing "man jar" at your Unix prompt, or by visiting the JDK documentation.the JDK documentation


Download ppt "Misc. Java Alittle more network programming & Using jar files to share your work."

Similar presentations


Ads by Google