cannot receive UDP Packet from microcontroller - vb.net

I'm currently working on a UDP communication PC <-> ARM LM3S6965 (Luminary) through the Ethernet. On the PC there is a VB.net application that simulates a UDP server/client.
When the packet is sent from the PC to the ARM LM3S6965, the packet is received without errors, but when the ARM LM3S6965 sends the UDP packet back to the PC, the packet is lost somewhere (the application doesn’t receive it).
The strange thing is that WireShark captures these packets coming to the PC and it seems they are valid.
Turning off Firewall in Windows did not help. I know that this topic might be wrong for this forum, but can anybody explain why WireShark captures these packets, but my application doesn’t? ARM LM3S6965 (192.168.0.100), PC (192.168.0.116), sending and receiving goes through port number 3040, and i am sending broadcast message from VB.Net application which is received by ARM LM3S6965 micro controller.
Here is VB.net Code:
Public Const mnPort As Int16 = 3040 'Port number to send/recieve data on
Public Const msBroadcastAddress As String = "255.255.255.255" 'Sends data to all LOCAL listening clients, to send data over WAN you'll need to enter a public (external) IP address of the other client
Public udpReceivingClient As UdpClient 'Client for handling incoming data
Public udpSendingClient As UdpClient 'Client for sending data
Public receivingThread As Thread 'Create a separate thread to listen for incoming data, helps to prevent the form from freezing up
Public mbiClosing As Boolean = False 'Used to close clients if form is closing
Public Sub InitializeSender()
udpSendingClient = New UdpClient(msBroadcastAddress, mnPort)
udpSendingClient.EnableBroadcast = True
End Sub
Public Sub InitializeReceiver()
udpReceivingClient = New UdpClient(mnPort)
'Dim start As ThreadStart = New ThreadStart(AddressOf MT_Receiver)
'receivingThread = New Thread(start)
'receivingThread.IsBackground = True
'receivingThread.Start()
End Sub
Public Sub MT_Send_UDP(ByVal lbTxBuffer() As Byte)
Try
udpSendingClient.Send(lbTxBuffer, lbTxBuffer.Length)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Try
udpReceivingClient.BeginReceive(AddressOf MT_RX_Callback, Nothing)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub MT_RX_Callback(ByVal IR As IAsyncResult)
Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 3040)
Dim lbData() As Byte
Dim llRet As UInt16
If mbiClosing = False Then
llRet = udpReceivingClient.Available
lbData = udpReceivingClient.EndReceive(IR, endPoint)
If llRet > 0 Then
MT_Validate_Msg(lbData)
End If
udpReceivingClient.BeginReceive(AddressOf MT_RX_Callback, Nothing)
End If
End Sub
Private Sub frmSearchUDP_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
mbiClosing = True
udpReceivingClient.Close()
udpSendingClient.Close()
frmMain.Timer.Enabled = True
End Sub
Private Sub frmSearchUDP_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
InitializeSender()
InitializeReceiver()
End Sub

More a comment, but it's too long ...
No196: 42.430628
From 192.168.0.168 -> 255.255.255.255 (From your PC to your Hardware)
UDP ... Source Port: 63162 (63162)
Destination Port: tomato-springs (3040)
This looks nice and it works obviously, as your hardware send the response.
No197: 42.431017
From 192.168.0.100 -> 255.255.255.255 (From your hardware to your PC)
Source Port: tomato-springs (3040)
Destination Port: 63162 (63162)
Why your PC should receive this packet?
The destination Port is 63162 but you are listen to port 3040.

Related

UDP listener only receives first incoming message - how to keep receiving additional messages?

