3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

How to make a simple chat program in visual basic using the Microsoft Winsock Control

Step 6Coding the Winsock

Coding the Winsock
now it is time to code the winsock!

we need to tell it what to do when someone tries to connect and we need to tell it where to put the data it receives

ok double click on the winsock control(sckMain)
in the upper right corner of the code box it should say: error
click the drop down box and select connection request
now it should have made a new place to enter code

in there type:

If sckMain.state <> sckClose then
sckMain.close
end if
sckMain.accept requestid
txtLog = txtLog.text & "Connected to: " & sckMain.RemoteHostIP & vbcrlf

first and second lines check if the winsock(sckMain) is closed and if it is not then it closes it
third line closes the if statement
forth line tells the winsock (sckMain) to accept connections
fifth line tells you that you are connected by adding a line to txtLog

now we need to tell the winsock what to do with the data it recieves

in the top right of the code window click the drop down box and select: DataArrival

now enter this code:

dim Data as string
sckMain.getdata Data, vbstring
txtLog = txtLog.text & data & vbcrlf

first line makes data a string the sting is where the data that is received is going to be put before it is added to txtLog
second line gets the data and places it in the string data as a vbstring
third line adds the data received to txtLog

now once again if it is confusing and you have questions post comment or message me
« Previous StepDownload PDFView All StepsNext Step »
19 comments
May 12, 2009. 8:34 PMde7ox says:
Hi, i am coding in VB2008, it worked so far but this part doesn't work @ all for me :

sckMain.state <> sckClose


sckMain.accept requestid


Please e-mail me with a solution asap as i really like this proggy u made!
de7ox@hotmail.com
Thanks!
Mar 5, 2011. 5:28 PMjflores-3 says:
I created a chat program without winsock and its works
Mar 11, 2011. 5:39 AMjflores-3 says:
the correct code is:If sckMain.CtlState <> 0 Then

sckMain.Accept(e.requestID)
Feb 28, 2012. 12:55 AMsathiprady says:
how is it possible can you explain
Sep 13, 2010. 4:46 PMgurgle528 says:
Dim proCedure As String
sckMain.Close()
sckMain.Accept(e.requestID)
sckMain.Accept(e.requestID)
proCedure = txtLog.Text & "Connected to: " & sckMain.RemoteHostIP & vbCrLf
proCedure = txtLog.Text
you will need to fix the lines, but this is what I have.
Sep 22, 2009. 5:58 AMRoboticProgrammer says:
Winsock1.Close() Winsock1.Accept(e.requestID)
May 12, 2009. 8:34 PMde7ox says:
Error Msgs 4 both are sckClose and requestID not declared
May 10, 2009. 11:28 AMTrumple says:
The winsock code dosent seem to work for Visual basic 2008 :( I read further down that you figured out how to code it for 2008, could you please explain to me how to do it? You seem to know alot about this, whereas im quite new :) Thanks for any help
Feb 7, 2009. 3:52 AMgafugaf says:
first i clicked on host and then clicked on connect ,then error message is like "invalid operation at currwent state) y it is like this?
Feb 8, 2009. 3:37 AMgafugaf says:
ok fine.when i opend the .exe two times on the same window, it works but how can i use it on a LAN. . i opend one exe in one system and other in other but then it doesn;t work. please help me.
Oct 15, 2008. 9:17 AMDarlyle says:
Any chance you might know a way to add a file share feature?
Oct 13, 2008. 1:04 AMDarlyle says:
Ok, it's sending the first message I attempt, but as soon as it sends i get a object required error. Please help me out here. You can reach me easier at Darlyle@live.com thanks.
Apr 30, 2011. 8:16 AMDarlyle says:
Nah I figured it out, in the line sckmain.GetData, I accidentally put getdate with an E. Lol.

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
8
Followers
2
Author:twenglish1
i love working with electronics and computers, playing video games, and i love to play with fire, HACKING!!!!