Hi,
I would highly appreciate if anyone can answer following questions regarding the Following code:
Also please note that the Driver (containing main()) is also included after this class.
1. Where should the getLine() method BODY go , so that if only one instance of SocketServer is instantiated i can access the accessor (get Methods) on it.
2. Should I move it INSIDE the default Constructor ? if not Why Not ?
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;
public class SocketServer extends
Thread {
// Private reference to the one and only instance called uniqueInstance
private static SocketServer uniqueInstance = null;
//Instance attributes
private Socket Client;
private
String FileName;
public static SocketServer instance() {
if (uniqueInstance == null)
uniqueInstance = new SocketServer();
return uniqueInstance;
}
private SocketServer() {
}
public String getLine() {
//This part Needs to go to the Singleton Class, Since THAT is where this work is done, and if there is
//ONE Client requesting, the file, then the Singleton ONLY Sends out one line at a time,
// And if there are 2 Clients , then the Singleton Gives out 1 line per each, hence at a half rate
// to each Client
try {
DataOutputStream send =
new DataOutputStream(Client.getOutputStream());
BufferedReader in = new BufferedReader(new FileReader(FileName));
String str;
while ((str = in.readLine()) != null) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Exception" + e);
}
send.writeBytes(str + "\n");
}
in.close();
} catch (IOException e) {
}
System.out.println("Client Disconnected!");
return null;
}
/**
* @return
*/
public Socket getClient() {
return Client;
}
/**
* @return
*/
public String getFileName() {
return FileName;
}
/**
* @param socket
*/
public void setClient(Socket socket) {
Client = socket;
}
/**
* @param string
*/
public void setFileName(String string) {
FileName = string;
}
}
------------------------------------------------
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
public class SocketDriver {
public static void main(String args[]) {
int port = 0; //Port as first Argument , >> 1000, typically 40001
String fileName = "";
//File Name of CheckPointFirewall File , used as input to this program
//Fourth Argument would be Duration in terms of Seconds, 0 meaning Continuous
if (args.length > 0)
port = Integer.parseInt(args[0]);
//System.out.println(port);
if (args.length > 1) {
fileName = args[1];
}
//Printing System Info
Properties p = System.getProperties();
System.out.println(p);
System.out.println("File Name = " + fileName);
ServerSocket serversocket;
Socket MyOwnClient;
String fMessage = "";
try {
serversocket = new ServerSocket(port);
try {
InetAddress local_Address = InetAddress.getLocalHost();
fMessage = "Local DNS Name = " + local_Address.toString();
} catch (UnknownHostException e) {
fMessage = "Unable to obtain Domain Name";
}
System.out.println("Accepting Connections...on port: " + port);
System.out.println(fMessage);
System.out.println("Starting to Accept Connections ....");
while (true) {
MyOwnClient = serversocket.accept();
System.out.println(
"Client Machine "
+ MyOwnClient.getInetAddress().getHostName()
+ " Connected!");
SocketServer instance = SocketServer.instance();
}
} catch (IOException e) {
System.out.println("Failed to Connect to socket!");
}
}
}
----------
Thanks,
-Kamal.