Wierd Client Server Errors
Ok i got some wierd client server errors here that make no sense to me..well i have a theory but i have no idea how to fix it.
Here is the Cllient program:
Code:
import java.io.*;
import java.net.*;
class ProbeClient{
public static void main(String args[]){
String msgIn;
String msgOut;
boolean transmit=true;
String rockTypeArray[]={"s","ig"}; //simple array for rock type
String rockType="";
int fluidFactor=0; //fluid factor
//grid arrays
//grid identifiers
int gx1=1; int gx2=0;
String gridAnalysis;
while(transmit=true){
try {
//get server port
String server = args[0];
int port = Integer.parseInt(args[1]);
//create socket
Socket s = new Socket(server, port);
PrintWriter n = new PrintWriter(new OutputStreamWriter(
s.getOutputStream()),true);
rockType=rockTypeArray[(int)(Math.random()*2)];
if(rockType.equals("s")){
fluidFactor=(int)(Math.random()*10+1);
}
if(gx1==8 && gx2==8){
System.out.println("The Sending of info is complete");
n.close();s.close();
break;
}
else if(gx1<8 && gx2==8){
gx1++;gx2=1;
gridAnalysis=toString(gx1,gx2,rockType,fluidFactor);
n.println(gridAnalysis);
}
else{
gx2++;
gridAnalysis=toString(gx1,gx2,rockType,fluidFactor);
n.println(gridAnalysis);
}
}
catch(Exception e){
System.out.println("Exception: " + e);
}
}
}
public static String toString(int i, int j,String rock,int flF){
if(rock.equals("s")){
return "[" +i+","+j+"]"+rock+flF;
}
if(rock.equals("ig")){
return "[" + i+","+j+"]"+rock;
}
else{
return "";
}
}
}
And here is the Server
Code:
import java.io.*;
import java.net.*;
import java.util.*;
class ResultServer {
public static void main(String args[]){
String message;
int i=0;
try {
//get a port
int port = Integer.parseInt(args[0]);
//create server socket on specified port
ServerSocket ss = new ServerSocket(port);
System.out.println("Server is up and running, waiting for probe to connect");
while(true && i<64){
//create a socket as you accept incoming requests
Socket s = ss.accept();
//write result to client
BufferedReader y=new BufferedReader(new InputStreamReader(
s.getInputStream()));
PrintWriter output;
output = new PrintWriter (new FileWriter("exploreData.txt"));
String msgIn=y.readLine();
output.println(msgIn);
System.out.println(msgIn);
i++;
if(i==63){
output.close(); y.close(); s.close();
System.out.println("Information has been store in file...Program Completed");
}
}
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
You will have to make an exploreData.txt file ..
I have no idea why it doesnt work.. it goes up to [4,5] and then quits as if it were [8,8].
I think the problem is that everytime i send a stream to the server it also sends some other line or something..(When u run it u see at the end in the server there is a bunch of blank lines) and therefore it thinks its been sent something else and it for every one it thinks its gone 2...
I need to fix this ASAP!!!!!!!!!!!!!!!
Thx for any help!!!!!!!!!!!!!!