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
----------------------------------------------------------------------------------
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
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
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
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles SvrSendToClientBtn.Click
If txtName.Text = "" Or SvrToClientTxt.Text = "" Then
MessageBox.Show("All Fields must be Filled", _
"Error Sending Message", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Try
Client = New TcpClient(SvrIpTxt.Text, SvrPort)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(txtName.Text & ": " & SvrToClientTxt.Text)
Writer.Flush()
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
---------------------------------------------------------------------------
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
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
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
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles SvrSendToClientBtn.Click
If txtName.Text = "" Or SvrIpTxt.Text = "" Then
MessageBox.Show("All Fields must be Filled", _
"Error Sending Message", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Try
Client = New TcpClient(SvrIpTxt.Text, SvrPort)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(txtName.Text & ": " & SvrToClientTxt.Text)
Writer.Flush()
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