[2008] How to establish a connection between two computers?
Hello,
I have a chat application. I want my app be able to create a server, and other people using the same app to be able to join others' servers, after that those two computers (I will at first have only max 2 guys/server) should be able to communicate.
How would I start? I have no clues what so ever.
Thanks for your help! :)
Re: [2008] How to establish a connection between two computers?
Re: [2008] How to establish a connection between two computers?
Quote:
Originally Posted by perito
That's interesting, I'll take a look at it... :)
Re: [2008] How to establish a connection between two computers?
Yeah, you basically need the one hosting the server to listen to a port, then have the other users connect to that the server hosts IP and port.
The TcpListener class provides listening functionallity.
The TcpClient class can initiate communications with listening sockets.
Re: [2008] How to establish a connection between two computers?
I have one problem with that code in http://www.vbforums.com/showthread.php?t=503294
When I very first time start a listen server my firewall blocks the connection and popups "Would you like to allowed blaa blaa". That's ok, but the problem is that if my firewall blocks my program for even a sec, my program will throw an error. =(
How would I modify the server code so that it will wait a sec so that a user can press Allow Connection from firewall ? Or maybe somehow modify it to keep trying to connect without throwing errors.
Re: [2008] How to establish a connection between two computers?
What code are you looking at specifically? Perito's attached code or the code that he's linking to?
Re: [2008] How to establish a connection between two computers?
Quote:
Originally Posted by Atheist
What code are you looking at specifically? Perito's attached code or the code that he's linking to?
In this page http://www.vbforums.com/showthread.php?t=503294
The two attached codes, I tried them and they work sweet. The only problem is that when I first time start the app my firewall blocks it initially and that causes the program to throw an error. The second time firewall acceps the connection and no errors occurs anymore.
Re: [2008] How to establish a connection between two computers?
And it is the server application causing this? Does it occur when you click the "Listen" button?
Re: [2008] How to establish a connection between two computers?
Quote:
Originally Posted by Atheist
And it is the server application causing this? Does it occur when you click the "Listen" button?
Yes! Exactly. When I click listen my fw temporarily blocks the connection and ask me what to do, at this point my app crashes and I press Accept Connection. After I open app again and press Listen it connects successfully.
Re: [2008] How to establish a connection between two computers?
Okay everything is working great except I have slight problems...
Code:
Error 3 'GetStream' is not a member of 'Chatter.Client'. C:\Documents and Settings\Kai\My Documents\Visual Studio 2008\Projects\Chatter\Chatter\Private.vb 9 38 Chatter
This is the error I keep getting.
I have a main form which is the client. In client I have a button "Open private chat window" which opens a private chat window. I have a private.vb form similar to my main chat window. The only problem is the above, my private.vb seems not to be able to use the connection code anymore, so:
How to make a new form/window to use existing connection used by main.vb?
Any help is much appreciated!
Re: [2008] How to establish a connection between two computers?
How does your code look right now?
Re: [2008] How to establish a connection between two computers?
Quote:
Originally Posted by Atheist
How does your code look right now?
- I have ConnectedClient.vb that has the ConnectedClient class.
- Server.vb is the form and the code for the server window
- Client.vb is the client form and code
- PrivateChat.vb is a private window form that tries to send message like Client.vb does, but only client.vb is able to do it, somehow it does not carry its codes over to PrivateChat.vb and I this is the code in PrivateChat (Cleaned out of buttons and useless code for you):
Code:
Imports System.Text
Imports System.Net
Imports System.IO
Public Class PrivateChat
Public Sub SendMessage(ByVal Msg As String)
Dim sw As IO.StreamWriter
Try
sw = New IO.StreamWriter(Client.GetStream)
sw.Write(Msg)
sw.Flush()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendMessage("test")
End Sub
End Class
And it tells:
Code:
Error 3 'GetStream' is not a member of 'Chatter.Client'. C:\Documents and Settings\Kai\My Documents\Visual Studio 2008\Projects\Chatter\Chatter\PrivateChat.vb 9 38 Chatter
Any ideas?
Re: [2008] How to establish a connection between two computers?
Well, I'd suggest that you create a constructor in PrivateChat that takes a ConnectedClient argument. Then, upon creating a new PrivateChat form instance, you could simply pass a ConnecteClient instance to it and use that for communication.