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

Picture of How to make a simple chat program in visual basic using the Microsoft Winsock Control
In this instuctable i will show you how to make a simple chat program in visual basic. i will go over what all the code does so you will be learning as you make it, and at the end i will show you how to use it.
Remove these adsRemove these ads by Signing Up

Step 1: Overview

Picture of Overview
This program will let you exchange messages between two people on your same network. It may work if your not on the same network but i can't test it now.

You should have Microsoft Visual Basic 6.0, that is what i am using.
Basic programing knowledge is recommended

Lets get started!!!

open visual basic and create a Standard EXE

Step 2: Creating the user infrence

Picture of Creating the user infrence
Should look like this.JPG
Now it is time to create the user form

you will need to add the winsock control to the toolbox
do this by:
1. right click somewhere in the toolbox where there is nothing
2. click Components
3. in the long list find Microsoft Winsock Control 6.0 and check the box next to it
4. then click apply then close

5 text boxes
4 command buttons
1 winsock control

arrange them to look like the second picture below

Step 3: Renaming and other property changes

Picture of Renaming and other property changes
we are now going to be changing the captions(what is says in the command buttons)

1. click command1
2. look on the right side in the properties and find Caption
3. change it to Host

do the same for the rest but make them:

command2 = Connect

command3 = Close

command4 = Send

now we are going to change what the text boxes say

1. click on the text box that says text1
2. go to the properties window and find the Text property
3. make it blank
4. do the same for the one that says Text2
5. now click text3 and change the text property to
6. now click text4 and change the text property to 1234
7. now click text5 and change the text property to Name is your local ip so you can test the program on your computer when its done
1234 is the port you can make it whatever you want but just leave it 1234 for now

Step 4: Changing the names of the buttons and text boxes

Picture of changing the names of the buttons and text boxes
we need to rename the command buttons and text boxes to make them easier to code

1. click Text1 and go to the properties and click on name and change it to txtLog i chose that because it is the log of messages you send and receive

2. do the same for the rest but make the names:

change Text2 to txtSend

change Text3 to txtIP

change Text4 to txtPort

change Text5 to txtName

change Command1 to cmdHost

change Command2 to cmdConnect

change Command3 to cmdClose

change Command 4 to cmdSend

change Winsock1 to sckMain

now there is one more thing you need to change
click txtLog and in the properties window find MultiLine and make it true

this step may cause some confusion if you have any questions post a comment or message me

Step 5: Coding the command buttons

Picture of Coding the command buttons
now it is time to write the code for the command buttons

lets start with the first one cmdHost

1. double click it, this will bring up the code window
2. it should look like this
Private Sub cmdHost_Click()

End Sub
3. enter this code

sckMain.LocalPort = txtPort.text

now what does that mean??

the first line makes the winsocks local port equal the number in the textbox (txtPort)

the second line tells the winsock to listen on that port for someone trying to connect

now close that and double click on cmdConnect

enter this code

sckMain.RemoteHost = txtIP.text
sckMain.RemotePort = txtPort.text

the first two lines change the RemoteHost and the RemotePort of the winsock (sckMain) to the ip and port we want

the sckMain.Connect tells it to connect to someone listening on that port and ip

now for the close button

double click on cmdClose and type

txtLog = txtLog.text & "Connection closed!" & vbCrlf

the sckMain.close tells the winsock to disconnect

the second line adds Connection closed to txtLog

double click on cmdSend and type

sckMain.senddata "[" & txtName.text & "]" & txtSend.text
txtLog = txtLog.text & "[" & txtName.text & "]" & txtSend.text & vbcrlf
txtSend.text = ""

the first two lines are wrong i have them right in the editor and it changes them when i save so look at the picture for the right code

first line sends the text in txtSend and your name in txtName
second line adds the message you sent and your name to txtLog
third line clears the text in txtSend

Step 6: Coding the Winsock

Picture of 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
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

Step 7: Using the program/Conclusion

