2010-01-15 4 views
2

저는 간단한 채팅 클라이언트/소프트웨어 (전체 실행 파일)를 빌드하려고합니다. 시작은 포트 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 

(편도에서 작업)? 두 개의 포트를 사용합니까? 하나는 쓰기 용이고 다른 하나는 읽을 용? 그리고 만약 내가 하나의 사용자에게 여러 클라이언트를 연결해야합니까? 당신은 다시 서버에서 오는 데이터를 읽을되지 않습니다 (=

이 제발 도와주세요

답변

1

을 (같은 EXE 서버/클라이언트 기억) 당신은 당신이 그러면 BeginRead를 호출하여 OnServerConnect 방법에 알 수 있습니다. - 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 
+0

그것은 잘 작동을하지만, intead도록 sServer 클라이언트는 다음과 같습니다 P –

+0

그래서 문제 것을이었다? –

+0

클라이언트가 서버를 청취하지 못하여 편도 채팅이었습니다. –

관련 문제