vb6:Simple Winsock Tutorial

Any help regarding any computer based programming language (non serverside) can be requested and recieved here.
Locked
barmaki
Posts: 10
Joined: Sat Jul 15, 2006 7:26 am

vb6:Simple Winsock Tutorial

Post by barmaki »

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 **** the 'host' button, it would activate the server and wait for somebody to try to connect.

Now, double **** 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 **** 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 **** the Winsock control, then go to the 'winsock1_RecieveData' sub (or something like that). Put in this code.

winsock1.getdata(data,vbstring,bytestotal)
or is it winsock1.getdata data,vbstring,bytestotal? I think I might be mixing c++ syntax w/ visual basic :-/
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, **** 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, **** '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, **** send, then type in a message. No, don't just type in any message, type 'Hello World!'. How original is that? **** 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!


jeremy90
Posts: 440
Joined: Mon Jun 26, 2006 3:15 pm

Post by jeremy90 »

thanks for the tutorial, it helps me out a lot
breeze
Posts: 340
Joined: Sat Mar 18, 2006 4:49 am

Post by breeze »

Yeah, thanks for that. It helped with my schoolwork. Where did you get it from or is it your own tutorial?
This is a message, a message from the all-mighty (me): You shall go here, you shall not complain: Breeze's Corner.
TheCrymsonLegends
Posts: 1246
Joined: Wed Feb 16, 2005 6:59 am

Post by TheCrymsonLegends »

Dunno but if you know VB6, think you would want to help out with my game? I could use a programmer. It's mostly Java but Java should be a breeze ( lol go figure ) if your a VB Programmer.
Reached 5000 Credits! The highest of any member on Smokyhosts! New milestone for Me!
breeze
Posts: 340
Joined: Sat Mar 18, 2006 4:49 am

Post by breeze »

I'm not that great at it...

we have to make a game for IPT, and we gotta have all the N-S diagrams and theory and all that - a real pain in the neck.

Anyways, I doubt I'll be able to make a good game without a lot more study and all of that...
This is a message, a message from the all-mighty (me): You shall go here, you shall not complain: Breeze's Corner.
rwshthn
Posts: 500
Joined: Tue Sep 26, 2006 1:39 am
Contact:

wow

Post by rwshthn »

wow........................
what all this informaition man
raina
Posts: 213
Joined: Sat Jan 20, 2007 11:18 am
Contact:

Post by raina »

Thans a lot for the info mate.
Image
dexter
Posts: 32
Joined: Fri Mar 09, 2007 1:18 pm

Post by dexter »

thanks you very much for tutorial,..
Locked