Hi everyone,

I'm new at socket programming and threads. I'd be happy if anyone can help me out with this exception.

In the main() I constructed the thread and then run it:

TransferServer _transferThread = new TransferServer(variableFilter.getExpression(),simu lationStopTime.getExpression());
_transferThread.run();


public class TransferServer extends Thread {
/** Construct Transfer Server thread by creating Transfer Server object and setting IP/port of the Server.
* @param toServer An output stream to send the request from Control Client to Control Server.
* @param inFromControlServer Set up an input stream to receive the response/simulation result back from the control server.
* @throws IOException If I/O error occurs while creating the socket.
*/
public TransferServer(String parameterFilter, String stopTime)
throws IOException {
_stopTime = stopTime;
_parameterFilter = parameterFilter;
_omcLogger = OMCLogger.getInstance();
_controlClient = new Socket("localhost", 10501);
// Set up an output stream to send request/operation to Control Server.
_toServer = new BufferedWriter(new OutputStreamWriter(
_controlClient.getOutputStream()));
_controlServer = new ServerSocket(10500);
String _controlRequest = "setcontrolclienturl#1#127.0.0.1#10500#end";
// Set the protocol of Control Server.
if (_controlRequest != null) {
_toServer.write(_controlRequest);
_toServer.flush();
}
Socket _controlConnection = _controlServer.accept();
// Set up an input stream to receive the response/simulation result back from Control Server.
_inFromControlServer = new BufferedReader(new InputStreamReader(
_controlConnection.getInputStream()));
_transferServer = new ServerSocket(10502);
String _transferRequest = "settransferclienturl#2#127.0.0.1#10502#end";
if (_transferRequest != null) {
_toServer.write(_transferRequest);
_toServer.flush();
}
Socket _transferConnection = _transferServer.accept();
_inFromTransferServer = new BufferedReader(new InputStreamReader(
_transferConnection.getInputStream()));
if (_controlClient.isConnected() && _toServer != null) {
try {
String parameterSequence = _parameterFilter;
String[] parameters = null;
String parameterDelimiter = "#";
parameters = parameterSequence.split(parameterDelimiter);
if (parameters.length >= 2) {
_toServer
.write("setfilter#3#" + parameterSequence + "#end");
_toServer.flush();
} else if (parameters.length == 1) {
_toServer.write("setfilter#3#" + parameters[0] + "#end");
_toServer.flush();
} else
System.err.println("Filter cannot be set!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
_toServer.write("start#4#end");
_toServer.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
_toServer.write("shutdown#5#end");
_toServer.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
throw new IOException("!" + e.getMessage());
}
char[] controlServerBuffer = new char[124];
String statusMessage = null;
try {
_inFromControlServer.read(controlServerBuffer);
statusMessage = new String(controlServerBuffer).trim();
} catch (IOException e) {
e.printStackTrace();
throw new IOException("!" + e.getMessage());
}
String loggerInfo = "Status Message from Control Server for setting transfer and control client url: ";
statusMessage = new String(controlServerBuffer).trim();
if (statusMessage != null) {
loggerInfo = "Status Message from Transfer Server : "
+ statusMessage;
_omcLogger.getInfo(loggerInfo);
}
}
}
public void run() {
// Read simulation result from Transfer Server, then write them to the console.
String lineDelimiter = "end";
String timeDelimiter = "#";
String[] lineSplitResult = null;
String[] timeSplitResult = null;
String[] recordResult = null;
String wholeSimulationResult = null;
String lineSimulationResult = null;
int streamIndex = 0;
char[] transferServerBuffer = new char[1024];
RecordToken record = null;
// It returns -1 if the end of the stream is reached.
// If not, it returns the number of chars that have been read.
if (_transferServer.isBound()) {
Outerloop: try {
while ((streamIndex = _inFromTransferServer
.read(transferServerBuffer)) != -1) {
wholeSimulationResult = new String(transferServerBuffer)
.trim();
lineSplitResult = wholeSimulationResult
.split(lineDelimiter);
for (int i = 0; i < lineSplitResult.length - 1; i++) {
lineSimulationResult = lineSplitResult[i];
timeSplitResult = lineSimulationResult
.split(timeDelimiter);
for (int j = 1; j < timeSplitResult.length; j++) {
if (j == 1) {
if ((timeSplitResult[j].toString()
.startsWith(_stopTime)))
break Outerloop;
else
System.out.print("At time : "
+ timeSplitResult[j] + " ");
} else {
System.out.print(timeSplitResult[j] + " ");
}
}
}
}
System.out.println("Stop time is reached!");
} catch (IOException e) {
e.printStackTrace();
}
try {
_controlClient.close();
_controlServer.close();
_toServer.close();
_inFromControlServer.close();
// Stop the thread and close the server socket.
stopExecuting();
String loggerInfo = "Socket Closed!";
_omcLogger.getInfo(loggerInfo);
} catch (IOException e) {
e.printStackTrace();
}
}
}
///////////////////////////////////////////////////////////////////
//// private variables ////
// Control client socket.
private Socket _controlClient = null;
// Control server socket.
private ServerSocket _controlServer = null;
// Set up an input stream to receive the response/simulation result back from the control server.
private BufferedReader _inFromControlServer = null;
// An input stream to receive the simulation result back from Transfer Server.
private BufferedReader _inFromTransferServer = null;
// OMCLogger Object for accessing a unique source of instance.
private OMCLogger _omcLogger = null;
//
private String _parameterFilter = null;
// Stop time of model simulation.
private String _stopTime = null;
// Set up an output stream to send the request/operation from control client to the server.
private BufferedWriter _toServer = null;
// Transfer Server.
private ServerSocket _transferServer = null;
}
That's the stacktrace of the exception:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream. java:196)
at java.net.SocketInputStream.read(SocketInputStream. java:122)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.j ava:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.ja va:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:1 77)
at java.io.InputStreamReader.read(InputStreamReader.j ava:184)
at java.io.BufferedReader.fill(BufferedReader.java:15 4)
at java.io.BufferedReader.read1(BufferedReader.java:2 05)
at java.io.BufferedReader.read(BufferedReader.java:27 9)
at java.io.Reader.read(Reader.java:140)
at ptolemy.domains.openmodelica.lib.omc.TransferServe r.run(TransferServer.java:216)
at ptolemy.domains.openmodelica.lib.OpenModelica.fire (OpenModelica.java:466)
at ptolemy.actor.sched.FixedPointDirector._fireActor( FixedPointDirector.java:808)
at ptolemy.actor.sched.FixedPointDirector.fire(FixedP ointDirector.java:241)
at ptolemy.domains.continuous.kernel.ContinuousDirect or.fire(ContinuousDirector.java:448)
at ptolemy.actor.CompositeActor.fire(CompositeActor.j ava:450)
at ptolemy.actor.CompositeActor.iterate(CompositeActo r.java:1089)
at ptolemy.actor.sched.StaticSchedulingDirector.fire( StaticSchedulingDirector.java:210)
at ptolemy.domains.sdf.kernel.SDFDirector.fire(SDFDir ector.java:492)
at ptolemy.actor.CompositeActor.fire(CompositeActor.j ava:450)
at ptolemy.actor.Manager.iterate(Manager.java:787)
at ptolemy.actor.Manager.execute(Manager.java:352)
at ptolemy.actor.Manager.run(Manager.java:1202)
at ptolemy.actor.Manager$PtolemyRunThread.run(Manager .java:1760)

Thanks