Click here to Skip to main content
15,881,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So i was wondering why my messages only appears when i have sended the next message

as example:
Sended: Hello Shows:
Sended: 2nd message Shows: Hello
Sended: NICE! Shows: 2nd Message

Heres My TCPControl Class:
VB
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Public Class TCPControl
    Public Event MessageReceived(sender As TCPControl, Data As String)

    ' SERVER CONFIG
    Public ServerIP As IPAddress = IPAddress.Parse(Form1.TextBox1.Text)
    Public ServerPort As Integer = Form1.TextBox2.Text
    Public Server As TcpListener

    Private CommThread As Thread
    Public IsListening As Boolean = True

    ' CLIENTS
    Private Client As TcpClient
    Private ClientData As StreamReader

    Public Sub New()
        Server = New TcpListener(ServerIP, ServerPort)
        Server.Start()

        CommThread = New Thread(New ThreadStart(AddressOf Listening))
        CommThread.Start()
    End Sub

    Private Sub Listening()
        ' CREATE LISTENER LOOP
        Do Until IsListening = False
            ' ACCEPT INCOMING CONNECTIONS
            If Server.Pending = True Then
                Client = Server.AcceptTcpClient
                ClientData = New StreamReader(Client.GetStream)

            End If

            ' RAISE EVENT FOR INCOMING MESSAGES
            Try
                RaiseEvent MessageReceived(Me, ClientData.ReadLine)
            Catch ex As Exception

            End Try

            ' REDUCE CPU USAGE
            Thread.Sleep(100)
        Loop
    End Sub
End Class


My Form1:

VB
Imports System.Net

Public Class Form1
    Private Server As TCPControl

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Server.IsListening = False
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Server = New TCPControl
        'txtChat.Text = ":: SERVER STARTED ::" & vbCrLf

        'AddHandler Server.MessageReceived, AddressOf OnLineReceived

        Dim IPList As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)

        For Each IPaddress In IPList.AddressList
            'Only return IPv4 routable IPs
            TextBox1.Text = IPaddress.ToString
        Next
    End Sub

    ' ALLOW THREAD TO COMMUNICATE WITH FORM CONTROL
    Private Delegate Sub UpdateTextDelegate(TB As TextBox, txt As String)

    ' UPDATE TEXTBOX
    Private Sub UpdateText(TB As TextBox, txt As String)
        If TB.InvokeRequired Then
            TB.Invoke(New UpdateTextDelegate(AddressOf UpdateText), New Object() {TB, txt})
        Else
            If txt IsNot Nothing Then TB.AppendText(txt & vbCrLf)
        End If
    End Sub

    ' UPDATE TEXT WHEN DATA IS RECEIVED
    Private Sub OnLineReceived(sender As TCPControl, Data As String)
        UpdateText(txtChat, Data)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Server = New TCPControl
        txtChat.Text = ":: SERVER STARTED ::" & vbCrLf

        AddHandler Server.MessageReceived, AddressOf OnLineReceived
    End Sub
End Class
Posted
Comments
[no name] 2-Nov-14 11:01am    
Problem appears to be in your client and how you handle messages on your server class also. The delay I think is in your client class.
RaiseEvent MessageReceived(Me, ClientData.ReadLine)

Assuming your client is sending data on the first request, your server should be receiving it. I am not seeing anything obvious at a glance, however, ReadToEnd would be more advised than just ReadLine. Or better yet; ReadBlock since this will allow you to count how many bytes you are receiving per string. (Which is a valuable part of any server communication platform.) You can find the documentation on that here.

A look at your client class might reveal something more, can you update your question to include that code?
ramon.robben 4-Nov-14 5:27am    
Yeah my client is a TCP Client app i downloaded it from the play store.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900