I'm trying to build a simple Windows Forms app in VB.net (.net Framework 2.0 in order to support Windows XP) which allows multiple client PCs to send short text strings as UDP messages to a listening "server".
The "server" part must listen asynchronously and display the received text from each client as it arrives.
My code can't use the newer ReceiveAsync() function because it must support XP and thus must be built on .net Framework 2.0, which doesn't support these Async functions, so I'm using the BeginReceive() function and passing a callback.
It works perfectly for the first incoming UDP message, but the problem is that no additional messages are received. Even if I send the same message twice, nothing is output after receiving the first incoming message. It's as if the listener ignores anything after the first message, or as if it's hung in an incorrect state. Is there some sort of "reset" procedure that must be performed on the UDP listener after each incoming message in order that it will listen for further incoming data? (Note: no exceptions are raised)
Here's my listener code:
Private Sub btnListen_Click(sender As Object, e As EventArgs) Handles btnListen.Click
Dim state As New UdpState
listener = New UdpClient(CInt(txtListenPort.Text)) ' Create UDP client on specified port (all interfaces)
'Async receive data:
Dim ip As System.Net.IPAddress = System.Net.Dns.GetHostEntry("localhost").AddressList(0)
state.udpClient = listener
state.ipEndPoint = New Net.IPEndPoint(ip, CInt(txtListenPort.Text))
Dim callback As AsyncCallback
callback = AddressOf rx
listener.BeginReceive(callback, state)
lblListenStatus.Text = "Listening..."
End Sub
Shared Sub rx(result As IAsyncResult)
Dim r As UdpState = result.AsyncState
Dim endpt As System.Net.IPEndPoint = r.ipEndPoint
Dim client As UdpClient = r.udpClient
Dim bytes() As Byte
bytes = client.EndReceive(result, endpt)
Dim s As String = System.Text.Encoding.ASCII.GetString(bytes)
Debug.Print("Received string = " & s)
End Sub
Private Class UdpState
Public ipEndPoint As System.Net.IPEndPoint
Public udpClient As System.Net.Sockets.UdpClient
End Class
And here's the code I'm using to send data to this socket endpoint:
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
Dim send As New UdpClient
Dim s As String = txtSendMessage.Text
Dim b_len As Integer = System.Text.Encoding.ASCII.GetByteCount(s)
Dim b() As Byte
ReDim b(b_len)
b = System.Text.Encoding.ASCII.GetBytes(s) 'Convert text to bytes.
send.Send(b, b_len, txtSendIP.Text, CInt(txtSendPort.Text)) 'Blocking SEND.
send.Close()
End Sub
Note that I'm testing sending from localhost to itself - not even sending over the network.
Thanks for any insight.

how to send data to remote device over udp?

i am trying to establish connection between calamp lmu (gps tracking device) and my server.
i am using following code to send and receive data.
i am using timer1 to receive data from device
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(remoteEP)
Dim returnData As String = BitConverter.ToString(receiveBytes)
txtLog.Text &= returnData.ToString & vbCrLf
Dim rep As New IPEndPoint(remoteEP.Address, C_DEVICE_LISTNING_PORT)
sktSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
sktSocket.Connect(remoteEP.Address, 20510)
sktSocket.SendTimeout = 100
'Dim udc As New UdpClient
'udc.Send(sendBytes, sendBytes.Length, rep)
'receivingUdpClient.Client.Send(sendBytes)
Catch ex As Exception
End Try
End Sub
following two variables are class level:
Dim remoteEP As New IPEndPoint(IPAddress.Any, 0)
Dim sktSocket As Socket
i am using following code to send data to device:
Private Sub butSend_Click(sender As Object, e As EventArgs) Handles butSend.Click
Dim sendBytes() As Byte = Encoding.ASCII.GetBytes(txtCommand.Text)
sktSocket.Send(sendBytes)
End Sub
my code to receive data works fine. when device sends data timer1 displays it in a textbox. But when i send data to the ip address returned by receivingUdpClient.Receive, it does not reach to the device. however wireshark shows that data has been sent.
any help will be appriciated.
i solved my problem myself. actually nothing was wrong in the code. actually i was connecting the device using forwarded port. it was hindering inward traffic from device to my system. Assigning static ip direct to my system solved the problem.

UDP local broadcast

