Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 547047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:55:52+00:00 2026-05-13T10:55:52+00:00

I’m trying con build a simple chat client/software (whole in on executable) wich start

  • 0

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 =(

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:55:52+00:00Added an answer on May 13, 2026 at 10:55 am

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 301k
  • Answers 301k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ok, so the solution ended up being fairly similar to… May 13, 2026 at 8:14 pm
  • Editorial Team
    Editorial Team added an answer Yes, you can change the contents of a static control… May 13, 2026 at 8:14 pm
  • Editorial Team
    Editorial Team added an answer maybe you can read the namespace references out and try… May 13, 2026 at 8:14 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.