TCP Problem

vbBart

Member
Joined
Feb 27, 2008
Messages
16
Programming Experience
1-3
Hi all,

I have problems with my TCP script.

I can connect to the server but while I want to send text to the server, the server don't receive it. See below my classes that I use.

Server Code (Listening)
VB.NET:
    Sub listen()
        Dim LISTENING As Boolean
        Dim localhostAddress As IPAddress = Net.IPAddress.Parse(ipAddress.ToString)
        Dim port As Integer = 4001     '' PORT ADDRESS
        ''''''''''' making socket tcpList ''''''''''''''''
        Dim tcpList As New TcpListener(ipAddress, port)
        Try
            tcpList.Start()
            LISTENING = True
            Do While LISTENING
                Do While tcpList.Pending = False And LISTENING = True
                    Thread.Sleep(10)
                Loop
                Dim tcpCli As TcpClient = tcpList.AcceptTcpClient()
                Dim ns As NetworkStream = tcpCli.GetStream()
                Dim sr As New StreamReader(ns)
                ''''''''' get data from client '''''''''''''''
                Dim receivedData As String = sr.ReadLine()
                MsgBox(sr.ReadToEnd.ToString)
                If receivedData = "###CONNECT_V1.01_$DYA###" Then
                    ListView1.Items.Add(receivedData)
                End If
                Dim returnedData As String = "###OK###" '& " From Server"
                Dim sw As New StreamWriter(ns)
                sw.WriteLine(returnedData)
                sw.Flush()
                sr.Close()
                sw.Close()
            Loop
            tcpList.Stop()
        Catch ex As Exception
            MsgBox(ex.Message)
            LISTENING = False
        End Try
    End Sub

Client Code (Connect to server and send data)
VB.NET:
    Public Sub s()
        Dim host As String = TextBox1.Text
        Dim port As Integer = 4001
        Dim tcpCli As New TcpClient()
        tcpCli.Connect(host, port)
        Dim ns As NetworkStream = tcpCli.GetStream()
        'Dim sw As New StreamWriter(ns)
        Dim sendBytes As Byte() = Encoding.ASCII.GetBytes("###CONNECT_V1.01_$DYA###")
        Dim bytes1() As Byte = encoding.ascII.getbytes("###CONNECT_V1.01_$DYA###")
        ns.Write(sendBytes, 0, sendBytes.Length)
        ns.Close()

        Dim sr As New StreamReader(ns)
        Dim result As String = sr.ReadLine()
        If result = "###OK###" Then
            TextBox1.Enabled = False
            Button1.Enabled = False
            Me.ShowInTaskbar = False
            Me.Visible = False
        ElseIf result = "###EXIT_APP###" Then
            Dim pro As New Process
            pro.GetProcessesByName(result)
            pro.Kill()
        ElseIf result = "###EXIT_WINDOWS###" Then
            System.Diagnostics.Process.Start("Shutdown", "/s")
        End If
        sr.Close()
    End Sub

Thanks.

Bart

PS sorry for my bad English, I'm dutch.
 
One thing I can see is you close the networkstream (ns.close) in the middle somewhere there, but looks like you're still not finished.
 
Back
Top