473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.
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
4 15204
dafodil
392 256MB
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
9,735 Expert Mod 8TB
Check if the TcpClient is closed before attempting to use it.
Oct 27 '09 #3
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
9,735 Expert Mod 8TB
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

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

Similar topics

2
by: David Konerding | last post by:
Hello, I have written an app which opens a TCP connection to a server and uses a protocol to communicate with it. Specifically, I've written a python IMD client for the molecular dynamics...
2
by: Piotr Bieniek | last post by:
Hello, I have a problem with UDP sockets. It concerns UdpClient class as well. It throws strange exceptions on subsequent Send calls. Exception is SocketException with native error code 10049. I...
0
by: Todd Bandrowsky | last post by:
Any idea why a web service synchronously called would throw a socket exception some time ten minutes down the road?
1
by: mp | last post by:
Hi, I have following problem: Operation must use an updateable query I have use OleDb, C# and ASP.NET and MS Access DB named pubs. Code:
2
by: Naveen Mukkelli | last post by:
Hi, I'm writing a client/server app. I want to run my server on "Windows Server 2003" PC. This PC has two network cards. 1 for Internet and other is plugged into a switch. My plan is that...
1
by: Bernd | last post by:
Hello, i have a problem with my network code. What i want to do is, send an UDP-packet and check if i get an icmp-reply type 3 (dest. unreachable). the code looks like that so far: .. ..
6
by: Rik | last post by:
Hello Experts, I have a communication server in VB.NET. It was working fine from last 6 months, but now start giving error message like that. 21-03-2005 07:58:27...
4
by: Rollasoc | last post by:
Hi, We have a range of four products that can talk to our software (Written in C# & managed C++) via ethernet. Using standard socket class. This has all been working fine for a long time now,...
6
by: toton | last post by:
Hi, I am c++ standard exceptions like out_of_range, but want it to have some nonascii message. However what returns a char* and the class is not templated. Do I need to write my own exception...
2
by: Steve Walton | last post by:
We have a little server application that sends data down to a client application via a TCP/IP socket over port 30006. These applications are both written in C#. They have been working on our site...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.