Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
.::Simple Winsock Tutorial ::.
-From the author of 'Simple Direct3d
Tutorial w/ source' on PSC!
This tutorial will teach you how to use the Winsock
control to make chat programs, multiplayer games, email checkers, and anything
else that uses Winsock!
Winsock is a communications 'thingy' that lets you
communicate with other computers via network/internet. Using TCP/IP or UDP
protocols, you can send data from one computer to another. We will be
using the Winsock control with visual basic. First, open up visual basic, then
add the Winsock Control. I also assume that you know what a port is...
Add one to your form. Now, make 2 command buttons. Label one 'Connect' and
the other 'Host'.
Under the 'connect' button, add the following code:
Host = inputbox("Enter the host's computer name
or ip address:")
Port = inputbox("Enter the host's port to connect to:")
Winsock1.connect host, port
That code let you specify the host computers name/ip address, and the port to
connect to on it. Now, add the following code to the 'Host' button.
Port = inputbox("What port do you want to host on?")
Winsock1.localport = Port
If you were to click the 'host' button, it would activate the server and wait
for somebody to try to connect.
Now, double click the Winsock control and go to the sub
'winsock1_ConnectionRequest'. Under that
' , put this code.
if winsock1.state <> sckclosed then winsock1.close 'Got
to do this to make sure the Winsock control isn't already being used.
That code simply let the client connect. Now, add a 3rd button to the form
and call it 'Send'. Under it, add this code.
Text = inputbox("Send what text?")
When you click that, it sends data to the other computer, no matter if you
are the host or the client. You need a way to receive this data on both, so
double click the Winsock control, then go to the 'winsock1_RecieveData' sub (or
something like that). Put in this code.
or is it winsock1.getdata data,vbstring,bytestotal? I think I might be mixing c++ syntax w/ visual basic :-/
This displays the data that the other user sent you.
I guess I really didn't go in depth with this article, but it is all REALLY
simple. Maybe you want to send player coordinates if you are making a
multiplayer game, or send messages if it's a chat program that you want to make.
Doesn't really matter.
To test this program, compile it, then open 2 instances of it. in one, click the
'host' button and enter the port 100 (Ju
' st because. You could do any port you
want (1 to 1000 or higher!))
In the other one, click 'Connect' Then type in 'Localhost' for the server, and
the port should be, guess what, 100!
This will create a loop, and you will connect to your self. On one of the
instances, click send, then type in a message. No, don't just type in any message,
type 'Hello World!'. How original is that? Click OK, then a message box from the
other instance should pop up with the same message. You could also use 127.0.0.1
for the host instead of Localhost, because that is the same thing as Localhost,
only in an IP address.
I guess this doesn't TEACH you how to make multiplayer games, but at least you
know how to now...
That was a real simple tutorial. Not much else to say, except, Please vote for
me if you liked this!
Other 20 submission(s) by this author
|Report Bad Submission|
See Voting Log |
|Other User Comments|
|1/6/2002 3:59:18 PM:Peter Schmitz|
Actually, I'd recommend against using
any port under 2000 or so, because of
other services commonly running on
those ports... If you want to be safe,
and make sure that the most default
portscans won't pick up your open port,
chose a port over 5000.
|1/7/2002 3:43:37 AM:J Pack|
Thanks for your short but to the point
Tutorial. I gave you a 5 rating but I
did have to make a small change to get
it to work properly. I changed your
line of code "Data = winsock1.getdata"
to "Winsock1.GetData Data". It works
|1/7/2002 7:10:20 PM:dumbass|
I had to do the same as the person
change "Data =
winsock1.getdata" to "Winsock1.GetData
|1/16/2002 10:05:52 AM:d3|
I thought this tutorial is short but
sweet, it told me everything I needed
to know :)
|1/16/2002 12:04:34 PM:d3|
ok I thought this worked... I don`t
know what it is but winsock wont work
for me.... here is my code please can
Port = InputBox(
|1/22/2002 1:12:07 PM:Cessna 182|
Well this works fine other than that
you must use: winsock1.GetData Data,
vbString, bytesTotal because it
will just give you a message box saying
"???" if you dont
|1/22/2002 1:17:31 PM:Guato|
I got everything to work, but all I get
in the msgbox is a question mark for
every two characters sent. Anyone know
|1/22/2002 1:19:03 PM:guato|
haha, whoops, during my registration
you answered my question before I
posted it... erie...
|1/25/2002 1:23:34 AM:Dan Wold|
"ok I thought this worked... I
don`t know what it is but winsock wont
work for me.... here is my code please
can someone help!! Private Sub
Form_Click() Port = InputBox(
from what i Pieced together from
your message, I beleive this is what
InputBox("Please enter the port.","Port
If not feel free To email me!
|1/29/2002 12:05:45 PM:Maniac|
Interesting tutorial. It all seems so
|2/2/2002 10:07:27 PM:angelo|
i've been at this winsock nonsense on
and off for what seams like years now
and i STILL haven't gotten the
basics...so here's my latest
problem...i have my program set up to
simply MSGBOX me when i connect to it
through IE on the designated port...i
used Matt's code in listening,
accepting RequestID, and
DataArrival...however, i noticed in the
accepting the RequestID it tells the
winsock to close the connection:
wskMain.State <> sckClosed Then
basically the program
works the first time around and a
messagebox pops up showing me the IE
http header info...but then it won't
work again...i'm thinking it's due to
the fact i'm closing the connection w/
winsock, however the program won't work
without closing the connection!?!?!?
i'm stuck...should i relisten
somewhere? if so i sure can't find the
right spot to start
relistening...PLEASE HELP!!! :)
|2/6/2002 6:43:39 PM:Bob|
Hey, I'm trying to make a server that
can have 2 clients connected at a time.
What do I need to do it?
|2/23/2002 5:10:01 PM:Danny Weyand|
I don't have an ocx of winsock and have
been trying to use the api calls does
anyone know how to determine the data
that you received?
|3/6/2002 4:20:07 AM:Andrei Lisovoi (ALSI)|
Nice tutorial! Very Basic though...
Doesn't show how you could transfer
files over the network, how to maintain
more than 1 user... Lot's to do. 4 For
|3/6/2002 9:38:37 AM:Terry|
Could not get the code to work (VB6,
W2K sp2) What am I doing wrong?
|3/6/2002 1:38:06 PM:jb|
Nice Simple & to the point. Dont
forget to define the 'data' received as
a string such as: Dim data as string.
If you dont, you'll get the '??'
|3/13/2002 4:58:00 AM:Shadowhunter|
I get a compile error: Syntax Error on
the line "Data = Winsock1.GetData Data"
anyone know how I can solve this?
|3/17/2002 6:42:58 PM:Billy|
Great job now i have mastered the
basics of Winsock
|3/29/2002 4:40:36 AM:Julio Montufar|
Excellent, give him the highest rate 6
|3/29/2002 7:31:38 PM:bulut|
hello and thank you for sharing i can
get everything to work but one thing
the ?'s marks show up. I tried what the
other people said but MsgBox Data,
vbString, bytesTotal does not work any
|3/29/2002 7:38:04 PM:bulut|
oops i put it on the wrong line i got
it now sorry
|3/31/2002 1:57:38 PM:Gaurav|
Accurate and to the point....great work
|4/13/2002 11:08:59 AM:wah_tak|
Cool tutorial! now i'm off to my first
|4/15/2002 8:19:55 PM:Mystins|
Nice tutorial. angelo, you have to use
2 winsock controls (you could use one,
but I prefer order)
1 to always
listen, never close it. The 2nd one
needs to be an array.
Total as long
if total = 0 then
arrayedwinsock(0).state = sckclosed
total = total +
arrayedwinsock is the winsock
with the array. The winsock
controlling all of this is the normal,
no array, winsock control.
.......*looks at his
ok you can
just email me at Mystinsuo@yahoo.com or
icq 32363881 and I can help you.
There's not enough room here to explain
everything I need to explain. Now if I
could only get my chatroom to work
|4/15/2002 8:22:34 PM:Mystins|
Nice example above on winsock, I
remember using it a while
erm.. Darn. I didn't remember
that this thing reads coding ;) Just
email me at Mystinsuo@yahoo.com (BIG
SUBJECT) and I'll give you some
But remember, if your going to
email, make sure you remember you can't
walk before you crawl.
|4/17/2002 2:37:40 PM:Tatiane Ferrari |
How we could send the .XML file using
Winsock ?? Someone may answer me
|4/17/2002 2:39:44 PM:Tati Ferrari|
Someone could help me?? How we may send
an XML file using winsock
|5/2/2002 4:35:10 PM:FosSiL|
yo great tutorial. I need something
that will connect to a server and send
it data from a game like the playername
they are using.
|5/6/2002 5:05:36 PM:Nick Fantis|
Hey, can you guys tell me if there is
any way to keep my IP from changing
every time I log onto the internet?
|5/10/2002 1:00:56 PM:Alex|
I cant seem to make it work.
host n port variable are not
declare.So i declare it as
2)And i also cant find
Private Sub Winsock1_RecieveData(ByVal
data As Long)
Can anyone help me by
senting me a copy of this
|5/14/2002 10:54:27 AM:jore|
|5/30/2002 2:07:51 PM:10x man|
u really halped me but i had to read
the commants for understading and for
geting it work
|6/17/2002 2:32:49 PM:underlaget|
A question ::
Is there a code for
If "User" Connected
label1.caption = "User Is
This is like a beginning
for my multiplayer game im developing
... i also have another question ...
Here's what im planning to do
Lets say the game is an easy RPG
kind and you get to choose between 4
different characters .... im gonna use
something like this
Every "player" gets too choose 1
option between the 4 chars .. but how
do i tell the others that that option
(character) is taken .. like ..
option1.enabled = False ... but how i
tell the other guys program's aswell ??
Understand what i mean ??
If you only
use option1.enabled = false then only
that guy's program will change .. and
not the others ... Im thankful for any
answer i will get to theese 2
questiongs !! // Thnx
|6/18/2002 11:52:40 AM:Dezzy|
Short and sweet tutorial. How can I
send a file to a remote computer and
get the remote computer to start a
|6/19/2002 5:05:14 AM:Nagesh|
i tried the code after placing
winsock control on my form1 with lil
---As u can see i tried the
supported event winsock1_dataarrival
instead of the suggested unsupported
---when i run the
above code and click on connect and
feed my local ip 192.168.1.216 and port
as 5010 and click on send and feed the
text as "hi" I GET THE RUNTIME ERROR
40006 "Wrong Protocol or connection
state for the requested transaction or
request" and i click on debug to find
the debugger point the code line
|6/20/2002 3:52:56 PM:gavros tou kerata|
I dont know nothing about winsock.
you can give me some basic
information.How to write ip adress(i
know mine)but i dont know about ports
etc. iwant to send a message to my self
with winsock.please help
|6/21/2002 1:25:52 AM:greg|
nice tutorial, basic as said.. but nice.
|6/28/2002 1:27:41 AM:Matt|
For me, I had trouble with my client
application because my server was
always on a different IP address each
time I logged onto the internet.
I wrote code on the server app to write
to a CGI script on my webpage. This
CGI script stores the IP my server uses
each time I load it up. Then the
client uses an HTTP protocol to
retrieve this IP address stores on the
web. It works great.
Now I only
have to work on having more than one
client connected at once. :-)
|6/30/2002 1:41:42 AM: |
This is my code
Winsock1.LocalPort = 2002
requestID As Long)
Winsock1.State <> sckclose Then
Winsock1_Error(ByVal Number As Integer,
Description As String, ByVal Scode As
Long, ByVal Source As String, ByVal
HelpFile As String, ByVal HelpContext
As Long, CancelDisplay As Boolean)
|7/3/2002 12:50:53 AM:raghuraja|
|7/16/2002 12:43:44 PM:JOSHUA STEWARD|
THIS WAS JUST WHAT I NEEDED. THAT
WINSOCK IS CONFUSING IF YOU DON'T
|7/31/2002 1:15:14 PM:DrDre|
uhh i read everyone and cant find my
problem, when it tries to listen to the
port i get runtime error 2. WHAT IS
Private Sub Command1_Click()
port = InputBox(
|7/31/2002 1:16:38 PM:DrDre|
InputBox("What port do you want to host
|8/11/2002 5:48:55 AM:jozo|
the code is neat, but i have problems
compiling it. Something lik argument
not optional in the
click winsock icon i get only this
Winsock_error sub. No sign of recieve
data or connection request. Am i
missing something here? thanks
what is on my winsock icon:
Sub Winsock1_Error(ByVal Number As
Integer, Description As String, ByVal
Scode As Long, ByVal Source As String,
ByVal HelpFile As String, ByVal
HelpContext As Long, CancelDisplay As
|8/20/2002 10:37:46 AM:Kurt|
Great Tutorial, definately helped alot!
|8/23/2002 12:09:33 PM:skad0g|
it says "Wrong Protocol or Connection
|9/4/2002 12:29:21 PM:|
Wow, that's a great tutorial, for the
people that get question marks and
blank boxes, you must remember to place
it in the Winsock1_DataArrival, like
|9/5/2002 12:18:43 PM:|
Good job! I used to be stuck using a
UDP connection. Now TCP is easy to use!
|9/7/2002 11:49:23 AM:Wouter Lievens|
Not explained badly, but placing it in
intermediate category is lame.
that would belong in intermediate is a
full multi user system with automatic
timeouts and pinging.
would belong in Advanced would be a
full PeerToPeer application that does
graph searching for the most efficient
connection and all that.
So if you
would have put this in Beginner I would
have given 3 or 4. In Intermediate it
deserves only 1.
|9/7/2002 11:59:31 AM:Matt Carpenter|
Isn't the level stuff for the CODER not
the CODE? Notice when you post the
code, next to the 'expert' radio
button, it says ( 1+ years)
be for the coder :-/
|9/7/2002 10:43:37 PM:Jeremiah Ng|
Very easy to understand winsock
do you have any tutorial as
simple as this and has multi client
or any other winsock
server and client tutorial??? Please
help me thankz.... email me...
|10/13/2002 6:20:38 PM:|
OK, first. I really appreciate your
taking the time to write this down and
try to help those of us who don't know
these basics. I did learn from you and
Not to be mean or to
sound ungreatful, but I wish you would
have taken just a bit more time to try
to ensure that your code needed no
modification. That would have saved me
a lot of time from having to delve
through the responses to locate the
No biggie though. I
learned a lot and you as well as those
who offered additional help via their
comments have my gratitude. Thanks.
|10/23/2002 8:35:34 AM:Logix|
|11/8/2002 7:10:41 PM:Patabugen|
Thank you so much for being simple. Ive
been trying 2 learn Winsock for ages
but all i have found is "Advanced
Winsock" and "Multi User Chats" nothing
which just tells me how it works
(and thanks to
everyone who posted about that mindor
|11/15/2002 11:58:22 AM:|
I compare with some users, that there
was some problems, but I've solved it
with a little help of solutions already
existing here ;P really simple and
powerful. And, I'm soryy I don't
believe You're really 13;) . Actually
I'm 14 :D
|12/2/2002 9:42:24 AM:|
This code realy helped me.
|12/18/2002 12:41:28 AM:|
gave me a nice hint then i found out
what to do... might be missing a few
i made a chat program and
got it to work after a few hours of
|12/21/2002 10:14:02 AM:Ricky Casavecchia|
i think there is something wrong with
my winsock thing but when ever i run
the program and try to host it says
error so i click debug and it hilights
winsock1.listen it does this everytime
i use winsock does anyone know whats
|12/27/2002 8:08:14 AM:Marvin Hufford|
Your code does not work because the
command, if winsock1.state <> sckclosed
then winsock1.close, always closes the
winsock. I agree with Mystins in that
you need an array for the winsock in
the host (not the client) so that there
is one element of the array (an
instance) for listening and one element
for each client. Suggestion: Put a 0
for the Index in the property menu.
Then, your code for the host could
include, Winsock(0).localport = Port,
and Winsock(0).Listen. Also add, Load
winsock(1), before the line:
winsock(1).accept RequestID. Make other
code changes so the host winsock is
winsock(num) (depending on the winsock
caption) and the client winsock remains
|12/28/2002 1:41:45 AM:|
supoose if in client
client is under
proxy. then how can client connect to
server through proxy..
|1/11/2003 6:59:13 PM:|
everytime i try to put this all
together it works but when i run it and
goto connect to my self (after doing
the listen on a diffrent one) i get a
runtime error 13 type mismatch and im
putting in 127.0.0.1 and when i put in
127001 it works but crashes after wards
... whats wrong? i did everything right
|2/8/2003 4:58:03 AM:|
ok my problem is simple im making a
program using upto to 10 winsocks i get
them all to logon but when i try to
send any data it causes a protocol
errori use a diff port for aech winsock
so if you know what im doing wrong or
if i need to index the winsocks please
email me with your idea .....ur advice
was simple to the point and easy to
understand although some changes were
needed to make ur example work i give u
a 6 but keep up the good work :)
|2/22/2003 10:43:35 PM:|
Hi I was wondering if there is any
tutorials anywhere on how to create a
chat or media desktop program like
|3/18/2003 11:33:35 PM:|
I dont mean to be a bad guy, but im
bein hounest. this tutorail is wrong. i
dont no why but it is. please if any
one knows how to teach a good way to
make a game using winsock. e-mail me
|3/19/2003 12:20:29 AM:Matt Carpenter|
Note to all: Im sorry about the error
in this code that everybody's been
complaining about. I dumped visual
basic a long time ago and I had
forgotten all about this article until
I started recieving emails notifing me
about the comments. I'll attempt to
change this code right now, but those
who say that they can't get it to work
should've read through the comments
because I belive somewhere in there I
posted a fix.
|3/22/2003 9:19:57 AM:|
Nice,.. had to define data, host, port
as string to make it work.
|4/8/2003 10:34:51 PM:|
DUDE THIS TUTORIAL ROCKS! i have tried
to learn winsock by reading through
source but it was all too complicated-
this tutorial is great! i give u a 10/10
|4/16/2003 6:06:33 AM:Ali Damji|
Great tutorial......easy to understand.
|4/20/2003 4:30:51 PM:|
i cant add the winsock control to my
project what should i do when i try i
get this error licence information for
componet not found or something like
that how can i fix this??????
|5/10/2003 12:25:10 AM:Teena417|
plz how can i connect many people using
Please send me a
With Love And
|6/5/2003 8:55:58 AM:|
I like the mannor of teaching applied
here. However I get a run time error.
It's due to the fact that
'winsock1_RecieveData' Doesnt exist in
my 6.0 version. the closest thing that
was there was 'winsock1_DataArrivel'.
This disapointed me. :(
|6/5/2003 9:28:47 AM:|
Hummm. I ended that poorly. I like the
concept, and I'm not trying to
criticize. But merely offer
constructive feedback. You should (if
you don't) make sure to check and
follow up with these posts. If you're
going to start such an endevor, you
must follow through. However. Don't
let negitive posts discourage you from
continuing in your quest.
|6/11/2003 1:10:45 AM:|
how do i add a port to the form?
|6/21/2003 8:29:43 AM:|
great tutorial, straight to the point.
| Add Your Feedback! |
|Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name. |
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here.