PDA

Click to See Complete Forum and Search --> : [VB6] Multiple UDP Connections


Hellomy
Apr 22nd, 2008, 05:48 PM
I'm using this UDP Server & Client Code. Any way to make it accept multiple connections at once?

CLIENT:
Option Explicit

Private Sub cmdGO_Click()
txtHost.Text = Trim$(txtHost.Text)
txtPort.Text = Trim$(txtPort.Text)

If Len(txtHost.Text) > 0 And Len(txtPort.Text) > 0 And _
IsNumeric(txtPort.Text) And Len(txtData.Text) > 0 Then

sckClient.RemoteHost = txtHost.Text
sckClient.RemotePort = CInt(txtPort.Text)
sckClient.Bind 1024

sckClient.SendData txtData.Text
End If

End Sub


SERVER:

Option Explicit

Private Sub Form_Load()
sckServer.Protocol = sckUDPProtocol
sckServer.LocalPort = 3794
sckServer.Bind 3794
Me.Caption = "Server - OPEN"
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
sckServer.Close
End Sub

Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)
Dim strData As String

sckServer.GetData strData, vbString, bytesTotal

Debug.Print strData

Me.Caption = "DATA RECEIVED"
End Sub

CVMichael
Apr 22nd, 2008, 06:17 PM
There is no such thing as multiple connections when you deal with UDP, since UDP is connection-less.

Just send data to the same port, and make sure you identify each packet properly

Hellomy
Apr 22nd, 2008, 06:32 PM
I don't get how to do it.

EDIT: Do I do like this?
sckServer.RemotePort = 7777
sckServer.RemoteHost = "127.0.0.1"
sckServer.SendData "SOMETHING HERE"

'Then to send to other client

sckServer.RemotePort = 7777
sckServer.RemoteHost = "127.1.2.3"
sckServer.SendData "HJDSH"

Shaggy Hiker
Apr 22nd, 2008, 06:54 PM
I haven't dealt with VB6 UDP in about 10 years, but yes, it should be as simple as that. With UDP, you are just firing the data off into the ether in the hopes that it will arrive at the correct destination. It is much like sending a letter in that you put the right address on it and send it off. If the address is right, and the system is working, the message should get through. On the other hand, if the sytem goes down, or something else happens to disrupt your packet, you'll never know. The advantage to UDP is simplicity and speed, but the cost is uncertainty about receipt.

Genovah
May 4th, 2008, 07:40 AM
With that being said would a person need to incorporate a timer in order to make sure that two packets dont get sent out simultaniously? I set up a timer that waits a half second before it sends another packet is that necessary or not really?

k1ll3rdr4g0n
May 4th, 2008, 12:21 PM
This sounds like you should be using TCP and not UDP. UDP is only used if you don't care if the packet is out of order ect. But, TCP makes sure the packet comes in the right order.

Genovah
May 4th, 2008, 04:52 PM
Ah got it!

Shaggy Hiker
May 5th, 2008, 06:39 PM
A half second delay is a lifetime in UDP. I'm not as sold on TCP based on the original example. UDP is ideal for sending small packets very fast. If the amount of information is larger than the packet size, then you really could have issues, and UDP wouldn't be such a good choice. However, with TCP, packet size isn't an issue, but you have to deal with connections, and it's slower.

There are advantages and disadvantages to both.