Hi everyone,
I am a little new to Java programming and am doing a simple project. The Problem is that I am able to connect but am not able to send any data. It is a little weird. The server gets the client's IP and the client to is connecting to the correct port yet no data is getting transferred. Perhaps, I am over looking on something, that's why I end up here.
A Brief Description of what I am trying to do:
Monitoring system:
Monitoring system has a pool of threads. Each thread connects to one clustered server. The Job of these threads is to dequeue the request and send them to the clustered servers(something may be wrong here). The Main program gets requests from clients(end user) and queues them.(This part is working well)
Clustered Servers:
These are more like workforce, all they do is get a request and calculate the answer and send them back to the Monitoring System.
End User:
Just a simple application to send the requests and get back answer.
That's all about the project.
Here is a code from Monitoring System.
/** delegates Request to the Clustered server */
public class Delegator implements Runnable {
int serverID;
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
FileInputStream fis = null;
public Delegator(int serverID){
this.serverID = serverID;
connectToServer();
}
public void run(){
while(true){
if(defs.reqQueue[serverID].isEmptyQueue() == false ){
RequestDetails reqDetails = defs.reqQueue[serverID].peekinto();
SendandRelay(reqDetails);
// record time stamp
long elaspsedTime = (Calendar.getInstance().getTimeInMillis() -
reqDetails.TimeStamp.getTimeInMillis()) / 1000;
// TODO: Update View State
}
}
}
public void SendandRelay(RequestDetails reqDetails){
try{
out.writeUTF(reqDetails.clientIP );
out.writeUTF(reqDetails.requestType);
out.writeUTF(reqDetails.request);
if(reqDetails.requestType.equalsIgnoreCase("compile")){
sendFileToServer(reqDetails);
}
} catch (IOException ex){}
relayReply(reqDetails.clientIP);
}
private void connectToServer(){
try{
socket = new Socket(defs.ServerIP[serverID], defs.clusterPort);
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
} catch (UnknownHostException ex){
} catch (IOException ex){}
}
// Below methods are not in context of the problem
private void sendFileToServer(RequestDetails reqDetails){...}
private void relayReply(String clientIP){...}
The above class is called by the class RequestHandler.java
...
for ( int i = 0; i < defs.nServer ; i++){
delegate[i] = new Delegator(i);
new Thread(delegate[i]).start();
}
...
RequestDetails has no methods, except for this constructor method public RequestDetails(String clientIP, String requestType, String request, Calendar TimeStamp)
RequestQueue, just handles this in a queue, its working well.
defs contains all the constants and other stuffs as static members.
It contains the server IP, port number to connect, etc
Clustered System:
This part is fairly simple, accepts a request, finds the answer and sends the result back.
class ReqHandler
{
static DataInputStream in = null;
static DataOutputStream out = null;
static ServerSocket server = null;
public static void main(String[] arg) throws IOException{
if(arg.length!=0)
System.out.println("No Parameters please:)");
try {
server = new ServerSocket(defs.listeningPort);
Socket incoming = server.accept();
in = new DataInputStream(new BufferedInputStream(incoming.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(incoming.getOutputStream()));
while (true) {
//Has not managed to read a single line:(
String requestID = in.readUTF();
String requestType = in.readUTF();
String Request = in.readUTF();
...
help me please.