Results 1 to 2 of 2
  1. #1
    mania is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default java.net.SocketException: Connection reset

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

    Two server sockets are running,
    1) Control Server at port 10500, I have no problem in getting result from this server it returns "done#1#end#done#2#end#done#3#end#done#4#end#done# 5#end" at line 67.
    2) Transfer Server at port 10502, the socket exception occurs at the time of reading result from Transfer server at line 96.

    And the Client Socket is running at localhost/10501.

    At line 92, I've already checked if there is a client connection and the opening state of ControlServer/TransferServer.
    But the inputstream (_inFromTransferServer) for reading the result from TransferServer is not ready. I wonder if that causes this exception?

    That's the main():
    Java Code:
    transferThread = new TransferServer(variableFilter.getExpression(),simulationStopTime.getExpression());  
                                _transferThread.run();
    That's the thread class:
    Java Code:
    public TransferServer(String parameterFilter, String stopTime)  
                    throws IOException {  
                    _controlClient = new Socket("localhost", 10501);  
                    _toServer = new BufferedWriter(new OutputStreamWriter(  
                        _controlClient.getOutputStream()));  
              
                _toServer.write("setcontrolclienturl#1#127.0.0.1#10500#end");  
                    _toServer.flush();  
                _controlServer = new ServerSocket(10500);  
                Socket _controlConnection = _controlServer.accept();  
                _inFromControlServer = new BufferedReader(new InputStreamReader(  
                        _controlConnection.getInputStream()));  
      
         _toServer.write("settransferclienturl#2#127.0.0.1#10502#end");  
            _toServer.flush();  
            _transferServer = new ServerSocket(10502);  
             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];  
                try {  
                    _inFromControlServer.read(controlServerBuffer);  
                    String statusMessage = new String(controlServerBuffer).trim();  
                    if (statusMessage != null) {  
                        String loggerInfo = "Status Message from Transfer Server : "  
                                + statusMessage;  
                        _omcLogger.getInfo(loggerInfo);  
                    }  
                } catch (IOException e) {  
                    e.printStackTrace();  
                    throw new IOException("!" + e.getMessage());  
                }  
    }  
    }  
    public void run() {  
    String lineDelimiter = "end";  
            String timeDelimiter = "#";  
            String wholeSimulationResult = null;  
            String lineSimulationResult = null;  
            String[] lineSplitResult = null;  
            String[] timeSplitResult = null;  
            char[] transferServerBuffer = new char[1024];  
            int streamIndex = 0;  
            try {  
        // FIXME THE SERVER'S INPUTSTREAM IS NOT READY, THIS CONDITION RETURN FALSE.  
       //  if(_inFromTransferServer.ready()){  
                    if (_controlClient.isConnected()  
                            && !(_transferServer.isClosed() && _controlServer  
                                    .isClosed())) {  
                        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();  
                            String loggerInfo = "Socket Closed!";  
                            _omcLogger.getInfo(loggerInfo);  
                        } catch (IOException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }
    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)

  2. #2
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

Similar Threads

  1. java.net.SocketException: Connection reset
    By mania in forum Threads and Synchronization
    Replies: 0
    Last Post: 10-08-2013, 07:34 PM
  2. Replies: 1
    Last Post: 02-01-2013, 04:55 AM
  3. Replies: 1
    Last Post: 11-22-2012, 06:04 PM
  4. Replies: 2
    Last Post: 04-29-2011, 07:04 AM
  5. Replies: 2
    Last Post: 01-24-2010, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •