I am tryin to make a simple server and client in vb.net that can connect through a port. I ahve looked around on the internet and have become completely confused. Any help is appreciated. I installed the vb6 socket control but i dont think that is the right way.
Here is wat i have so far:
Code:
Public Class Form1
Dim tcpserver As System.Net.Sockets.TcpListener = New System.Net.Sockets.TcpListener(2999)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tcpserver.Server.Listen(5)
End Sub
Private Sub timeraccept_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeraccept.Tick
tcpserver.AcceptSocket()
End Sub
End Class
How do i make an event when a connect attempt is made?
You should be getting a warning about the overload your using to create the TcpListener, you need to declare it like this instead:
VB.Net Code:
Dim tcpserver As System.Net.Sockets.TcpListener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 50023)
And I'd advise you to use a worker thread to handle all listening code. AcceptSocket is a blocking method so the main UI thread will basically stop responding until a connected socket has been returned.
About your question, why do you want to raise an event if a connection has been made?
Since AcceptSocket is a blocking method, a connection will always have been made when the execution reaches a line under AcceptSocket:
VB.Net Code:
Private Sub timeraccept_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeraccept.Tick
tcpserver.AcceptSocket()
MessageBox.Show("Since this line has been executed, a connection has been made!")
You don't. As the documentation states, AcceptSocket and AcceptTcpClient block, which means that they will not return until they receive a connection request. I would suggest placing your call to AcceptSocket or, more likely, AcceptTcpClient, in a background thread. That way you can simply keep calling the method in a loop and, each time a connection request is received you can farm it out as required, either back to the UI thread or off onto a new thread of its own.
Public Class Form1
Dim tcpserver As System.Net.Sockets.TcpListener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 2999)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tcpserver.Start()
tcpaccept.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles tcpaccept.DoWork
MsgBox("")
tcpserver.AcceptTcpClient()
MessageBox.Show("Since this line has been executed, a connection has been made!")
End Sub
End Class
But when i try to make a client to connect to it, I get an error that the server did not respond in time. Here is the client
Code:
Public Class Form1
Dim tcpclient As System.Net.Sockets.TcpClient = New System.Net.Sockets.TcpClient()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tcpclient.Connect(txtip.Text, 2999)
Button1.Enabled = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
tcpclient.Close()
Button1.Enabled = True
End Sub
End Class
Can the client and server both be run from the same computer?
Yes they are both on the same pc. Will this not work? I thought it was similar to how a portscanner can be run on the computer it is scanning. I am putting in my ip address in the client. Could my firewall be messing with the open port?
I think it is the write ip. I got it from http://whatismyip.com/.
Edit: nm i am an idiot and was not using the correct ip, the one from running ipconfig was different adn that one worked. Thanks for your patience.
Well i did some things with the TCP server and clients.
If there is an pending connection you can see that in TCPLISTNER.pending = true.
Very easy to use a timer TICK every 0.1 sec that checks the pending state.
so
if pending = true then
tcplistner.accept()
end if
this is the easy-est way to check incomming connections.
But now you olso wanna do something with that connection.
So you must store the accepted client someone like:
my_tcp_client = tcpListner.accept()
But after this with checking if there is data incomming things will get quite hard. You must do some threading to create data-arrival, dataSended, disconnected events.
And after all you write your own wonderfull network program that will be quite simular to WinSock.
So what i suggest is leave all the tcp-socket stuff and get the winsock.dll.
It provides all you need. Including events and the multi-threading.
I managed once to get a full working multi client program with that system.net.sockets and beleave me. . . if its only for your own use. Use winsock.
Ok i took your advice and got winsock, but i connot figure out how to get the server to accept an incoming connection. Here is what i have so far:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
win.LocalPort = 2999
win.RemotePort = 9999
win.Listen()
End Sub
Private Sub win_ConnectionRequest1(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles win.ConnectionRequest
MsgBox("1")
win.Accept(sender)
MsgBox("2")
End Sub
win.accept(e.client)
winsock just inherits system.net.sockets.
So it will ask a socket to accept wich can be found in the event arguments(e).
You can find the connecting port and ipadress 2.
Thx for the quick reply i think im on the right track now but:
Code:
Private Sub win_ConnectionRequest1(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles win.ConnectionRequest
win.Accept(e.requestID)
MsgBox("2")
End Sub
Why would you want to add extra depencies to your application when everything needed is included in the .Net framework? The user have to install the .Net framework wether you'd like it or not, so not using what its providing is a waste. Besides, you're going to be able to get alot more help from the guys in the .Net section of VBforums if you stay with the standard classes.
The reason why your client couldnt connect to the server is because you gave it the external IP, you have to make sure the needed ports are opened and the firewall properly configured.
If you instead of using the external IP, would have used 127.0.0.1 or "localhost", it would've probably have worked better.
True true is what you say atheist but just copy an .dll to your .exe location works fine.
Btw i added the newest version(altrought it says its for vb.net 2005) i hope it works for 2003.
I work with this one since it came out in april this year and it works fine.
If you want to i can olso post the pure source of the winsock component and then you will se a simiular it is to what a lot of programmers want to write on their own.
Public Class Form1
Private Sub btnmsgbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmsgbox.Click
win2.Send("1")
End Sub
Private Sub btnconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconnect.Click
Dim ip = txtip
MsgBox(win2.RemoteServer)
win2.Close()
win2.Connect(ip, 2999)
btnconnect.Enabled = False
btnmsgbox.Enabled = True
End Sub
Private Sub btndisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisconnect.Click
win2.Close()
btnconnect.Enabled = True
btnmsgbox.Enabled = False
End Sub
Private Sub win_Error(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent)
MsgBox("ERROR")
End Sub
End Class
Everything works fine now, except i cannot assign the ip of the remote server at runtime. If i try to i get an error that says no source code. Is there another way to assign the ip to connect to? If i set the variable "ip" to anyhting i get the same error.
When connecting from one other pc to an other PC you should use your LAN ipaddress. But when you are connecting to your server that is not in your network you should set-up your server on a pc that is open for your port.
When you wanna connect to your pc from the outside you must configure your modem/router to redirect that port you are using to that pc.
That is probably because you cant change the ipaddress after you started listning or after you got connected. You have to close/dispose and =new winsock the control you are using to change the IP you are listning on /connecting 2.
Tip. For testing winsock.send you should not try to send "1". Winsock sends bytes(). Sometimes(dont ask me why or how) it will have problems with sending numbers only. Try a normal string "sdfsdfsdgsdfgf" or something.