Socket Exception is : The operation is not allowed on non-connected sockets.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keithseah
    New Member
    • Oct 2009
    • 2

    Socket Exception is : The operation is not allowed on non-connected sockets.

    Hi all,

    i've been having this problem and its kiiling me! i'm a newbie at this so i hope someone would be able to help me.

    picture link: http://i98.photobucket.com/albums/l2...plication2.jpg

    this pops up whenever i click on the Disconnect button after i have connected.

    these are the following codes for the program.
    Code:
    Imports SN = System.Net
    Imports SNS = System.Net.Sockets
    Imports ST = System.Text
    Imports System.Net
    
    Public Class _Default
        Dim tcpClient As New System.Net.Sockets.TcpClient
        Dim connectport As String
    
        Sub dc(ByVal tcpclient As SNS.TcpClient)
            Dim NetworkStream As SNS.NetworkStream = tcpclient.GetStream
            tcpclient.Client.Close()
            NetworkStream.Close()
            msg("Disconnected from port:" + connectport)
            status("IP:" + "Disconnected" + vbCrLf + "Port:" + "Disconnected")
        End Sub
    
        Private Sub dcBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dcBtn.Click
            Try
                dc(tcpClient)
            Catch ex As Exception
                msg("Socket Exception is : " & ex.Message)
            End Try
        End Sub
    
    End Class
    
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Sub msg(ByVal message As String)
            Msgbox.Text = Msgbox.Text + message + vbCrLf
        End Sub
    
        Sub status(ByVal message As String)
            statusbox.Text = message + vbCrLf
        End Sub
        Sub outputconnect(ByVal tcpclient As SNS.TcpClient)
            'Input IP Address and TCP port
            Dim address As String
            Dim port As String
            address = ipbox.Text
            port = portbox.Text
            connectport = portbox.Text
    
            If tcpclient.Connected = False Then
                tcpclient.Connect(address, port)
                msg("Connected to IP:" + address + " via port:" + port)
                status("IP:" + address + vbCrLf + "Port:" + port)
            End If
        End Sub
    
    
        Private Sub connectBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles connectBtn.Click
            tcpClient = New System.Net.Sockets.TcpClient
            Try
                outputconnect(tcpClient)
            Catch ex As Exception
                msg("Socket Exception is : " & ex.Message)
            End Try
        End Sub
    End Class

    I hope someone would be able to help me, i would really appreciate it!

    Keith.
    Last edited by Frinavale; Oct 27 '09, 01:25 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • dafodil
    Contributor
    • Jul 2007
    • 389

    #2
    The exception clearly states that you should not try to disconnect unconnected sockets. Instead of declaring new sockets to disconnect, I suggest try to use you're old TcpClient connection.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Check if the TcpClient is closed before attempting to use it.

      Comment

      • keithseah
        New Member
        • Oct 2009
        • 2

        #4
        How do i check if my TcpClient is closed or how do i use my old TcpClient?
        Sorry i'm really new at this so i dont even know the basics.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Use the TcpClient.Conne cted property to check whether the underlying Socket for the TcpClient is connected to a remote host.

          Cheers!

          -Frinny

          Comment

          Working...