I'm working on a remote control tool. The client runs a program to locally send commands to servers in order to control them.
However, the client doesn't know the server's IP address and vice versa.
I decided to use UDP broadcasting (please tell me if there's a better way to do this, I tried using multicast but I didn't really understand it). When started, the client (which controls the servers) broadcasts a message to tell the servers that a new client connected. Then (or when the server is started), the servers broadcast their own IP addresses. When the client receives an IP address, it tries to connect via TCP.
Unfortunately, I ran into problems with that. I randomly got An existing connection was forcibly closed by the remote host exceptions and I wasn't able to find out why.
The exception occurred in my client program when listening for UDP broadcasts.
Now, I'm looking for a better way to find the clients.
Should I use broadcast or multicast?
How would I implement that?
EDIT: It wouldn't be a problem to use multiple ports. However, I need to be able to run a client AND a server on a single computer.
Here's the code I was using
Client (controls servers)
'Variables
Private UdpBroadcaster As UdpClient
Private UdpBroadcasterEndpoint As New IPEndPoint(IPAddress.Broadcast, 4334)
'Sub New()
Try
UdpBroadcaster = New UdpClient(4333)
UdpBroadcaster.EnableBroadcast = True
Catch
MsgBox("Error creating UDP client! Port already in use?", ...)
End Try
'Called when the application starts
Private Sub StartUdpListener()
Dim ListenerUdp As New Thread(AddressOf UdpListener)
ListenerUdp.IsBackground = True
ListenerUdp.Start()
End Sub
'Started as thread in StartUdpListener()
Private Sub UdpListener()
Try
Do
'The next line fails with the error I described (An existing connection was forcibly closed by the remote host)
Dim ReceivedBytes() As Byte = UdpBroadcaster.Receive(UdpBroadcasterEndpoint)
Dim ReceivedString As String = System.Text.Encoding.UTF32.GetString(ReceivedBytes)
'The following three lines will just connect to the received hostname
Dim ScanThread As New Thread(Sub() ScanSingle(ReceivedString))
ScanThread.IsBackground = True
ScanThread.Start()
Loop
Catch
If Not UdpBroadcaster Is Nothing Then
UdpBroadcaster.Close()
UdpBroadcaster = Nothing
End If
InvokeStatus("UDP connection lost, please try again later.")
End Try
End Sub
'Called when the application starts and when the user manually clicks the "UDP Scan" button
Private Sub StartBroadcastUdpThread()
Dim UdpBroadcastThread As New Thread(Sub() BroadcastUdp())
UdpBroadcastThread.IsBackground = True
UdpBroadcastThread.Start()
End Sub
'Started as thread in StartBroadcastUdpThread()
Private Sub BroadcastUdp()
If UdpBroadcaster Is Nothing Then
Try
UdpBroadcaster = New UdpClient(4333)
UdpBroadcaster.EnableBroadcast = True
Catch
MsgBox("Error creating UDP Client.", MsgBoxStyle.Critical, "Error")
Application.Exit()
Return
End Try
End If
Dim BroadcastBytes() As Byte = System.Text.Encoding.UTF32.GetBytes("Client-Identify")
UdpBroadcaster.Send(BroadcastBytes, BroadcastBytes.Length, UdpBroadcasterEndpoint)
InvokeStatus("UDP request sent successfully")
End Sub
Servers (controlled by client)
'Variables
Private UdpBroadcaster As UdpClient
Private UdpBroadcasterEndpoint As New IPEndPoint(IPAddress.Broadcast, 4333)
'Main method
Public Sub Main()
Try
UdpBroadcaster = New UdpClient(4334)
UdpBroadcaster.EnableBroadcast = True
StartUdpListener()
StartBroadcastUdpThread()
Catch
Console.WriteLine("Failed to create server. Port already in use?")
End Try
End Sub
'Called in Main()
Private Sub StartUdpListener()
Dim ListenerUdp As New Thread(AddressOf UdpListener)
ListenerUdp.IsBackground = True
ListenerUdp.Start()
End Sub
'Started as thread in StartUdpListener()
Private Sub UdpListener()
Try
Do
Dim ReceivedBytes() As Byte = UdpBroadcaster.Receive(UdpBroadcasterEndpoint)
Dim ReceivedString As String = System.Text.Encoding.UTF32.GetString(ReceivedBytes)
If ReceivedString.Equals("Client-Identify") Then
StartBroadcastUdpThread()
End If
Loop
Catch
If Not UdpBroadcaster Is Nothing Then
UdpBroadcaster.Close()
End If
End Try
End Sub
'Called when the application is started or a "Client-Identify" command is received
Private Sub StartBroadcastUdpThread()
Dim UdpBroadcastThread As New Thread(Sub() BroadcastUdp())
UdpBroadcastThread.IsBackground = True
UdpBroadcastThread.Start()
End Sub
'Started as thread in StartBroadcastUdpThread()
Private Sub BroadcastUdp()
Dim BroadcastBytes() As Byte = System.Text.Encoding.UTF32.GetBytes(Dns.GetHostName)
UdpBroadcaster.Send(BroadcastBytes, BroadcastBytes.Length, UdpBroadcasterEndpoint)
End Sub
Thanks in advance!
Thank you for your answers. I fixed it by removing the feature to manually call StartBroadcastUdpThread() in my client.
I still don't understand why this happens though. I use exactly the same code for both client and server, except the ports are swapped. The TCP server doesn't crash even if the StartBroadcastUdpThread() method is called multiple times, the client does. By the way, the problem occurs regardless of whether the client or server is started first.
Even if I don't really understand why broadcasting the second time stops the client from receiving broadcasts - it's fixed for now. Thanks for you help!
I would suggest using Zeroconf to find the server and clients, and then use a TCP socket to communicate between the two. You can see an example implementation on key-value pair zeroconf announcements here: https://github.com/Eyescale/Lunchbox/blob/master/lunchbox/servus.cpp
Minimal UDP server basis:
Imports System.Threading
Shared client As UdpClient
Shared receivePoint As IPEndPoint
client = New UdpClient(2828) 'Port
receivePoint = New IPEndPoint(New IPAddress(0), 0)
Dim readThread As Thread = New Thread(New ThreadStart(AddressOf WaitForPackets))
readThread.Start()
Public Shared Sub WaitForPackets()
While True
Dim data As Byte() = client.Receive(receivePoint)
Console.WriteLine("=" + System.Text.Encoding.ASCII.GetString(data))
End While
End Sub

