The Artima Developer Community
Sponsored Link

Java Answers Forum
Socket Communication (Server and Client)

1 reply on 1 page. Most recent reply: Nov 12, 2003 9:53 PM by Parag Agarwal

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
steps

Posts: 14
Nickname: spanishste
Registered: Nov, 2003

Socket Communication (Server and Client) Posted: Nov 11, 2003 5:03 AM
Reply to this message Reply
Advertisement
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

if (event.getActionCommand().equals(StartRC)){
MatlabRCSocket = new Socket("IP-address",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

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");
}
}

}
}
}


Parag Agarwal

Posts: 1
Nickname: cisparag
Registered: Nov, 2003

Re: Socket Communication (Server and Client) Posted: Nov 12, 2003 9:53 PM
Reply to this message Reply
In order to connect to the other machine u dont have to do the extra coding. Just give the address of the remote machine and the port number in the socket constructor.
Only thing is that server has to be present on the network.

Flat View: This topic has 1 reply on 1 page
Topic: TCP/IP programming using sliding window Previous Topic   Next Topic Topic: Need Help with an

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use