Simple chatbox problem

Luc

Well-known member
Joined
Nov 29, 2005
Messages
59
Programming Experience
1-3
I've never really tried programming network and internet stuff with VB.net so I tried writing a simple TCP/IP chatbox, although I'm encountering some problems, since my VS is in dutch I'll try to transalate the error as good as I can. Here's the source:
Main form:
VB.NET:
    Dim localIp As IPAddress
    Public Shared strIP As String
    Public Event Incomming_msg(ByVal msg As String)
    Private WithEvents mlisten As New listen
    Private thrdListener As New Threading.Thread(AddressOf mlisten.listen)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        thrdListener.Start()
        strIP = InputBox("geef de chatter zijn IP in aub")
        localIp = System.Net.IPAddress.Parse(strIP)
    End Sub

    Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
        Dim objClient As New Net.Sockets.TcpClient
        objClient.Connect(IPAddress.Parse(strIP), 6580)
        Dim sw As New StreamWriter(objClient.GetStream)
        sw.Write(TXTsend.Text)
        sw.Flush()
        sw.Close()
        objClient.Close()
    End Sub

    Private Sub incomming_messages(ByVal str As String) Handles mlisten.incomming_msg
        TXTmsg.AppendText(str & vbCrLf)
    End Sub
And the Listen class:

VB.NET:
Imports System.io
Public Class listen
    Event incomming_msg(ByVal msg As String)
    Private objListener As New Net.Sockets.TcpListener(6580)
    Public Sub listen()
        Dim objclient_ As Net.Sockets.TcpClient
        Dim sr As StreamReader
        Dim LocalIP As Net.IPAddress = Net.IPAddress.Parse("127.0.0.1")
        objListener = New Net.Sockets.TcpListener(LocalIP, 6580)
        Dim line$

        Do While 1
            objListener.Start()
            objclient_ = objListener.AcceptTcpClient()
            sr = New StreamReader(objclient_.GetStream)

            While sr.Peek > -1
                line += sr.ReadLine
            End While

            RaiseEvent incomming_msg(line)
            line = String.Empty
        Loop

    End Sub
End Class
The error translated is:
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in system.dll

Additional information: A connectionattempt has failed caused the connected party did not respond after a small time, or the created connection failed cause the connected didn't answer.
the error in dutch is:
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in system.dll

Additional information: Een verbindingspoging is mislukt omdat de verbonden party niet correct heeft geantwoord na een bepaalde tijd, of de gemaakte verbinding is mislukt omdat de verbonden host niet heeft geantwoord
I tested this with a friend of mine and I'm pretty sure he send me the right IP. Both partys have disabled their firewall and I opened the needed ports on my router, but might the error be that he has to open his ports to? If this is so then what port could I use without needing to do forward porting?

Anny help and comment is apreciated thx :)
 
You might be getting that error message because your TCP Client does not connect to the Server unless you push Send. It is a little wired how you have it set up. Every time the Send button is pushed, it will create a new connection, send the texts, and then immediately shut down. If you had an asynchronous routine (BeginAcceptTCPClient) instead of the while loop, you would not have a problem. Take a look at my example below to see what I mean; of course I could be totally wrong about your error message.

My example is written in the .NET 2.0 framework (VS.NET 2005). If you are using an earlier version, let me know and I’ll post the code.
 

Attachments

  • Chat Example.zip
    59.2 KB · Views: 990
anyone can give example client to client chat by modified MasterZero's Source ?

Mistake me if i am wrong people (because i have no knowledge of this subject), but surely a client to client one would just be at each client side it would have a server side part and a client part (obviosuly you will have to modify the code a bit)

Just a thought, using a bit of common sense.
 
Not bad with some common sense :) Socket communication work by client to server connection. For the typical chat there are two options; either all clients connect to and communicate via a common server or else each client must also include server feature making direct client to 'client' (in 'server' mode) connection possible. A setup combining these are also possible, ie clients connects to common server to see who is online and get their IP in order to make direct call to other client.
 
Mistake me if i am wrong people (because i have no knowledge of this subject), but surely a client to client one would just be at each client side it would have a server side part and a client part (obviosuly you will have to modify the code a bit)

Just a thought, using a bit of common sense.
yes just like that i was already did it and works fine only many optimized code,atleast i 've got nice enough mark from my lecturer ;)
 
yes just like that i was already did it and works fine only many optimized code,atleast i 've got nice enough mark from my lecturer ;)

sorry about many optimized code,i was wrong type it because in hurry i mean many unoptimized code,anyway it works so far since i have test it on lan,must be works also on wan since not very much different network topology, i lost the final source which has many basic feature like built-in encryption,save-log,copy log.i will try to find it on my lecturer copy or from my friend,will posted the files tommorow or at monday hopely ...
 
Thanks man :) Don't care about sloppy coding i just wanted to see how you went about doing it, because i am trying and failing myself.
 
Master Zero, if you are still monitoring this post, thank you.

Is there any way you could share a simple way to have the client only? I'm going to use it locally while remotely controlling client PCs. We would be using it to talk to each other on the same PC, so there is no need for TCP support. It would save me from opening notepad as it would be launched at the same time as the remote session.

The problem I'm having is posting TextBox data to the ListBox. If anyone could help with that I would appreciate it.

Thanks!
 
does anyone know how to create the chat program from the above .zip file with the added ability for the server to communicate back with the client? I guess this would be client to client.
 
Tnak

You might be getting that error message because your TCP Client does not connect to the Server unless you push Send. It is a little wired how you have it set up. Every time the Send button is pushed, it will create a new connection, send the texts, and then immediately shut down. If you had an asynchronous routine (BeginAcceptTCPClient) instead of the while loop, you would not have a problem. Take a look at my example below to see what I mean; of course I could be totally wrong about your error message.

My example is written in the .NET 2.0 framework (VS.NET 2005). If you are using an earlier version, let me know and I’ll post the code.

Tnak you ! :idea:
Thank you - and it was a good source:wavey:
 
Back
Top