OCX,tutorial,will,teach,basics,winsock,contro
Quick Search for:  in language:    
OCX,tutorial,will,teach,basics,winsock,contro
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
RentACoder Stats

 Code:  lines
 Jobs: 0 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for RentACoder.
Click here to see a screenshot of this code!age
By sherif rofael on 8/19

(Screen Shot)

Click here to see a screenshot of this code!your age
By sherif rofael on 8/19

(Screen Shot)

Quad Click
By Skitzo Monk on 8/18


Afro Speak
By Skitzo Monk on 8/18


a 25% done 3D level editor
By Homee G on 8/18


Converts Seconds to Time (functions withour any errors)
By Sunil Wason on 8/18


Click here to see a screenshot of this code!PSC explorer-See the code from 0-38000 and more!!
By XasanSoft on 8/18

(Screen Shot)

Click here to see a screenshot of this code!VB IRCd (pure VB6 Code, no dlls, winsock.ocx is everything u need)
By Dennis Fisch on 8/18

(Screen Shot)

begginerproject
By Gary Seume on 8/18


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

__Simple Winsock Tutorial__

Print
Email
 

Submitted on: 1/5/2002 12:42:24 AM
By: Matt Carpenter 
Level: Intermediate
User Rating: By 50 Users
Compatibility:

Users have accessed this article 19382 times.
 
(About the author)
 
     This tutorial will teach you the basics of winsock (the winsock OCX control). How to connect to computers, how to be a server, sending and getting data. If you like this, Please vote for me ;)

 
 
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
Winsock1.Listen

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.
winsock1.accept RequestID

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?")
winsock1.senddata 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.

Data = winsock1.getdata
msgbox Data

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 (Just 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
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 fine now.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/7/2002 7:10:20 PM:dumbass
I had to do the same as the person above. To compile: change "Data = winsock1.getdata" to "Winsock1.GetData Data".
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/16/2002 10:05:52 AM:d3
I thought this tutorial is short but sweet, it told me everything I needed to know :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 someone help!! Private Sub Form_Click() Port = InputBox(
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 why?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/22/2002 1:19:03 PM:guato
haha, whoops, during my registration you answered my question before I posted it... erie...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/25/2002 1:23:34 AM:Dan Wold
To: 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 someone help!! Private Sub Form_Click() Port = InputBox( " from what i Pieced together from your message, I beleive this is what you needed... PortBox = InputBox("Please enter the port.","Port Number") Winsock.RemotePort = PortBox If not feel free To email me!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/29/2002 12:05:45 PM:Maniac
Interesting tutorial. It all seems so easy now
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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: If wskMain.State <> sckClosed Then wskMain.Close wskMain.Accept requestID 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!!! :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 effort though!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/6/2002 9:38:37 AM:Terry
Could not get the code to work (VB6, W2K sp2) What am I doing wrong?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 '??' question marks.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/17/2002 6:42:58 PM:Billy
Great job now i have mastered the basics of Winsock
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/29/2002 4:40:36 AM:Julio Montufar
Excellent, give him the highest rate 6
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 suggestions
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/29/2002 7:38:04 PM:bulut
oops i put it on the wrong line i got it now sorry
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/31/2002 1:57:38 PM:Gaurav
Accurate and to the point....great work
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/13/2002 11:08:59 AM:wah_tak
Cool tutorial! now i'm off to my first net-game!!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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. Do something like this Public Variables.. Dim Total as long if total = 0 then if arrayedwinsock(0).state = sckclosed then arrayedwinsock(0).close else arrayedwi nsock(0).accept total = total + 1 load arrayedwinsock(total) arrayedwinsock(to tal).accept Requestid end if arrayedwinsock is the winsock with the array. The winsock controlling all of this is the normal, no array, winsock control. .......*looks at his text* ............. 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 using microsofts extremely-slow-data-processing ocx...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/15/2002 8:22:34 PM:Mystins
Nice example above on winsock, I remember using it a while back. 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 help. But remember, if your going to email, make sure you remember you can't walk before you crawl.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/17/2002 2:37:40 PM:Tatiane Ferrari
How we could send the .XML file using Winsock ?? Someone may answer me ? Winsock.senddata
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/17/2002 2:39:44 PM:Tati Ferrari
Someone could help me?? How we may send an XML file using winsock ? winsock.senddata
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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? Thanks.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/10/2002 1:00:56 PM:Alex
I cant seem to make it work. 1)The host n port variable are not declare.So i declare it as string. 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 tutorial. Thanks. wodeming@hotmail.com
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/14/2002 10:54:27 AM:jore
puyeng man
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/17/2002 2:32:49 PM:underlaget
A question :: Is there a code for Something like this: --------------------------------- ------ If "User" Connected Then label1.caption = "User Is Connected" ---------------------------- ----------- 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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 batch file.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/19/2002 5:05:14 AM:Nagesh
hi, i tried the code after placing winsock control on my form1 with lil changes, Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Winsock1.GetData Data, vbString, bytesTotal MsgBox Data End Sub ---As u can see i tried the supported event winsock1_dataarrival instead of the suggested unsupported event recievedata... ---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 "Winsock1.SendData Text" help me plz. regards, nags
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/20/2002 3:52:56 PM:gavros tou kerata
I dont know nothing about winsock. If 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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/21/2002 1:25:52 AM:greg
nice tutorial, basic as said.. but nice.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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. So 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. :-)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/30/2002 1:41:42 AM:
This is my code Private Sub Command1_Click() Winsock1.Connect Text1.Text, 2002 End Sub Private Sub Command2_Click() Winsock1.LocalPort = 2002 Winsock1.Listen End Sub Private Sub Command3_Click() Winsock1.SendData Text2.Text End Sub Private Sub Command4_Click() Winsock1.Close End Sub Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) If Winsock1.State <> sckclose Then Winsock1.Close Winsock1.Accept requestID End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Winsock1.GetData adata MsgBox adata End Sub Private 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 Boolean) MsgBox "Error" End Sub
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/3/2002 12:50:53 AM:raghuraja
good
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/16/2002 12:43:44 PM:JOSHUA STEWARD
THIS WAS JUST WHAT I NEEDED. THAT WINSOCK IS CONFUSING IF YOU DON'T UNDERSTAND IT.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 WRONG!!! Private Sub Command1_Click() Dim port port = InputBox(
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/31/2002 1:16:38 PM:DrDre
InputBox("What port do you want to host on?") Winsock1.LocalPort = port Winsock1.Listen End Sub
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 Data=winsock1.Getdata line When i click winsock icon i get only this Winsock_error sub. No sign of recieve data or connection request. Am i missing something here? thanks here what is on my winsock icon: Private 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 Boolean) End Sub
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | RentACoder Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.