双向客户端和服务器?

时间:2015-03-21 17:36:14

标签: vb.net

我创建了单向客户端服务器通信,以便客户端可以将消息发送到刚收到它们的服务器。如何修改代码,以便服务器和客户端都可以发送和接收消息。

客户代码:

TCPControl.vb

Imports System.Net
Imports System.Net.Sockets
Imports System.IO

Public Class TCPControl
    Public Client As TcpClient
    Public DataStream As StreamWriter

    Public Sub New(Host As String, Port As Integer)
        ' CLIENT
        Client = New TcpClient(Host, Port)
        DataStream = New StreamWriter(Client.GetStream)
    End Sub

    Public Sub Send(Data As String)
        DataStream.Write(Data & vbCrLf)
        DataStream.Flush()
    End Sub
End Class

Form1.vb的

Public Class Form1
    Private Client As TCPControl

Private Sub cmdSend_Click(sender As System.Object, e As System.EventArgs) Handles cmdSend.Click
    SendMessage()
    txtMessage.Clear()
    txtMessage.Focus()
End Sub

Private Sub cmdConnect_Click(sender As System.Object, e As System.EventArgs) Handles cmdConnect.Click
    Client = New TCPControl("10.0.0.253", 64555)
    If Client.Client.Connected Then cmdConnect.Text = "Connected"
End Sub

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If Client.Client.Connected = True Then
        Client.DataStream.Close()
        Client.Client.Close()
    End If
End Sub

Private Sub txtMessage_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMessage.KeyDown
    If e.KeyCode = Keys.Enter Then SendMessage()
End Sub

Private Sub SendMessage()
    If Client.Client.Connected = True Then Client.Send(txtMessage.Text)
End Sub
End Class

服务器代码:

TCPControl.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("10.0.0.253")
Public ServerPort As Integer = 64555
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



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

TCPControl.vb

   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
    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
End Class

0 个答案:

没有答案