Java Notes

Here are some of my notes about programming in Java that may help you.

To write a java program you can use any text editor, but the name of the file must be *.java (for example myprogram.java) and the name of the first class in the file must match the file name.

Here’s an example..

filename : myprogram.java

contents:

public class myprogram

{

public static void main(String[] args)

{}

}

 

Now to get the program to compile you have to have installed a java compiler like eclipse or whatever and then you can run the command “javac myprogram.java” in the directory of the file. This will compile the program into a file called myprogram.class. You can execute the file with the command “java myprogram”. If you try “java myprogram.class” you will get the error message Error: Could not find or load main class myprogram.class”.

import java.net.*;
import java.io.*;
import java.util.*;
public class ServerProgram
{

public static void main(String[] args)
{
int port = 1234;
try
{
System.out.println(“Hi”);
ServerSocket ss = new ServerSocket(port);
System.out.println(“Server Socket open on port ” + port);
while (true)
{
Socket s = ss.accept();
System.out.println(“Accepting connections on port ” + port);
Thread t = new Thread(new MyThread(s));
t.start();
}

}
catch (Exception e)
{
System.out.println(“failed to created server socket, port probably in use.”);
}
}
}

class MyThread implements Runnable
{
private Socket s;

public MyThread(Socket socket)
{
s = socket;
}

public void run()
{
try
{
System.out.println(“Connection Established”);
Scanner MyScannerObject = new Scanner(s.getInputStream());
PrintWriter myout = new PrintWriter(s.getOutputStream(),true);

while (true)
{
String myinput = MyScannerObject.nextLine();

if (myinput.equalsIgnoreCase(“exit”)) break;
else if (myinput.equalsIgnoreCase(“get”))
{
myout.println(“This is the Server Speaking. Sending you my love as a retrieved message.”);
}
else
{
System.out.println(myinput);
myout.println(“The server did not understand the command. Please try again.”);
}
}
}
catch (Exception e)
{
System.out.println(“Failed to send connect scanners and writers.”);
}

try
{
s.close();
System.out.println(“Socket is now closed”);
}
catch (Exception e)
{
System.out.println(“Could Not Close Connection”);
}
}
}

import java.net.*;
import java.util.*;
import java.io.*;

public class ClientProgram
{
public static void main(String[] args)
{
int port = 1234;
System.out.println(“Client Program 1.0”);
Socket s = getSocket(port);//this creates a socket obj. called s using the
//previously defined port variable
System.out.println(“Connected on port ” + port);
try{
Scanner in = new Scanner(s.getInputStream());
PrintWriter myout;
myout = new PrintWriter(s.getOutputStream(),true);
myout.println(“get”);

String RetrievedMessage = in.nextLine();//stores the retrieved message in a variable
System.out.println(RetrievedMessage);//displays the retrieved message from that variable

//myout.println(“This is the client signing out. Good bye.”);
}
catch (IOException e)
{
System.out.println(“IO exception”);
}

try
{
s.close();//closes the socket
}
catch (Exception e)
{
System.out.println(“Failed to close the socket.”);
e.printStackTrace();
}

}

private static Socket getSocket(int port)
{
Socket s;
String host;
InetAddress ip;

Scanner sc = new Scanner(System.in);
while (true)
{
System.out.print(“What server do you want to connect to?”);
host = sc.nextLine();

try
{
ip = InetAddress.getByName(host);
s = new Socket(ip,port);
return s;
}

catch (UnknownHostException e)
{
System.out.println(“Unknown host.”);
}

catch (IOException e)
{
System.out.println(“network IO error…”);
}
}
}
}

Leave a Reply