dimanche 1 juillet 2018

VB.NET If statement not working with sockets

I have a simple client socket program set up to connect to a python server over a network. The program is able to send data to the server and receive a reply to display but the data from the reply does not work inside an if statement so it is useless. Here is some of the code:

Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream

Function Log(Entry) As Single
    LBLog.Items.Add(Entry)
    LBLog.TopIndex = LBLog.Items.Count - 1
End Function

Function Send(Data) As Single
    Dim serverStream As NetworkStream = clientSocket.GetStream()
    Dim outStream As Byte() = Encoding.ASCII.GetBytes(Data)
    serverStream.Write(outStream, 0, outStream.Length)
    Log("S: " + Data)



    Dim inStream(1024) As Byte
    serverStream.Read(inStream, 0, 1024)
    Dim rData As String
    rData = Encoding.ASCII.GetString(inStream)
    Log("R: " + rData)

    If rData = "CONN SUCC" Then
        Log("Connection Test Succesful")
    End If
End Function

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TBIP.Text = My.Settings.LastIP
    TBPort.Text = My.Settings.LastPort
    Log("Program Started")
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles BConnect.Click
    Log("Connecting...")
    clientSocket.Connect(TBIP.Text, TBPort.Text)
    Log("Connected")
    Log("Testing Connection")
    Log("Starting Stream")
    Send("CONN TEST")
    My.Settings.LastIP = TBIP.Text
    My.Settings.LastPort = TBPort.Text
End Sub

I have set up the python server to reply with "CONN SUCC" whenever it receives "CONN TEST". The VB program is able to store the reply in a variable called 'rData' but the if statement is never run even though the data is correct.

If rData = "CONN SUCC" Then
    Log("Connection Test Succesful")
End If

Aucun commentaire:

Enregistrer un commentaire