Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
So i'm Stuck.

At the moment the following will only send messages from the Server to the client. I can't figure out how to send a message from the client to the server also.

This needs to be 2 way communication not 1 way.

I know it has something to do with the Listener / Client TCP.
The code is a little messy at the moment.

Could someone please steer me the right way or show me what i'm doing wrong?

CLIENT
----------------------------------------------------------------------------------


VB
Imports System.Threading
Imports System.IO
Imports System.Net
Imports System.Drawing.Color
Imports System.Net.Sockets
Public Class Client
    Dim SvrPort As String = "12345"
    Dim Listener As New TcpListener(12345)
    Dim Client As New TcpClient
    Dim Message As String = ""
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SvrStatusLst.Items.Add("Server Running...")
        SvrPortTxt.Text = SvrPort
        'GetIPAddress()
        Listener.Start()
        Listenertimer.Start()
       

    End Sub


    Private Sub GetIPAddress()

        Dim strHostName As String

        Dim strIPAddress As String



        strHostName = System.Net.Dns.GetHostName()

        strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()


        SvrIpTxt.Text = strIPAddress

    End Sub



    'Start Server stuff
    '------------------------------------------------------------------------------------------------------

    Private Sub Listening()
        Listener.Start()

    End Sub



    Private Sub Clear_Status_List(sender As Object, e As EventArgs) Handles ClearStatusLst.Click
        SvrStatusLst.Items.Clear()
    End Sub

    Private Sub Listener_Timer_Tick(sender As Object, e As EventArgs) Handles Listenertimer.Tick

        If Listener.Pending = True Then
            Message = ""
            Client = Listener.AcceptTcpClient()

            Dim Reader As New StreamReader(Client.GetStream())
            While Reader.Peek > -1
                Message = Message + Convert.ToChar(Reader.Read()).ToString
            End While

            SvrStatusLst.Items.Add(Message)


        End If



    End Sub

    '------------------------------------------------------------------------------------------------------
    'End Server stuff


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles SvrSendToClientBtn.Click


        If txtName.Text = "" Or SvrToClientTxt.Text = "" Then
            'Check to make sure that the user has entered 
            'a display name, and a client IP Address
            'If Not, Show a Message Box
            MessageBox.Show("All Fields must be Filled", _
                            "Error Sending Message", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            Try
                Client = New TcpClient(SvrIpTxt.Text, SvrPort)

                'Declare the Client as an IP Address. 
                'Must be in the Correct form. eg. 000.0.0.0
                Dim Writer As New StreamWriter(Client.GetStream())
                Writer.Write(txtName.Text & ": " & SvrToClientTxt.Text)
                Writer.Flush()

                'Write the Message in the stream

                SvrStatusLst.Text += (txtName.Text & ": " & SvrToClientTxt.Text) + vbCrLf
                SvrToClientTxt.Text = ""

            Catch ex As Exception
                Console.WriteLine(ex)
                Dim Errorresult As String = ex.Message
                MessageBox.Show(Errorresult & vbCrLf & vbCrLf & _
                                "Please Review Client Address", _
                                "Error Sending Message", _
                                MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

    End Sub


    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Listenertimer.Start()
    End Sub
End Class








SERVER
---------------------------------------------------------------------------


VB
Imports System.Threading
Imports System.IO
Imports System.Net
Imports System.Drawing.Color
Imports System.Net.Sockets



Public Class Server
    Dim SvrPort As String = "12345"
    Dim Listener As New TcpListener(12345)
    Dim Client As New TcpClient
    Dim Message As String = ""

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SvrStatusLst.Items.Add("Server Running...")
        SvrPortTxt.Text = SvrPort
        'GetIPAddress()

        
        'Listener.Start()
        'ListenerTimer.Start()

    End Sub

    Private Sub GetIPAddress()

        Dim strHostName As String

        Dim strIPAddress As String



        strHostName = System.Net.Dns.GetHostName()

        strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()


        SvrIpTxt.Text = strIPAddress

    End Sub



    'Start Server stuff
    '------------------------------------------------------------------------------------------------------

    Private Sub Listening()
        Listener.Start()

    End Sub



    Private Sub Clear_Status_List(sender As Object, e As EventArgs) Handles ClearStatusLst.Click
        SvrStatusLst.Items.Clear()
    End Sub

    Private Sub Listener_Timer_Tick(sender As Object, e As EventArgs) Handles ListenerTimer.Tick

        If Listener.Pending = True Then
            Message = ""
            Client = Listener.AcceptTcpClient()

            Dim Reader As New StreamReader(Client.GetStream())
            While Reader.Peek > -1
                Message = Message + Convert.ToChar(Reader.Read()).ToString
            End While

            SvrStatusLst.Items.Add(Message)


        End If



    End Sub

    '------------------------------------------------------------------------------------------------------
    'End Server stuff


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles SvrSendToClientBtn.Click


        If txtName.Text = "" Or SvrIpTxt.Text = "" Then
            'Check to make sure that the user has entered 
            'a display name, and a client IP Address
            'If Not, Show a Message Box
            MessageBox.Show("All Fields must be Filled", _
                            "Error Sending Message", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            Try
                Client = New TcpClient(SvrIpTxt.Text, SvrPort)

                'Declare the Client as an IP Address. 
                'Must be in the Correct form. eg. 000.0.0.0
                Dim Writer As New StreamWriter(Client.GetStream())
                Writer.Write(txtName.Text & ": " & SvrToClientTxt.Text)
                Writer.Flush()

                'Write the Message in the stream

                SvrStatusLst.Text += (txtName.Text & " : balls " & SvrToClientTxt.Text) + vbCrLf
                SvrToClientTxt.Text = ""

            Catch ex As Exception
                Console.WriteLine(ex)
                Dim Errorresult As String = ex.Message
                MessageBox.Show(Errorresult & vbCrLf & vbCrLf & _
                                "Please Review Client Address", _
                                "Error Sending Message", _
                                MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

    End Sub



End Class
Posted

1 solution

There is absolutely no difference in direction of data transfer. You need to just organize your code. No matter how you do it, you always develop some application-level protocol on top of TCP: http://en.wikipedia.org/wiki/Application_layer[^].

In your protocol, define the order in which both client and server switch from reading to writing and visa versa. It could depend on some conditions sent in data, or could be simply taking turns.

Your problem is not the networking, but instruction flow and synchronization. You are trying to listen and accept new connections in a timer event. This is very, very bad. On the server part, you need at least two separate communication threads: one listening and accepting new connections, another one reading/writing from/to network stream. These threads should be synchronized by interlocking of shared data (a collection of the sockets representing remote client).

For some ideas, please see my past answers:
an amateur question in socket programming[^],
Multple clients from same port Number[^].

—SA
 
Share this answer
 

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