-3

I'm currently writting a TCP/IP client in Java. The only thing that the client should be able to do is to run code that the server sends to it. I have seen this question , but my problem is that the code would be ran like if it was another application, but if the client code contains a string and the server sends a message saying, read the string and send back the content, then the client would not be able to do so because that code would be executed in another program where that string dosen't exist :-(

So is there a way that i can run code from a input like if it was written in the source code of the program?

All help is much appreciated :-)

Example:

The client contains a string that says "string0001". The client should run the code that the server sends. The server sends a block of java-code that the client shouls be able to execute. The code says: check content of string and send it back to this ip. The client should now be able to read the string and send back "string0001" to the servers ip.

With the solution that the link provided, the code that the server sent would be executed like if it was another program, and in that program the string that the server requested the client to read does not exist.

The client whould now be able to run the code like if it was written in the clients source, where the string exist.

Hope that helps :-)

Community
  • 1
  • 1
deni
  • 119
  • 1
  • 1
  • 11
  • 1
    Can you please separate this into steps, one by one, detailing the flow of data between the client and the server. This is very hard to follow at the moment. I'm sure it's a good question but you run the danger of downvotes. – Joe Feb 11 '13 at 00:07
  • Please give a concrete example. What is an example of the string that is in the client code? – Code-Apprentice Feb 11 '13 at 00:07
  • 3
    This sounds like a security nightmare... –  Feb 11 '13 at 00:09
  • are you sure you need to run arbitrary code, rather than have functions on the client the server can call? – BevynQ Feb 11 '13 at 00:12
  • I am sure, it isn't a security nightmare because the connection between the server and the client is encrypted and the server has been veryfied :-) I have added an example :-) – deni Feb 11 '13 at 00:18
  • What is the representation of the source code that the server sends to the client? Is it a String containing Java code? Is it bytecode? Will the request always be "send the string to ip X"? – Code-Apprentice Feb 11 '13 at 00:21
  • Yes, it's a string containing some command and the code seperated by a space. And no, the request will not always be send string to IP X, I would wish it would be that easy :-) – deni Feb 11 '13 at 00:24

1 Answers1

0

Well the first problem is that the source code sent has to be complete. Java is an object oriented language so the code to execute might have references to classes which have yet to be sent.

Which leads onto the 2nd problem... How does it know when it has received all the code, and what packages and files does the code need to live in?

If the code is a very simple routine with no methods or classes, you could create a class with a method and insert the passed code into it, then keep it in memory.

You can then compile this code using a similar routine to the question answered here.

Community
  • 1
  • 1
Eurig Jones
  • 8,226
  • 7
  • 52
  • 74
  • But when the code is then compiled, it won't be able to read the string from the orriginal code :-( Or have i misunderstood something? Thanks anyway :-) – deni Feb 11 '13 at 00:20
  • Well you would have the original code in a String in memory, so if you needed to read it for whatever reason you would have it. – Eurig Jones Feb 11 '13 at 00:24
  • A better question would maybe be, how is the code executed in the new application going to know what the server IP is? And if you would somehow be able to send the code into the newly created application, and the code says send to this server at this port, the port is already binded by the other application. You help is very appreciated :-) – deni Feb 11 '13 at 00:29