聊天系统有一种还是两种方式?

发布于 2024-08-18 04:40:26 字数 4267 浏览 3 评论 0原文

我正在尝试构建一个简单的聊天客户端/软件(全部在可执行文件中),它从端口 5900 开始监听,当客户端连接到该端口时,聊天就会建立。

问题是只有客户端可以与服务器聊天,服务器无法应答客户端,因为连接以一种方式工作。

我尝试在建立连接时从“服务器”连接到客户端,但系统崩溃警告我该端口已在使用中。

这是我的代码:(以一种方式工作)

Imports System.Net.Sockets
Imports System.Text
Imports System.Reflection

Public Class frmComplete
  Dim Data As Integer
  Dim Message As String

  Private sServer As TcpListener
  Private sClient As New TcpClient

  Private cServer As TcpListener
  Private cClient As New TcpClient
  Private cNick As String

  Dim BufferSize(1024) As Byte

  Private Delegate Sub MessageDelegate(ByVal Message As String)

Private Sub frmComplete_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    srvListen(5900)
    btnSend.Enabled = False
End Sub

Private Sub OnServerConnect(ByVal AR As IAsyncResult)
    sClient = sServer.EndAcceptTcpClient(AR)

    sClient.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnRead, Nothing)

    My.Computer.Audio.Play(Application.StartupPath & "\Connected.wav", AudioPlayMode.Background)
End Sub

Private Sub OnRead(ByVal AR As IAsyncResult)
    Data = sClient.GetStream.EndRead(AR)
    Message = Encoding.ASCII.GetString(BufferSize, 0, Data)

    Dim Args As Object() = {Message}
    Me.Invoke(New MessageDelegate(AddressOf PrintMessage), Args)

    sClient.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnRead, Nothing)
End Sub

Private Sub PrintMessage(ByVal Message As String)
    Try
        txtChat.Text = txtChat.Text & Message & vbCrLf
        My.Computer.Audio.Play(Application.StartupPath & "\Message.wav", AudioPlayMode.Background)
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
End Sub

Private Sub srvListen(ByVal port As Integer)
    Try
        sServer = New TcpListener(System.Net.IPAddress.Any, 5900)
        sServer.Start()

        'THIS WILL RAISE THE EVENT WHEN A CLIENT IS CONNECTED
        sServer.BeginAcceptTcpClient(AddressOf OnServerConnect, Nothing)
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
End Sub



Private Sub txtMessage_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMessage.KeyDown
    'FIXME (SOUND T_T)
    If e.KeyCode = Keys.Enter Then
        SendMessage(cNick & ":" & txtMessage.Text)
    End If
End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    ConnectToServer(txtIP.Text)
    cNick = txtNickname.Text

    txtNickname.Enabled = False
    txtIP.Enabled = False
    btnConnect.Enabled = False
End Sub

Private Sub ConnectToServer(ByVal ipadress As String)
    Try
        cClient.BeginConnect(ipadress, 5900, AddressOf OnClientConnect, Nothing)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub OnClientConnect(ByVal AR As IAsyncResult)
    Try
        cClient.EndConnect(AR)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    If Not String.IsNullOrEmpty(txtMessage.Text) Then

        txtChat.Text = txtChat.Text & "Me:" & txtMessage.Text & vbCrLf
        SendMessage(cNick & ":" & txtMessage.Text)

    End If
End Sub

Private Sub SendMessage(ByVal message As String)
    If cClient.Connected = True Then
        Dim Writer As New IO.StreamWriter(cClient.GetStream)
        Writer.Write(message)
        Writer.Flush()
    End If

    txtMessage.Text = ""
End Sub

Private Sub SendCommand(ByVal command As String)
    If cClient.Connected = True Then
        Dim Writer As New IO.StreamWriter(cClient.GetStream)
        Writer.Write(command)
        Writer.Flush()
    End If

    txtMessage.Text = ""
End Sub

Private Sub txtMessage_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMessage.TextChanged
    If Not String.IsNullOrEmpty(txtMessage.Text) Then
        btnSend.Enabled = True
    Else
        btnSend.Enabled = False
    End If
End Sub
End Class

我应该做什么?使用两个端口?一个用于写入,另一个用于读取?如果我需要将多个客户端连接到一个用户? (记住相同的exe是服务器/客户端)

请帮助我=(

I'm trying con build a simple chat client/software (whole in on executable) wich start listen from the start on the port 5900 and when a client connect to that port the chat is established.

The problem is that only the client can chat to the server, the server cannot answer the client because the connection is working in one way.

The i've tried to connect from "server" to the client when it establishes a connection but the system crash warning me that the port is already on use.

This my code: (working in one way)

Imports System.Net.Sockets
Imports System.Text
Imports System.Reflection

Public Class frmComplete
  Dim Data As Integer
  Dim Message As String

  Private sServer As TcpListener
  Private sClient As New TcpClient

  Private cServer As TcpListener
  Private cClient As New TcpClient
  Private cNick As String

  Dim BufferSize(1024) As Byte

  Private Delegate Sub MessageDelegate(ByVal Message As String)

Private Sub frmComplete_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    srvListen(5900)
    btnSend.Enabled = False
End Sub

Private Sub OnServerConnect(ByVal AR As IAsyncResult)
    sClient = sServer.EndAcceptTcpClient(AR)

    sClient.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnRead, Nothing)

    My.Computer.Audio.Play(Application.StartupPath & "\Connected.wav", AudioPlayMode.Background)
End Sub

