Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help! TCP Communications

Posted on 2005-05-10
15
Medium Priority
?
399 Views
Last Modified: 2012-05-05
Friends,

I have a TCP Client, in one project, that connects to a specific IP address and port and "listens" for data.  Now I need some kind of class or module, to run inside of another vb.net project, that will allow my TCP Client to connect to.  Once the TCP client is connected, it waits until data is recieved from this yet to be determinded server class/module.

Does anybody have leads on such a TCP Server?  Sample code or app?   I need all the help I can get!!!

Thanks to a member of this forum, I was given this code that allows me to broadcast out a specific port, but it doesn't give my Client anything to connect to.

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class SendUpdateCommand

    Public Sub Broadcast()
        Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
        sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1)
        Dim iep As New IPEndPoint(Isloopback.Broadcast, 50089)
        Dim msg As Byte() = Encoding.ASCII.GetBytes("Update")
        sock.SendTo(msg, iep)
        sock.Close()
    End Sub

End Class

Thanks,

Eric
0
Comment
Question by:indy500fan
  • 8
  • 6
15 Comments
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13970507
Try this if you have any question let me know.

http://www.codeproject.com/vb/net/VbNetSendReceiveTcp.asp
0
 

Author Comment

by:indy500fan
ID: 13970598
I'm downloading it now.  I'll be trying it shortly.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13970608
Are you trying to set up a TCP server?  You are sending out UDP packets, which are connection-less.  Here is some code that I have to listen on a specified port:

Imports System.Text.Encoding

Public Class Socket_Library

  Private Class SocketPacket
    Public thisSocket As Socket
    Public dataBuffer As Byte() = New Byte(1024) {}
  End Class

  Private Shared socketListener As Socket
  Private Shared socketWorker As Socket

  Private Const messageToSend As String = "Hello, client!"


  Public Shared Sub ListenToPort(ByVal portNumber As Integer)

    socketListener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Dim ipLocal As IPEndPoint = New IPEndPoint(IPAddress.Any, portNumber)

    socketListener.Bind(ipLocal)
    socketListener.Listen(1)

    socketListener.BeginAccept(New AsyncCallback(AddressOf connectCallbackFunction), Nothing)

  End Sub


  Private Shared Sub connectCallbackFunction(ByVal result As IAsyncResult)

    Try

      socketWorker = socketListener.EndAccept(result)
      WaitForData(socketWorker)
      SendData(socketWorker)

    Catch oEx As ObjectDisposedException
      Console.WriteLine("The connection has been closed.")

    Catch sEx As SocketException
      Console.WriteLine(sEx.Message)

    End Try

  End Sub


  Private Shared Sub SendData(ByVal IOsocket As System.Net.Sockets.Socket)

    Try

      Console.Write("Sending this message: ")
      Console.Write(messageToSend)
      Console.WriteLine("")

      Dim byteData As Byte() = ASCII.GetBytes(messageToSend)

      IOsocket.Send(byteData)

    Catch sEx As SocketException
      Console.WriteLine(sEx.Message)

    End Try
  End Sub


  Private Shared Sub WaitForData(ByVal IOsocket As System.Net.Sockets.Socket)

    Try

      Dim packet As SocketPacket = New SocketPacket

      packet.thisSocket = IOsocket

      IOsocket.BeginReceive(packet.dataBuffer, 0, packet.dataBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf dataReceivedCallbackFunction), packet)

    Catch dataException As SocketException
      Console.WriteLine(dataException.Message)

    End Try

  End Sub


  Private Shared Sub dataReceivedCallbackFunction(ByVal result As IAsyncResult)

    Try

      Dim packet As SocketPacket = CType(result.AsyncState, SocketPacket)

      Dim size As Integer = packet.thisSocket.EndReceive(result)

      Dim buffer(size + 1) As Char
      Dim decoder As System.Text.Decoder = System.Text.Encoding.UTF8.GetDecoder
      Dim numCharacters As Integer = decoder.GetChars(packet.dataBuffer, 0, size, buffer, 0)
      Dim receivedData As System.String = New System.String(buffer)

    Catch oEx As ObjectDisposedException
      Console.WriteLine("The connection has been closed.")

    Catch sEx As SocketException
      Console.WriteLine(sEx.Message)

    End Try

  End Sub

End Class


Bob
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:indy500fan
ID: 13970615
Thanks Bob!  I'm going to try it out!!!
0
 

Author Comment

by:indy500fan
ID: 13970908
Bob,

Help me to understand what is going on here, please...

I created a form (Form1) and it has a button (Button 1)

When I click on on Button 1, it should call the WaitForData sub right?  Where do I assign the IP Address to listen to?

Eric
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13971018
I have this code in my bag of tricks, but haven't completed the code to make it totally useful.  It's question like this that help make it better and better :)


To create a listener on port 6000 from any IP address:
Socket_Library.ListenToPort(6000)

To make this class useful, we'll need to change a few things:

(1) Remove the Shared from all the definitions
(2) Instantiate an instance where you need to use it (i.e. in the form).

     Private WithEvents sockets As New Socket_Library

(3) Change to Public Sub SendData
(4) Add event:

Public Event ReceiveData(ByVal sender As Object, e As SocketEventArgs)  

Public Class SocketEventArgs : Inherit System.EventArgs

   Public Data As String = String.Empty

   Public Sub New(ByVal newData As String)

      Me.Data = newData

   End Sub

End Class

(5) Raise the event in dataReceivedCallbackFunction after receivedData line:

    RaiseEvent DataReceived(New SocketEventArgs(Me, receivedData))

(6) Handle the event:

   Private Sub sockets_DataReceived(ByVal sender As Object, e As SocketEventArgs)

   End Sub

