שתף באמצעות


TCP Socket Listen and allow multiple connections

Question

Saturday, January 25, 2014 11:30 PM

Hello,

Hope someone can help me with a TCP Listen socket example.

What I am trying to do is in VB.net create a socket that will listen for commands. This port will need to allow multiple connections all at the same time from different IP address.

I have so far created the following (which I don't know if I am 100% correct or not):

Imports System.Net.Sockets
Imports System.Net

Public Class Form1
    Private tcpListener As System.Net.Sockets.TcpListener

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim serverSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

        serverSocket.Bind(New IPEndPoint(IPAddress.Any, 7878))
        serverSocket.Listen(1024)
        serverSocket.Accept()

    End Sub

    Private Sub NewData(ByVal data As String)

    End Sub
End Class

So far it looks like it's opened the port 7878 (not sure if this will allow multiple connections at the same time?) but I don't know where to go from here to be able to read incoming data into the NewData sub and how to then send data back to the same connection that sent data to the server application.

How can I send data based on the connection to just 1 IP address.

Lets say the IP address of the server (running the above code) is 192.168.0.2 and I have a another computer connecting to this server and it's IP is 192.168.0.3.

Now if I have another computer with the IP of 192.168.0.4 and it connects to the server (192.168.0.2) and this computer sends a ASCII message "Test". How can I then receive that message in the NewData sub in the server application and know it come from 192.168.0.4 and allowing the server application to only reply back to this connection and not the 192.168.0.3 computer?

Anyone able to help me with this?

All replies (2)

Sunday, January 26, 2014 8:06 AM ✅Answered

Hi, the Accept function will return the connected socket, you can use it to receive and send data.

Dim socket = serverSocket.Accept()

get IP of connected client:

socket.RemoteEndPoint

Receive data from connected client:

socket.Receive(...)

send data to  connected client:

socket.Send(...)

Below link is the detail example, hope it can help you:

http://msdn.microsoft.com/en-us/library/w89fhyex(v=vs.110).aspx


Sunday, January 26, 2014 9:03 AM ✅Answered

The listen class sample is shown on the link that lapheal provided on the following webpage

http://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx

The code like yours is using the loopback address 127.0.0.1 (IPany) as the IP address for your listener.  The computer IP address like 192.0.0.1 forward all incoming IP to the loopback IP address.  Your code registers the port number 7878 and the windows operating system forwards all incoming messages with this port towards your application to the Listen(1024) which will accept up to 1024 clients.  The source code of each message is then examined and send to a socket that contains the source IP address.  When a new connection is detected the datagram is sent to the socket accept event where your code (or the default socket accept) creates a new socket.

jdweng