HI,
I am creating a client server application in VB.NET 3.5 with WPF. I have searched a lot for a week but I didnt find any helpful source to study. After searching everywhere I am not able to move ahead. Can anybody please provide me small example, any link? I just need to pass few strings to client application.
If its not appropriate question please tell me so I can rephrase it with more details.

Edited 4 Years Ago by Sahil89

I dont work with VB but i tryed it ;p

^ There is an problem with Buffers, i dont know how to make it in Visual Basic to find size of message
^ So i added buffer size 150 , i dont know why in Visual Basic if i add higer byte size than message it let a big space in screen :/ .. check it

YG: i no have idea how arrays work in VB :/

soz for my english ...

TCPClient

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

Module Module1

    Sub Main()
        StartClient("127.0.0.1", 12345)
    End Sub

    Dim socket As Socket
    Dim host As IPEndPoint
    Dim client As System.Net.Sockets.TcpClient

    Sub StartClient(ByVal ip As String, ByVal port As Integer)
        socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        host = New IPEndPoint(IPAddress.Parse(ip), port)
        client = New System.Net.Sockets.TcpClient()

        Try
            Console.WriteLine("Connecting...")
            client.Connect(host)

        Catch ex As Exception
            Console.WriteLine("Connecting To {0} at Port {1} has failed!", ip, port)
        End Try

        ' If program arrive here means is connted, if connection
        ' upper has failed then program will stop because of (Catch)
        Console.WriteLine("Write your message.")
        Console.WriteLine("---------------------------------")

        Dim NStream As NetworkStream = client.GetStream()
        Try
            While (client.Connected)

                ' Reda message from user
                Dim message As String = Console.ReadLine()

                ' Get Date
                Dim _Date As String = DateTime.Now.ToString("HH:mm:ss: ")

                Dim _ASCII As New ASCIIEncoding()
                Dim _Buff(150) As Byte

                ' Get bytes of message you type
                _Buff = _ASCII.GetBytes(_Date + message)

                ' Send message to Server
                ' You send bytes of messages (_Buff)
                NStream.Write(_Buff, 0, _Buff.Length)

            End While

        Catch ex As Exception
            Console.WriteLine("You have been disconected.")
            System.Threading.Thread.Sleep(2000)
        End Try
    End Sub

End Module

TCPServer

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

Module Module1

    Sub Main()
        Start()
    End Sub

    Dim socket As Socket
    Dim listener As TcpListener
    Dim Port As String

    Sub Start()
        ' Port Selection
        Console.WriteLine("Starting Server...")
        Console.Write("Select Port: ")
        Port = Console.ReadLine()

        listener = New TcpListener(IPAddress.Any, Convert.ToInt32(Port))
        ' Start Server
        listener.Start()

        Console.WriteLine("Server is listening at Port " + Port)

        Try

            ' Accept in-come requests
            Dim Client As System.Net.Sockets.TcpClient = listener.AcceptTcpClient()
            Console.WriteLine("Connection Accepted!")
            Console.WriteLine("---------------------------------")

            Dim Buffer(150) As Byte
            While (True)

                ' Get the stream
                Dim NStream As NetworkStream = Client.GetStream()

                NStream.Read(Buffer, 0, Buffer.Length)

                Dim CData As String = Encoding.ASCII.GetString(Buffer)

                Console.WriteLine(CData)

            End While

            ' Close Connection
            Client.Close()
            listener.Stop()

        Catch ex As Exception
            Console.WriteLine("Connection has been closed!")
        End Try

    End Sub


End Module

Edited 4 Years Ago by Panathinaikos22

Hi Panathinaikos22,
Thanks a lot for your help, thats what I needed for my project. I searched a lot but finally your example helps me.

Do you want for C# and then try convert it?

You can post your C# code if you want, just provide this link http://converter.telerik.com/ or just convert from here and then post :)

Thanks again...

There is one problem,
if I send shorter message than the pevious one then shorter msg gets appended with rest of longer message.
example
first message hello, second message hi
then output for second message is hillo. Well its not big deal I can cover this part by making my each string of same length.

Now what I want to know is, how can I code for multiple clients with single server? And how can server send back reply to client?

I little confused with that, well this is a new version in C#

About spaces in screen, i also got this problem in C#, only way to solve it is to use this code

            while (true)
            {
                int ReceivedMessage = _Socket.Receive(Buff);
                for (int i = 0; i < ReceivedMessage; i++)
                {   // Show received message
                    Console.Write(Convert.ToChar(Buff[i]));
                }
                Console.Write("\n");
            }

TCPServer
http://pastebin.com/NE8hBQP4

TCPClient
http://pastebin.com/94nwY7DE

Edited 4 Years Ago by Panathinaikos22

This question has already been answered. Start a new discussion instead.