Bob
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 13971060
Imports System.Net
Imports System.Net.Sockets
Imports System.Text.Encoding

Public Class Socket_Library

  Private Class SocketPacket

    Public thisSocket As Socket
    Public dataBuffer As Byte() = New Byte(1024) {}

  End Class 'SocketPacket'


  Public Class SocketEventArgs

    Public Data As String = String.Empty

    Public Sub New(ByVal newData As String)

      Me.Data = newData

    End Sub

  End Class 'SocketEventArgs'

  Private socketListener As Socket
  Private socketWorker As Socket

  Public Event ReceivedData(ByVal sender As Object, ByVal e As SocketEventArgs)


  Public Sub ListenToPort(ByVal portNumber As Integer)

    socketListener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Dim ipLocal As IPEndPoint = New IPEndPoint(IPAddress.Any, portNumber)

    Me.socketListener.Bind(ipLocal)
    Me.socketListener.Listen(1)

    Me.socketListener.BeginAccept(New AsyncCallback(AddressOf connectCallbackFunction), Nothing)

  End Sub 'ListenToPort'


  Private Sub connectCallbackFunction(ByVal result As IAsyncResult)

    Try

      socketWorker = socketListener.EndAccept(result)

      Me.WaitForData(socketWorker)

      Me.SendData(socketWorker, "Connected")

    Catch oEx As ObjectDisposedException
      Console.WriteLine("The connection has been closed.")

    Catch sEx As SocketException
      Console.WriteLine(sEx.Message)

    End Try

  End Sub 'connectCallbackFunction'


  Public Sub SendData(ByVal IOsocket As Socket, ByVal messageToSend As String)

    Try

      Console.Write("Sending this message: ")
      Console.Write(messageToSend)
      Console.WriteLine("")

      Dim byteData As Byte() = ASCII.GetBytes(messageToSend)

      IOsocket.Send(byteData)

    Catch sEx As SocketException
      Console.WriteLine(sEx.Message)

    End Try

  End Sub 'SendData'


  Private Sub WaitForData(ByVal IOsocket As System.Net.Sockets.Socket)

    Try

      Dim packet As SocketPacket = New SocketPacket

      packet.thisSocket = IOsocket

      IOsocket.BeginReceive(packet.dataBuffer, 0, packet.dataBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf dataReceivedCallbackFunction), packet)

    Catch dataException As SocketException

      Console.WriteLine(dataException.Message)

    End Try

  End Sub 'WaitForData'


  Private Sub dataReceivedCallbackFunction(ByVal result As IAsyncResult)

    Try

      Dim packet As SocketPacket = CType(result.AsyncState, SocketPacket)

      Dim size As Integer = packet.thisSocket.EndReceive(result)

      Dim buffer(size + 1) As Char
      Dim decoder As System.Text.Decoder = System.Text.Encoding.UTF8.GetDecoder
      Dim numCharacters As Integer = decoder.GetChars(packet.dataBuffer, 0, size, buffer, 0)
      Dim receivedData As System.String = New System.String(buffer)

      RaiseEvent ReceivedData(Me, New SocketEventArgs(receivedData))

    Catch oEx As ObjectDisposedException

      Console.WriteLine("The connection has been closed.")

    Catch sEx As SocketException

      Console.WriteLine(sEx.Message)

    End Try

  End Sub 'dataReceivedCallbackFunction'

End Class


Bob
0
 

Author Comment

by:indy500fan
ID: 13971082
Thanks for posting the updates.  I started to freak a little bit... :)

Do you know how to get it to listen to a specific IP Address?

Say 10.10.0.42, on port 6000 for example?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13971130
Not exactly, but try this:

  Public Sub ListenToPort(ByVal portNumber As Integer, ByVal addressConnect As String)

    Dim addressSource As IPAddress = IPAddress.Parse(addressConnect)

    socketListener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Dim ipLocal As IPEndPoint = New IPEndPoint(addressSource, portNumber)

    Me.socketListener.Bind(ipLocal)
    Me.socketListener.Listen(1)

    Me.socketListener.BeginAccept(New AsyncCallback(AddressOf connectCallbackFunction), Nothing)

  End Sub 'ListenToPort'


Bob
0
 

Author Comment

by:indy500fan
ID: 13971433
Bob,

I think that'll work.  It's going to take me a while to work through this, but I appreciate your help!

Regards,
Eric.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13977463
Any problems, Eric?  Just checking in :)

Bob
0
 

Author Comment

by:indy500fan
ID: 13977539
Bob,

Yeah, lots of problems, but I know it's my inexperience, it's just taking a lot of time in the implementation.  The reason for the TCP communications is because, I have an operator program and a results program (and they HAVE to be on two different computers).  As the times and speeds are calculated in the operator program, they are inserted/updated in the database.  I wanted to send a command from the operator to the results screen (that looks at the database) to tell the results program to go out and look at the latest results in the database.  I am against a deadline (tomorrow morning), so for now, until I get the TCP communications going I have set a timer to execute a qry every second that, in effect will do the same thing.  So, for now, I am finishing both programs (using the timer for the results), and if I have time before Friday, I will begin working on the TCP stuff again.)   It stinks, but it will have to do if I can't get the TCP stuff working it will have to do.

Thank you sensai!

Eric
0
 

Author Comment

by:indy500fan
ID: 14001069
Bob,

Still working, but I bet it will be wednesday (May 18th) before I can give this my heart to this.  Sorry to let you down.

Eric
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 14009475
You're not letting me down.  If you forget about this it will just be cleaned up later (by me) ;)

Bob
0
 

Author Comment

by:indy500fan
ID: 14024079
Bob,

Don't worry, I haven't forgotten.

Talk to you soon.

Eric
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month20 days, 22 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question