Ok long time no tutorial and I’ve been looking around and found no Winsock one on this forum! So I was inspired to write one
Ok fundamentals first!
Winsock is a control you can sue to transfer data between computers … simple aye for the purpose of this guide we are o0nly going to use two computers one named Server and one name Client! To start sending messages to each other you must first be connected, this is the general idea….
*Server opens a port and begins to listen
*Client connects to server IP through the same port
*Server accepts connection
*Server and Client are connected and can send data
Ok so you know how it works we will begin some coding
As most if not all Winsock learners create a chat room for their first project… guess what were going to make? Yep you got it a 2-user chat room
SERVER
Ok firstly we will create the server!
We need the following items
1 form
2 text boxes
1 command button
1 label
1 winsock control
Our form is going to be called "frmchat" and the caption set to "Chat (Server)"
To insert the winsock control press ctrl + t then go down and select microsoft winsock control! Apply one to your form and rename it to "winsock"
textbox 1 is going to be our main chat section so this is where we will see all the chat messages the properties are below!
The command button is going to be used to click and send the data to the other users the properties are below!
Name = cmdSend
Caption = "Send"
And finally we have the label which is just basicly to remind you that this is the server so just set its caption propertie to "server"
Screen shot below
(this is how it should look!)
screen shot attached
ok you need to create the exact same thing for the client in a new project just change the words from server to client, and save it we will use the client later
Coding
Ok lets get some coding done
Lets satrt with
VB Code:
Option explicit ' as all good coders do
ok because we are coding the server we need to ‘listen’ for incoming connections so we must start the server by doin this….
VB Code:
Private Sub form_Load()
winsock.localport = 15151'open the port on the local machine try to use ports between 3000 and 50000
winsock.listen ' start listening, simple!
End sub
ok so now your server is listening we must hadle ‘connection requests this is when the client trys to connect to us!
VB Code:
Private Sub Winscock_ConnectionRequest(byVal RequestID as long)
winsock.close 'this resets our socket ready to accept the incoming connection
winscok.accept requestID 'accept the connection!
End sub
Ok so the connection ahs been handled and we are connected to the client! But what about data arriving? Yep you guessed it the Winsock data arrival sub!
VB Code:
Private Sub winsock_DataArrival(byval bytesTotal as long)
dim incommingData as string 'this stores the data we receive
winsock.getdata incommingData ' this stores the data in the variable we set above
txtwindow.selstart = len(txtwindow.text) 'this just positions the text box to focus on the newest piece of information!
End sub
the final thing we need for the server is so it can send data back to the client for this we will use the cmdSend_click sub!
VB Code:
Private Sub cmdSend_click()
Winsock.senddata txtChatBox.text ' sends the data in the textbox
txtwindow.text = txtwindow.text & vbnewline & txtchatbox.text 'displays data
txtwindow.selstart = len(txtmain.text) ' this just positions the text box to focus on the newest piece of information!
Txtchatbox.text = "" ' just empties the text box
End sub
And there you have it the server is complete… you could include error handling and more complex stuff (might add additional guides in the future)!
Now we must complete the client! As I said before we will create the client to look exactly the same as the server just change the names around!
This time we must connect to the server!
VB Code:
Option Explicit
Private Sub Form_Load()
Winsock.RemoteHost = "127.0.0.1" ' this is the Ip of the server 127.0.0.1 loops back to your own computer
Winsock.RemotePort = 15151 ' this is the port it must be the same as the server
Winsock.Connect ' connect!
End Sub
after that the dataarrival and sending of data is the same as with the server! But we do have another sub you can use!
VB Code:
Public Sub winsock_connect()
'this sub tells us that we are coinnected to the server and we can alert the user
msgbox " we are connected "
end sub
This is just the bare minimal you need to set up client/server relationship and you can advance it in many ways, for example I have no form of error handling, you can insert error handling! ! I hope this has helped you!
Run-time error '40006'
Wrong protocol or connection state for the requested transaction or request.
at:
VB Code:
Winsock.SendData txtchatbox.Text
i get that on both client comp and server comp (they are side by side, same network, and yes i got the confirmation msg box, meaning i do have the local ip's right)
ok due to popular demand i'll get the source code up later on today, i've lost the origional through a reboot, and i'm workign today i'll do my best to get it online tonite