Picture of Using the program/Conclusion
ok now im going to tell you how to use the program

in visual basic click file then make project1.exe save it to your desktop
if there are any errors leave a comment and i will try to help you

now go to where you made the program and double click it twice and two programs should pop up
on one click host and on the other click connect and on the one you clicked host on it should say: Connected to:

now type in a name for both of them and you can send messages between the two

it works on LAN and it might work on WAN but im not sure

leave comments letting me know how i did or if you have questions or if you just feel like it
if you liked it or didn't like it rate it
1-40 of 246Next »
mykilld2 months ago

It only works on 2 users? I tried to connect another pc but it failed

FabendryA4 months ago

Where is the winsock control in Visual Studio 2013?

RajeshS65 months ago

I am running my chat application on 3 computers. but they are not connecting. how should I set IP and port address of both server side and client side computers.

benzjoshua.labang made it!6 months ago

thank you!

benzjoshua.labang made it!6 months ago

thank you!

joshi_jay7 months ago

Help me with runtime error "object required" 424 as soon as possible.

please help me with 40020 error..

please help me with 40020 error..

please help me with 40020 error..

ankita081 year ago

this is working very well in the same pc and thanx a lot for that but i've tried to make it work on two but was not able to do so. can you plz tell me the way how to do it?

I think this program will only work in localhost so you need Hamachi to create virtual localhost and connect different computers to each other

JasperA10 months ago

maraming salamat

JasperA10 months ago

maraming salamat

fidnah1 year ago
i have error 40020
invalid operation
Chiranjith2 years ago
can u plz let me know how to transfer files instead of text... please... it will b helpful for my project..
twenglish1 (author) 2 years ago
I haven't been on here for a while, so sorry i haven't gotten a chance to reply to a lot of you asking questions or having problems, the way i wrote this chat program i believe it only works with the older vb 6.0 the newer versions do not have the winsock control module, i am not sure how to use it either, i know its possible though
enashton2 years ago
I can seem to find the "Microsoft WinSock" in Visual Basic 2012, please resond with a way to find it.

thank you
palavesam912 years ago
every thing should be done but finally have a runtime error as "object required 424"
its good but runtime error "object required" 424
its good but error object required
I tried your code and it works fine so thank you for it.

I want to perform following task in your program :-
1)Group chat
2)Add smilies
3)Sending files

Please note that i don't want to make more changes

I use richtextbox in my program.

I request you to upload pdf to perform above task
Screamz3322 years ago
I am still having trouble with the sckMain.GetData(Data, vbString) line, do you have any suggestions. I fixed the rest of the errors myself, but have no clue with this one.
mukeshdube2 years ago
when i click host and then connect runtime error 40020 occurrs how i can overcome this problem
adandoy2 years ago
thank you now i already know it...
t asdi2 years ago
when i click host and then connect runtime error 40020 occurrs how i can overcome this problem
t asdi2 years ago
when i click host and then connect runtime error 40020 occurrs how i can overcome this problem
thank you
rnvenkat2 years ago
operator '&' is not defined for types 'string' and 'system.windows.forms.textbox'

i am facing this problem in vb.net 2008

pls help me
purni21922 years ago
For the send command we get wrong protocol or connection state for the requested transaction,n for other commands v get a runtime error-424 object required..and no 2 forms appear here;pls help me in this...
i can't download the tut pdf!!
Corliansa3 years ago
there is this word in front = "Run-Time Error '40006';
Corliansa3 years ago
I can't when i click "Send" button it give this message = "Wrong Protocol Or Connection State For The Requested Transaction Or Request" can you fix it?
markjeggar3 years ago
A simple VB.NET chat program with source code


kumali3 years ago
is it working with at least 3 computers?
de7ox6 years ago
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!
jflores-3 de7ox4 years ago
I created a chat program without winsock and its works
the correct code is:If sckMain.CtlState <> 0 Then

how is it possible can you explain
twenglish1 (author)  jflores-34 years ago
How did you do it?
1-40 of 246Next »