operating system

Topics: Java, Source code, Programming language Pages: 8 (1156 words) Published: April 2, 2014
CS 2203: Operating Systems
Centre for Advance Studies in Engineering (CASE)
Hand-out 8: Lab 08 Instructions

March 2014
Computer Science Department
Instructor: Shehreyar Rashid

Lab 8: Introduction to Java and Sockets
Objectives




Write, compile, and execute Java programs in Linux using command line. Use an IDE to write, compile, and execute Java programs in Linux. Learn to write client-server application using Sockets.

Credits: This lab uses material from Silberschatz, Galvin, Gagne Operating System Concepts, 8th Edition (2011, Wiley).

Java Program Using Command Line
In this section, we learn to write, compile, and execute a Java program in Ubuntu (Linux). We have download JDK and Netbeans in to lab computers. The lab instructor will help you to know the locations for the JDK, and Netbeans IDE to download and work. 1.

2.
3.
4.

Open a terminal and go to the directory containing JDK.
tar xvzf (decompress the file into a folder).
mv jdk1.7.0 13 ~/jdk1.7 (move the folder into your home directory). gedit ~/.bashrc (bashrc file contains verious commands and environment variables for the shell).
5. Type in the following command at the end of the file:
export JAVA_HOME=~/jdk1.7/bin/java
export PATH=$PATH:~/jdk1.7/bin
6. Save and exit from gedit.
7. source ~/.bashrc (make sure the newly declared environment variables are available for the current terminal).
8. Now create a File named \Hello.java" and type in the following code: public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
9. Save and exit from gedit.
10. javac Hello.java (compile a java source code into a byte code). 11. java Hello (execute a java program).
12. Task! Now modify your program and print “Hello World!” 10 times using a for loop. 13. Task! Now create another java program which prints n!.

CS 2203: Operating Systems
Centre for Advance Studies in Engineering (CASE)
Hand-out 8: Lab 08 Instructions

March 2014
Computer Science Department
Instructor: Shehreyar Rashid

Java Program Using Netbeans IDE
Now in this section, we write the same “Hello World" program using Netbeans IDE. The lab instructor will help you to download and install Netbeans IDE. 1. Create a new Java project named “Hello World" in Netbeans IDE. 2. You need to find HelloWorld.java file under SourcePakcage. 3. Modify the source code to print “HelloWorld!" 10 times using for loop. 4. To compile and execute you need to play a green color play button on top bar. The IDE compile and run the Java program in one click.

Socket Programming
In this section, we practice to create a client-server application using Sockets. 1. Create a new Java project named “SocketExample1" in Netbeans IDE. 2. Create a new Java class named “SimpleClient" and type in the following code: import java.io.*;

import java.net.*;
public class SimpleClient
{
public static void main(String[] args) throws IOException
{
Socket clientSocket = null;
PrintWriter out = null;
try
{
clientSocket = new Socket("localhost", 4444);
out = new PrintWriter(clientSocket.getOutputStream(), true); }catch (Exception e) {
System.err.println("Exception!");
e.printStackTrace();
System.exit(1);
}
String userInput = "Hello From Client";
out.println(userInput);
System.out.println("Message Sent to Server: "+ userInput);
out.close();
clientSocket.close();
}
}
3. Create a new Java class named \SimpleServer" and type in the following code: import java.net.*;
import java.io.*;
public class SimpleServer

CS 2203: Operating Systems
Centre for Advance Studies in Engineering (CASE)
Hand-out 8: Lab 08 Instructions

March 2014
Computer Science Department
Instructor: Shehreyar Rashid

{

4.
5.
6.
7.
8.

public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
Socket clientSocket = null;
try
{
serverSocket = new ServerSocket(4444);
System.out.println("Server is UP!");...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essential Properties of Operating Systems Essay
  • operating system Essay
  • Essay on Operating System
  • Essay about Operating Systems
  • Essay about Operating Systems
  • Essay about Operating System
  • Computer Operating System Essay
  • Operating System and Class Scheduling Essay

Become a StudyMode Member

Sign Up - It's Free