By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,146 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

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

P: 2
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.
Expand|Select|Wrap|Line Numbers
  1. Imports SN = System.Net
  2. Imports SNS = System.Net.Sockets
  3. Imports ST = System.Text
  4. Imports System.Net
  5.  
  6. Public Class _Default
  7.     Dim tcpClient As New System.Net.Sockets.TcpClient
  8.     Dim connectport As String
  9.  
  10.     Sub dc(ByVal tcpclient As SNS.TcpClient)
  11.         Dim NetworkStream As SNS.NetworkStream = tcpclient.GetStream
  12.         tcpclient.Client.Close()
  13.         NetworkStream.Close()
  14.         msg("Disconnected from port:" + connectport)
  15.         status("IP:" + "Disconnected" + vbCrLf + "Port:" + "Disconnected")
  16.     End Sub
  17.  
  18.     Private Sub dcBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dcBtn.Click
  19.         Try
  20.             dc(tcpClient)
  21.         Catch ex As Exception
  22.             msg("Socket Exception is : " & ex.Message)
  23.         End Try
  24.     End Sub
  25.  
  26. End Class
  27.  
  28. Partial Class _Default
  29.     Inherits System.Web.UI.Page
  30.  
  31.     Sub msg(ByVal message As String)
  32.         Msgbox.Text = Msgbox.Text + message + vbCrLf
  33.     End Sub
  34.  
  35.     Sub status(ByVal message As String)
  36.         statusbox.Text = message + vbCrLf
  37.     End Sub
  38.     Sub outputconnect(ByVal tcpclient As SNS.TcpClient)
  39.         'Input IP Address and TCP port
  40.         Dim address As String
  41.         Dim port As String
  42.         address = ipbox.Text
  43.         port = portbox.Text
  44.         connectport = portbox.Text
  45.  
  46.         If tcpclient.Connected = False Then
  47.             tcpclient.Connect(address, port)
  48.             msg("Connected to IP:" + address + " via port:" + port)
  49.             status("IP:" + address + vbCrLf + "Port:" + port)
  50.         End If
  51.     End Sub
  52.  
  53.  
  54.     Private Sub connectBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles connectBtn.Click
  55.         tcpClient = New System.Net.Sockets.TcpClient
  56.         Try
  57.             outputconnect(tcpClient)
  58.         Catch ex As Exception
  59.             msg("Socket Exception is : " & ex.Message)
  60.         End Try
  61.     End Sub
  62. End Class

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

Keith.
Oct 23 '09 #1
Share this Question
Share on Google+
4 Replies


dafodil
100+
P: 392
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.
Oct 23 '09 #2

Frinavale
Expert Mod 5K+
P: 9,731
Check if the TcpClient is closed before attempting to use it.
Oct 27 '09 #3

P: 2
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.
Oct 28 '09 #4

Frinavale
Expert Mod 5K+
P: 9,731
Use the TcpClient.Connected property to check whether the underlying Socket for the TcpClient is connected to a remote host.

Cheers!

-Frinny
Oct 28 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.