yes as i said its socket client -server communication. The client code is below:
import java.net.*;
import java.io.*;
public class Client{
public static void main (String [] args ) throws IOException {
Socket sock = new Socket("127.0.0.1",45789);
System.out.println("Connected");
// send Response file
File myFile = new File ("Response.txt");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();
}
}
and now i made stand alone MAIN with server so first you run server , then client with some txt message containing string "not sure". So as soon as server will get the message it evokes parser...and here it happens!
server code:
import java.net.*;
import java.io.*;
public class Server {
public static void main (String [] args ) throws IOException {
//public String getResponse () throws IOException {
int filesize=500000; // file size
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
// create socket
ServerSocket servsock = new ServerSocket(45789);
while (true) {
System.out.println("Waiting for connection");
Socket sock = servsock.accept();
System.out.println("Connected : " + sock);
byte [] mybytearray = new byte [filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("ResponseGot.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();
Parser parser=new Parser();
System.out.println("file got! - start Parser");
String MessageGot=parser.response();
System.out.println("the response" + MessageGot);
// return MessageGot;
}
}
}