Private Sub OnRead(ByVal AR As IAsyncResult)
    Data = sClient.GetStream.EndRead(AR)
    Message = Encoding.ASCII.GetString(BufferSize, 0, Data)

    Dim Args As Object() = {Message}
    Me.Invoke(New MessageDelegate(AddressOf PrintMessage), Args)

    sClient.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnRead, Nothing)
End Sub

Private Sub PrintMessage(ByVal Message As String)
    Try
        txtChat.Text = txtChat.Text & Message & vbCrLf
        My.Computer.Audio.Play(Application.StartupPath & "\Message.wav", AudioPlayMode.Background)
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
End Sub

Private Sub srvListen(ByVal port As Integer)
    Try
        sServer = New TcpListener(System.Net.IPAddress.Any, 5900)
        sServer.Start()

        'THIS WILL RAISE THE EVENT WHEN A CLIENT IS CONNECTED
        sServer.BeginAcceptTcpClient(AddressOf OnServerConnect, Nothing)
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
End Sub



Private Sub txtMessage_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMessage.KeyDown
    'FIXME (SOUND T_T)
    If e.KeyCode = Keys.Enter Then
        SendMessage(cNick & ":" & txtMessage.Text)
    End If
End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    ConnectToServer(txtIP.Text)
    cNick = txtNickname.Text

    txtNickname.Enabled = False
    txtIP.Enabled = False
    btnConnect.Enabled = False
End Sub

Private Sub ConnectToServer(ByVal ipadress As String)
    Try
        cClient.BeginConnect(ipadress, 5900, AddressOf OnClientConnect, Nothing)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub OnClientConnect(ByVal AR As IAsyncResult)
    Try
        cClient.EndConnect(AR)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    If Not String.IsNullOrEmpty(txtMessage.Text) Then

        txtChat.Text = txtChat.Text & "Me:" & txtMessage.Text & vbCrLf
        SendMessage(cNick & ":" & txtMessage.Text)

    End If
End Sub

Private Sub SendMessage(ByVal message As String)
    If cClient.Connected = True Then
        Dim Writer As New IO.StreamWriter(cClient.GetStream)
        Writer.Write(message)
        Writer.Flush()
    End If

    txtMessage.Text = ""
End Sub

Private Sub SendCommand(ByVal command As String)
    If cClient.Connected = True Then
        Dim Writer As New IO.StreamWriter(cClient.GetStream)
        Writer.Write(command)
        Writer.Flush()
    End If

    txtMessage.Text = ""
End Sub

Private Sub txtMessage_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMessage.TextChanged
    If Not String.IsNullOrEmpty(txtMessage.Text) Then
        btnSend.Enabled = True
    Else
        btnSend.Enabled = False
    End If
End Sub
End Class

What I should do? use two ports? one for write and another to read? And if i need to conect multiple clients to one user? (remember the same exe is server/client)

Please help me =(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷默言语 2024-08-25 04:40:26

您没有读取从服务器返回的任何数据。您会注意到在 OnServerConnect 方法中调用了 BeginRead——您还需要在 OnClientConnect 方法中为客户端执行此操作,否则您将获得单向通信。也许这就是为什么您看不到任何数据的原因?

我猜,当您的服务器将数据发送回客户端时,您不会收到硬错误,只是没有数据。

只需浏览一下您的代码,我注意到您的客户端和服务器都有一个 TcpClient 和 TcpListener。你不需要这个。您的SERVER将是TcpListener,您的CLIENT将是TcpClient。通过询问是否应该在与服务器不同的端口上进行连接,您就欺骗了自己 TCP 连接的真正含义。一旦您的 TcpClient 连接到 TcpServer,您的连接就建立了。无需进一步尝试连接。

您的客户端代码应该类似于:

Private Sub OnClientConnect(ByVal AR As IAsyncResult) 
    Try 
        cClient.EndConnect(AR)
     sServer.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnClientRead, Nothing)
    Catch ex As Exception 
        MsgBox(ex.Message) 
    End Try 
End Sub 


Private Sub OnClientRead(ByVal AR As IAsyncResult) 
    Data = sServer.GetStream.EndRead(AR) 
    Message = Encoding.ASCII.GetString(BufferSize, 0, Data) 

    Dim Args As Object() = {Message} 
    Me.Invoke(New MessageDelegate(AddressOf PrintMessage), Args) 

    sServer.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnClientRead, Nothing) 
End Sub 

You aren't reading any data coming back from the Server. You'll notice in your OnServerConnect method you call the BeginRead -- you will also need to do this for your client in the OnClientConnect method, or you'll get a one way communication. Perhaps this is why you are not seeing any data coming through?

I'm guessing, when your Server sends back the data to the client, you aren't getting a hard-error, just no data.

Just glancing over your code I noticed that you have both a TcpClient and TcpListener for your client and server. You don't need this. Your SERVER will be the TcpListener, and your CLIENT will be the TcpClient. By asking if you should connect back on a different port from the server, you are shortchanging yourself of what the TCP connection really is. Once your TcpClient has connected to the TcpServer, your connection is established. There is no need further to attempt to connect.

You're client code should be something similar to:

Private Sub OnClientConnect(ByVal AR As IAsyncResult) 
    Try 
        cClient.EndConnect(AR)
     sServer.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnClientRead, Nothing)
    Catch ex As Exception 
        MsgBox(ex.Message) 
    End Try 
End Sub 


Private Sub OnClientRead(ByVal AR As IAsyncResult) 
    Data = sServer.GetStream.EndRead(AR) 
    Message = Encoding.ASCII.GetString(BufferSize, 0, Data) 

    Dim Args As Object() = {Message} 
    Me.Invoke(New MessageDelegate(AddressOf PrintMessage), Args) 

    sServer.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnClientRead, Nothing) 
End Sub 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文