I seem to be having an issue with a program I am creating. It should be a simple program that sends commands from a Java program to a Groovy script (whether the script be on the same computer or another on the network) but I am running into some problems.
I have found a very simple TCP/IP client snippet and am trying to get it to access a groovy script I have created that is listening on port a certain port.
Before implementing this in my program, I did a simple test with telnet. It came back with positive results so I went ahead and tried it out. Unfortunately I have received the following exception: java.io.StreamCorruptedException: invalid stream header: 57686174
Here is my Java program source:
public class Client {
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Client() {
}
void run() {
try {
requestSocket = new Socket("localhost", 1960);
System.out.println("Connected to localhost in port 1960");
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
do {
try {
message = (String)in.readObject();
System.out.println("server>" + message);
sendMessage("dir");
}
catch(ClassNotFoundException classNot) {
System.err.println("Data received in unknown format");
}
} while(!message.equals("exit"));
}
catch(UnknownHostException unknownHost) {
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException) {
ioException.printStackTrace();
} finally {
try {
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException) {
ioException.printStackTrace();
}
}
}
void sendMessage(String msg) {
try {
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException) {
ioException.printStackTrace();
}
}
public static void main(String args[]) {
Client client = new Client();
client.run();
}
}
Here is my groovy script (forgive me im new to groovy):
//variables
def serialNo
def ipAddress
def hostName
def operatingSystem
def test
server = new ServerSocket(1960)
while(true) {
server.accept() { socket ->
socket.withStreams { input, output ->
// def writer = new BufferedWriter(new OutputStreamWriter(output));
w << "What is your command? "
w.flush()
def reader = new BufferedReader(new InputStreamReader(input));
r = reader.getText();
if(r=="serial") {
getSerialNo();
} else if(r=="dir") {
getTest();
} else if(r=="os") {
getOperatingSystem();
} else if(r=="host") {
getHostName();
} else if(r=="ip") {
getIpAddress();
} else if(r=="passthru") {
//research passing through computers on a network
} else if(r=="exit") {
println "Goodbye."
writer.close()
reader.close()
} else {
println " $r command unknown."
}
reader.close()
writer.close()
}
}
}
def getTest() {
test = "cmd /c dir".execute().text
println test
return this.test
}
def getSerialNo() {
//search for hard drives on computer
//search for file containing serial no.
return this.serialNo;
}
def getIpAddress() {
ipAddress = "text"
return this.ipAddress;
}
def getHostName() {
machineName = "hostname".execute().text
return this.operatingSystem;
}
def getOperatingSystem() {
operatingSystem = "uname -a".execute().text
return this.operatingSystem;
}
I did some reading on the exception I am getting but couldn't find any definitive answers.
The only thing I could come up with is there might be an issue with my Java program when sending the message over to the computer that is running the groovy script. (for right now im just testing on the same computer)
Output:
Connected to localhost in port 2000
java.io.StreamCorruptedException: invalid stream header: 57686174
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at testapp.Client.run(Client.java:47)
at testapp.Client.main(Client.java:198)
Exception in thread "main" java.lang.NullPointerException
at testapp.Client.run(Client.java:67)
at testapp.Client.main(Client.java:198)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
Anyone have some suggestions?