Socket programming in VB .net 2008
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Socket programming in VB .net 2008

  1. #1
    Join Date
    Aug 2010
    Posts
    50

    Unhappy Socket programming in VB .net 2008

    Hello. I'm trying to make a very simple application that allows the sending of text messages between a server and one or more clients. Firstly i've tried the example from http://www.eggheadcafe.com/articles/20020323.asp with some small changes: i've put a multiline textbox on each form to show the messages written there and i've tried running the client in a different thread.

    If i don't use threading at all, the server form just locks up. If i do use it, i get no message from the client and the server just stays idle at "waiting for connection" but at least it doesn't lock up.

    So basically the server part (without threading) looks like this; this is performed when the Listen button is clicked:

    Code:
            Port = Val(txtPort.Text)
            Dim TcpListener As TcpListener = New TcpListener(IPAddress.Parse("127.0.0.1"), Port)
            TcpListener.Start()
            txtMesaje.Text &= ("Waiting for connection...") + vbCrLf
    
            Try
                'Accept the pending client connection and return 
                'a TcpClient initialized for communication. 
                Dim tcpClient As TcpClient = TcpListener.AcceptTcpClient()
                txtMesaje.Text &= "Connection accepted." + vbCrLf
                ' Get the stream
                Dim networkStream As NetworkStream = tcpClient.GetStream()
                ' Read the stream into a byte array
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                ' Return the data received from the client to the console.
                Dim clientdata As String = Encoding.ASCII.GetString(bytes)
                txtMesaje.Text &= (("Client sent: " + clientdata)) + vbCrLf
                Dim responseString As String = "Connected to server."
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                txtMesaje.Text &= (("Message Sent /> : " + responseString)) + vbCrLf
                'Any communication with the remote client using the TcpClient can go here.
                'Close TcpListener and TcpClient.
                tcpClient.Close()
                TcpListener.Stop()
                txtMesaje.Text &= ("exit") + vbCrLf
                Console.ReadLine()
            Catch ee As Exception
                Console.WriteLine(ee.ToString())
                Console.ReadLine()
            End Try
    When i've used threading, i put the {try..end try} part withing a new private sub and then replaced it in the Listen sub with

    Code:
           Dim t As Thread = New Thread(AddressOf Service)
           t.Start()
    On the client side there is a connect button with the following code(the conditions are there just to check for a proper IP and port):

    Code:
            Dim tcpClient As New System.Net.Sockets.TcpClient()
            If VALIDARE() Then
                IP = txtIP1.Text & "." & txtIP2.Text & "." & txtIP3.Text & "." & txtIP4.Text
                Port = Val(txtPort.Text)
                tcpClient.Connect(IP, Port)
            Else
                MsgBox("nevalidat")
                tcpClient.Close()
                Exit Sub
            End If
    
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then
                ' Do a simple write.
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                ' Read the NetworkStream into a byte buffer.
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                ' Output the data received from the host to the console.
                Dim returndata As String = Encoding.ASCII.GetString(bytes)
                txtMesaje.Text &= (("Host returned: " + returndata)) + vbCrLf
            Else
                If Not networkStream.CanRead Then
                    txtMesaje.Text &= ("cannot not write data to this stream") & vbCrLf
                    tcpClient.Close()
                Else
                    If Not networkStream.CanWrite Then
                        txtMesaje.Text &= ("cannot read data from this stream") & vbCrLf
                        tcpClient.Close()
                    End If
                End If
            End If
    It's clear i'm doing something wrong (or everything) since i cannot send data between the 2 applications (i created them in 2 separate projects that run at the same time on the same computer, for testing purposes). If you could guide me towards a simpler code of sending messages between a client and a server(+ what is required for multiple clients) i'd appreciate it (or some example that is in vb .net 2008 since i've searched google quite a lot and barely found anything clear and simple).

    Thank you!

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,770

    Re: Socket programming in VB .net 2008

    There is a working Peer to Peer Chat application right here on CG ...

    Have a look at Serverless IP Chat application Thread..

    It should give you all you need.....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Aug 2010
    Posts
    50

    Re: Socket programming in VB .net 2008

    Thank you for the link. I've checked it and it seems too complicated for what i need . Looking through the code i can see no explicit "server" form or something similar, but i can use the client part since it includes all the functions i need.

    So could anyone give me some tips on how to use that with a very simple server form that will allow sending messages between each other? Thanks

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,770

    Re: Socket programming in VB .net 2008

    IF you look at the Sample app linked too... Server and client code are almost identical .. One difference is that Server will retransmit all received data to all clients...

    The Actual Sockets and Core Code should be very close to identical for both.. with the Chat code differing on the Server side ..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center