Visual Basic UDPClient Server/Client model?

So I'm trying to make a very simple system to send messages from a client to a server (and later on from server to client as well, but baby steps first). I'm not sure exactly how to use UDPClient to send and receive messages (especially to receive them), mostly because I don't have anything triggering the ReceiveMessage() function and I'm not sure what would.
Source Code is at this link, go to File>Download. It is already built if you want to just run the exe.
So my question is basically: How can I easily use UDPClient, how can I get this system to work and what are some tips for executing this kind of connection? Anything I should watch out for (threading, issues with code,etc)?
Source.
You need first need to set up two UdpClients. One client for listening and the other for sending data. (You'll also need to pick a free/unused port number and know the IP address of your target - the machine you want to send data to.)
To set up the receiver,
Instantiate your UdpClient variable with the port number you chose earlier,
Create a new thread to avoid blocking while receiving data,
Loop over the client's receive method for as long as you want to receive data (the loop's execution should be within the new thread),
When you receive one lot of data (called a "packet") you may need to convert the byte array to something more meaningful,
Create a way to exit the loop when you want to finish receiving data.
To set up the sender,
Instantiate your UdpClient variable with the port number you chose earlier (you may want to enable the ability to send broadcast packets. This allows you to send data to all listeners on your LAN),
When you need to transmit data, convert the data to a byte array and then call Send().
I'd suggest that you have a quick skim read through this.
Here's some code to get you started off...
'''''''''''''''''''''''Set up variables''''''''''''''''''''
Private Const port As Integer = 9653 'Port number to send/recieve data on
Private Const broadcastAddress As String = "255.255.255.255" 'Sends data to all LOCAL listening clients, to send data over WAN you'll need to enter a public (external) IP address of the other client
Private receivingClient As UdpClient 'Client for handling incoming data
Private sendingClient As UdpClient 'Client for sending data
Private receivingThread As Thread 'Create a separate thread to listen for incoming data, helps to prevent the form from freezing up
Private closing As Boolean = False 'Used to close clients if form is closing
''''''''''''''''''''Initialize listening & sending subs'''''''''''''''''
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
InitializeSender() 'Initializes startup of sender client
InitializeReceiver() 'Starts listening for incoming data
End Sub
''''''''''''''''''''Setup sender client'''''''''''''''''
Private Sub InitializeSender()
sendingClient = New UdpClient(broadcastAddress, port)
sendingClient.EnableBroadcast = True
End Sub
'''''''''''''''''''''Setup receiving client'''''''''''''
Private Sub InitializeReceiver()
receivingClient = New UdpClient(port)
Dim start As ThreadStart = New ThreadStart(AddressOf Receiver)
receivingThread = New Thread(start)
receivingThread.IsBackground = True
receivingThread.Start()
End Sub
'''''''''''''''''''Send data if send button is clicked'''''''''''''''''''
Private Sub sendBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendBut.Click
Dim toSend As String = tbSend.Text 'tbSend is a textbox, replace it with whatever you want to send as a string
Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)'Convert string to bytes
sendingClient.Send(data, data.Length) 'Send bytes
End Sub
'''''''''''''''''''''Start receiving loop'''''''''''''''''''''''
Private Sub Receiver()
Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, port) 'Listen for incoming data from any IP address on the specified port (I personally select 9653)
While (True) 'Setup an infinite loop
Dim data() As Byte 'Buffer for storing incoming bytes
data = receivingClient.Receive(endPoint) 'Receive incoming bytes
Dim message As String = Encoding.ASCII.GetString(data) 'Convert bytes back to string
If closing = True Then 'Exit sub if form is closing
Exit Sub
End If
End While
End Sub
'''''''''''''''''''Close clients if form closes''''''''''''''''''
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
closing = True 'Tells receiving loop to close
receivingClient.Close()
sendingClient.Close()
End Sub
Here are a few other exmples: Here, here, here and here.
Imports System.Threading
Shared client As UdpClient
Shared receivePoint As IPEndPoint
client = New UdpClient(2828) 'Port
receivePoint = New IPEndPoint(New IPAddress(0), 0)
Dim readThread As Thread = New Thread(New ThreadStart(AddressOf WaitForPackets))
readThread.Start()
Public Shared Sub WaitForPackets()
While True
Dim data As Byte() = client.Receive(receivePoint)
Console.WriteLine("=" + System.Text.Encoding.ASCII.GetString(data))
End While
End Sub

Properly Disconnecting and Reconnecting an Asynchronous Connection

I am currently developing a file sharing application using asynchronous communication in VB.Net. I successfully coded a simple connection between the client and the server. But, I am currently having a bad time doing the disconnection sub routine for both the client and the server. I'm currently stuck in this problem for a couple of days already and have decided to seek help here because I can't find any good ways to do the disconnection thing.
Here's my code:
Server Application
Private Sub FileSharingInitialize()
ServerSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim EndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 7777)
ServerSocket.Bind(EndPoint)
ServerSocket.Listen(0)
End Sub
Private Sub FileSharingOnAccept(ByVal AsyncResult As IAsyncResult)
ClientSocket = ServerSocket.EndAccept(AsyncResult)
FileSharingAddClient(ClientSocket)
ClientSocket.BeginReceive(ByteData, 0, ByteData.Length, SocketFlags.None, New AsyncCallback(AddressOf FileSharingOnReceive), ClientSocket)
End Sub
Delegate Sub _FileSharingAddClient(ByVal Client As Socket)
Private Sub FileSharingAddClient(ByVal Client As Socket)
If InvokeRequired Then
Invoke(New _FileSharingAddClient(AddressOf FileSharingAddClient), Client)
Exit Sub
End If
lvConnectedClients.Items.Add("")
lvConnectedClients.Items(lvConnectedClients.Items.Count - 1).SubItems.Add(Client.LocalEndPoint.ToString)
End Sub
Private Sub FileSharingOnReceive(ByVal AsyncResult As IAsyncResult)
Dim Client As Socket = CType(AsyncResult.AsyncState, Socket)
Client.EndReceive(AsyncResult)
Dim ByteReceived As Byte() = ByteData
Dim LogMessage As String = System.Text.Encoding.ASCII.GetString(ByteReceived)
ReadLogMessage(LogMessage)
End Sub
Delegate Sub _ReadLogMessage(ByVal LogMessage As String)
Private Sub ReadLogMessage(ByVal LogMessage As String)
If InvokeRequired Then
Invoke(New _ReadLogMessage(AddressOf ReadLogMessage), LogMessage)
Exit Sub
End If
DisplayLogMessage(LogMessage)
End Sub
Private Sub DisplayLogMessage(ByVal Log As String)
Dim TimeStamp As String = "[" & DateAndTime.Now.ToString("hh:mm:ss tt dddd, dd MMMM yyyy") & "]: "
Dim LogMessage As String = ""
If txtLog.Text > "" Then
LogMessage = Chr(10) & Chr(13)
End If
LogMessage &= TimeStamp
LogMessage &= Log
txtLog.SelectionStart = Len(txtLog.Text)
txtLog.SelectedText = LogMessage
End Sub
Private Sub frmMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
FileSharingInitialize()
End Sub
Private Sub btnStart_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnStart.MouseClick
ServerSocket.BeginAccept(New AsyncCallback(AddressOf FileSharingOnAccept), Nothing)
DisplayLogMessage("Server started." & Environment.NewLine)
End Sub
On the server part, when I press the stop button, I want to disconnect all the clients from the server. I have tried this code in my start button but it doesn't work. I'm receiving an error that says the socket is not connected. I'm thinking that maybe I am disconnecting the wrong socket since I have also a socket named ClientSocket in the server application. Or maybe, I should do it with a loop to disconnect the clients? What do you think?
Private Sub btnStop_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnStop.MouseClick
ServerSocket.Shutdown(SocketShutdown.Both)
ServerSocket.Close()
ServerSocket = Nothing
End Sub
Client Application
Private Sub btnConnect_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnConnect.MouseClick
ClientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim Address As IPAddress = IPAddress.Parse(frmNetworkSettings.txtServerIPAddress.Text)
Dim Endpoint As IPEndPoint = New IPEndPoint(Address, CInt(frmNetworkSettings.txtFileSharingPort.Text))
ClientSocket.BeginConnect(Endpoint, New AsyncCallback(AddressOf FileSharingOnConnect), Nothing)
SendLogMessage(Username & " is connected." & Environment.NewLine, ClientSocket)
End Sub
Private Sub FileSharingOnConnect(ByVal AsyncResult As IAsyncResult)
ClientSocket.EndConnect(AsyncResult)
End Sub
Private Sub SendLogMessage(ByVal LogMessage As String, ByVal Client As Socket)
Dim SendByteData As Byte() = System.Text.Encoding.ASCII.GetBytes(LogMessage)
Client.BeginSend(SendByteData, 0, SendByteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSendLogMessage), Client)
End Sub
Private Sub OnSendLogMessage(ByVal AsyncResult As IAsyncResult)
Dim Client As Socket = CType(AsyncResult.AsyncState, Socket)
Client.EndSend(AsyncResult)
End Sub
The problem on the client part is the same on the server. The code to disconnect the client from the server doesn't work. It doesn't send any logs to the server after I press the disconnect button, even when I reconnect afterwards. It seems that the socket has been disposed? If so, how can I prevent it from doing that and reuse the socket when I try to reconnect? Another thing, is that when I press the connect button without starting the server and afterwards start the server, a log is sent to the server that the client connected. It seems that the problem is on the listen method of the server. I don't want that to happen, I just want an exception or something that will alert the user that the client is not connected to the server and do not send a log message to the server afterwards the client connects. I don't really know, but do you think that if I set the backlog of the listen method to zero will prevent that from happening?
Thanks in advance!
Better way to do this is to implement flag system. When one client or server want to disconnect from other send disconnect flag to other end so on other end you code will understand disconnection