Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket Communication

Status
Not open for further replies.

spanishsteps

Programmer
Nov 10, 2003
4
DE
Hello everybody,

I've a program where I can start matlab on a server where the ip-address and port no's are the inputs.As of now,these inputs are hard-coded in the code.When the IP-address matched with that of my local machine, then this works fine.I'm kinda confused in order to modify this code to make it working on a different machine.Could somebody pls help me.
Thanks for your time.

Heres the code under the action event
Code:
if (event.getActionCommand().equals(StartRC)){
			MatlabRCSocket = new Socket("131.246.110.43",7777);
			in = new DataInputStream(MatlabRCSocket.getInputStream());
			out = new DataOutputStream(MatlabRCSocket.getOutputStream());
			System.out.println(in.readUTF());
			remote = true;
			command.setText("");
			startRC.setEnabled(false);
                        stopRC.setEnabled(true);
                        return;
		}
		if (event.getActionCommand().equals(StopRC)){
			in.close();
			out.close();
			MatlabRCSocket.close();
			System.out.println("Connection ended");
			remote = false;
			startRC.setEnabled(true);
                        stopRC.setEnabled(false);
                        return;
		}
and the class file looks like
Code:
class MatlabRCServer
extends Thread
{
	private MatEng engine;
	DataInputStream in;
	DataOutputStream out;
	Socket MatlabRCSocket;
	ServerSocket MatlabRCServerSocket;

	public MatlabRCServer()
	{
		try {
			MatlabRCServerSocket = new ServerSocket(7777);
                                } catch (IOException e) {
			System.out.println("ServerSocket-Error" + e.getMessage());
		}
	}

	public void run ()
	{
		while (true) {
			try {

				MatlabRCSocket = MatlabRCServerSocket.accept();


				in = new DataInputStream(MatlabRCSocket.getInputStream());
				out = new DataOutputStream(MatlabRCSocket.getOutputStream());

				engine = new MatEng();

				engine.setOutputBuffer(4096);


				out.writeUTF("Matlab Engine started");
				out.flush();

				while (true) {
					engine.evalString(in.readUTF());
					out.writeUTF(engine.getOutput());
					out.flush();
				}
			} catch (IOException e1) {
			} finally {
				System.out.println("Server: Connection ended");
				engine.close();
				try{
					in.close();
					out.close();
					MatlabRCSocket.close();
				} catch (IOException e2){
					System.out.println("Exception in finally in MatlabRCServer");
				}
			}
		
     }
	}
}
 
You need to know the IP of the machine which is running your "MatlabRCServer".

So when you do :

>> MatlabRCSocket = new Socket("131.246.110.43",7777);

The IP address in that line needs to be that of your server ...

 
Hello Sedj,

Thats right, right now all the code is one single program.so, if i specify the ip-address and port no, the matlab starts on the local pc.How shld i modify this code in-order to start the matlab on any pc in the network.

Thanks for your time again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top