1

I started learning about TCP/Sockets yesterday and decided to make a chatbox for a friend and I.

Unfortunately, i am having some difficulties with MultiThreading.

Whenever i am using it, i can no longer receive messages from my friend.

But, if i disable it then, everything works perfectly.

I don't know what's going on here, could somebody help?

Imports System.Net.Sockets
Imports System.Net

Public Class ServerClient

    Dim _TCPServer As Socket
    Dim _TCPListener As TcpListener

    Dim _ListenerThread As System.Threading.Thread

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

        'When Submit is pressed, send some text to the client

        Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(txtInput.Text)
        txtBox.AppendText(vbCrLf & "Server: " & txtInput.Text)
        txtInput.Clear()
        _TCPServer.Send(bytes)

    End Sub

    Private Sub TCPListen()

        'If somebody calls port 2424, accept it, unblock the socket and start the timer

        _TCPListener = New TcpListener(IPAddress.Any, 2424)
        _TCPListener.Start()
        _TCPServer = _TCPListener.AcceptSocket()
        btnSend.Enabled = True
        txtBox.AppendText("Connection Established" & vbCrLf)
        _TCPServer.Blocking = False
        _Timer.Enabled = True

    End Sub

    Private Sub _Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Timer.Tick

        'If data has been sent, receive it

        Try

            Dim rcvdbytes(_TCPServer.ReceiveBufferSize) As Byte
            _TCPServer.Receive(rcvdbytes)
            txtBox.AppendText(vbCrLf & "Client: " & System.Text.Encoding.ASCII.GetString(rcvdbytes) & vbCrLf)

        Catch ex As Exception
        End Try

    End Sub

    Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Link the new thread to TCPListen(), allow access to all threads and wait for a call

        _ListenerThread = New Threading.Thread(AddressOf TCPListen)
        Control.CheckForIllegalCrossThreadCalls = False

        txtBox.AppendText("Waiting for connection.." & vbCrLf)
        btnSend.Enabled = False
        _ListenerThread.Start()

    End Sub

End Class

2 Answers 2

1

This example project contains four classes - TcpCommServer, TcpCommClient, clsAsyncUnbuffWriter and CpuMonitor. With these classes, you will not only be able to instantly add TCP/IP functionality to your VB.NET applications, but also has most of the bells and whistles we're all looking for. With these classes, you will be able to connect multiple clients to the server on the same port. You will be able to easily: throttle bandwidth to the clients, and send and receive files and data (text?) along 250 provided channels simultaneously on a single connection.

http://www.codeproject.com/Articles/307315/Reusable-multithreaded-tcp-client-and-server-class

Sign up to request clarification or add additional context in comments.

Comments

0

Well, i learned BackgroundWorkers could do the exact same thing and now it all works.

Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Wait for a call

    BackgroundWorker1.RunWorkerAsync()

End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    TCPListen()

End Sub


Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

    _Timer.Enabled = True

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.