2016-10-05 140 views
2

似乎没有办法在服务器端导致连接重置错误而断开套接字。Codenameone关闭套接字

我在测试应用中使用com.codename1.io.Socketcom.codename1.io.SocketConnection实现。我的代码如下:

private SpanLabel lblStatus; 
private SpanLabel lblIncoming; 
private CustomSocketConnection con; 
private Thread tIncoming; 

public ConnectForm() { 
    con = getSocketConnection(); 
    Button btnConnect = getConnectButton(); 
    Button btnDisconnect = getDisconnectButton(); 
    Button btnSendMessage = getSendMessageButton(); 
    lblStatus = getInfoLabel(); 
    lblIncoming = getIncomingLabel(); 

    setLayout(new BoxLayout(BoxLayout.Y_AXIS)); 
    addComponent(btnConnect); 
    addComponent(btnDisconnect); 
    addComponent(btnSendMessage); 
    addComponent(lblStatus); 
    addComponent(lblIncoming); 
} 

private Button getConnectButton() { 
    Button btn = new Button("Connect (localhost)"); 
    btn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      Socket.connect("localhost", 8687, con); 
     } 
    }); 
    return btn; 
} 

private Button getDisconnectButton() { 
    Button btn = new Button("Disconnect"); 
    btn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      //??? I don't know how to do this 
      try { 
       tIncoming.join(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       tIncoming.interrupt(); 
      } 
     } 
    }); 
    return btn; 
} 

private Button getSendMessageButton() { 
    Button btn = new Button("Send Message"); 
    btn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      try { 
       con.os.write("Hello".getBytes()); 
       con.os.write(Integer.parseInt("04", 16)); //end of transmit 
       con.os.flush(); 
       lblStatus.setText("Message Sent"); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    return btn; 
} 

private SpanLabel getInfoLabel() { 
    return new SpanLabel("Disconnected"); 
} 

private SpanLabel getIncomingLabel() { 
    return new SpanLabel("..."); 
} 

private CustomSocketConnection getSocketConnection() { 
    return new CustomSocketConnection(); 
} 

class CustomSocketConnection extends SocketConnection { 

    public OutputStream os; 
    public InputStream is; 

    @Override 
    public void connectionError(int errorCode, String message) { 
     lblStatus.setText("Error Connecting. ErrorCode: " + errorCode + " Message: " + message); 
    } 

    @Override 
    public void connectionEstablished(InputStream is, OutputStream os) { 
     lblStatus.setText("Connected :)"); 
     this.is = is; 
     this.os = os; 
     spawnIncomingMessageWatcher(); 
    } 
} 

private void spawnIncomingMessageWatcher() { 
    tIncoming = new Thread(new Runnable() { 
     public void run() { 
      String s = ""; 
      int eot = Integer.parseInt("04", 16); 
      while (con.isConnected()) { 
       try { 
        int temp; 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        while (((temp = con.is.read()) != -1) && (temp != eot)) { 
         baos.write(temp); 
        } 
        lblIncoming.setText(new String(baos.toByteArray())); 
        Thread.sleep(2000); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 
    tIncoming.start(); 
} 

随着getDisconnectButton()方法,我不知道如何正确地与服务器断开连接,作为SocketConnection对象似乎并不具备这种适当的方法。

回答

2

如果您在Input-或OutputStream上调用close(),则关闭套接字,Socket.SocketInputStream类中的代码为link

public void close() throws IOException { 
     closed = true; 
     if(Util.getImplementation().isSocketConnected(impl)) { 
      Util.getImplementation().disconnectSocket(impl); 
      con.setConnected(false); 
     } 
    } 

因此,首先发送关闭指令给服务器,然后关闭一个流。

希望这会有所帮助,