473,385 Members | 1,944 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 taking too long to close...

OK I'm about to pull my hair out on this one. I've got a socket created that sends a network stream of data from a listview to an open port.

It works great!! I'm getting ACK's back from the server with no problem. I can even send multiple messages at a time. HOWEVER...

If I highlight one item and send it... then highlight another item immediately afterwards and send it... I get a connection refused.

If I send one item, wait about 7-8 seconds... then send another one... I'm fine.

Please review my code and tell me where I'm going wrong!! Thanks in advance!!
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  3.  
  4.         Dim ListViewSelect As ListView.SelectedListViewItemCollection = Me.ListView1.SelectedItems
  5.         Dim item As ListViewItem
  6.         Dim selecteditem As String
  7.         Dim selecteditemhex As String = Chr(11)
  8.         Dim SelectedCount As String
  9.         Dim tcpClient As New System.Net.Sockets.TcpClient
  10.  
  11.  
  12.         Try
  13.             tcpClient.Connect("10.1.1.94", 8444)
  14.             'tcpClient.Connect("127.0.0.1", 8444)
  15.         Catch ex As Exception
  16.             MessageBox.Show(Err.Description)
  17.         End Try
  18.         SelectedCount = ListViewSelect.Count()
  19.  
  20.         Select Case SelectedCount
  21.             Case 0
  22.                 MessageBox.Show("Please select 1 item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  23.             Case Is > 0
  24.                 For Each item In ListViewSelect
  25.  
  26.                     selecteditem = selecteditemhex + LTrim(item.SubItems(4).Text)
  27.  
  28.                     Dim networkStream As NetworkStream = tcpClient.GetStream
  29.  
  30.                     If networkStream.CanWrite And networkStream.CanRead Then
  31.                         Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(selecteditem)
  32.                         networkStream.Write(sendBytes, 0, sendBytes.Length)
  33.                         Dim bytes(tcpClient.ReceiveBufferSize) As Byte
  34.                         networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
  35.                         Dim returndata As String = Encoding.ASCII.GetString(bytes)
  36.                         MessageBox.Show("Host returned: " + returndata)
  37.                     Else
  38.                         If Not networkStream.CanRead Then
  39.                             MessageBox.Show("cannot write data to this stream")
  40.                             tcpClient.Close()
  41.                         Else
  42.                             If Not networkStream.CanWrite Then
  43.                                 MessageBox.Show("cannot read data from this stream")
  44.                                 tcpClient.Close()
  45.  
  46.                             End If
  47.                         End If
  48.                     End If
  49.                 Next
  50.  
  51.         End Select
  52.         btnSend.Text = "Sent"
  53.         tcpClient.GetStream.Close()
  54.         'tcpClient.Close()
  55.     End Sub
Jul 20 '07 #1
0 1027

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

Similar topics

8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
3
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
2
by: yvan | last post by:
Hi, Here is my client/server scenario: Step1: Client connects to server and sends data sucessfully (using Socket.Send()). Step2: Server gracefully exists (calls Socket.Shutdown() and...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
1
by: Jay | last post by:
I have a client app sending messages over the socket on a certain port. In the client app: I do this for every message I need to send: 1. Create a socket and connect. 2. Send message 3. Close...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.