Introduction: 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.

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

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

Add:
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 127.0.0.1
6. now click text4 and change the text property to 1234
7. now click text5 and change the text property to Name

127.0.0.1 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
sckMain.listen

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
sckMain.Connect

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

sckMain.close
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
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

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: 127.0.0.1

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

Comments

SherwinC8 made it! (author)2017-06-11

thanks bro! it works!

YinB1 made it! (author)2017-05-14

biuld error

error 461

LemuelT3 (author)2017-04-09

debug please help me

DewmithaM (author)2017-02-08

I'm using visual studio 2013.

When I run the program, the following error message is given.

"

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in AxInterop.MSWinsockLib.dll

Additional information: Exception from HRESULT: 0x800A9C54"

my email:dewmitha.mahiti@gmail.com

Please help me!

ElangoE3 (author)2016-12-12

exe file not opend pls tell how to run the programe

NorS8 (author)2016-12-01

Hi, how can it connect with other pc? And how can i make it so there are more that two people can chat it this?

JhordanD (author)2016-09-29

please send me the code i'm creating a system with chatbox tnx in advance jhordan.meneses@gmail.com

girish_kengar (author)2016-07-08

cool

AshishJ38 (author)2016-06-28

please send me code at hackaholic08@gmail.com

de7ox (author)2009-05-12

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!

jflores-3 (author)de7ox2011-03-05

I created a chat program without winsock and its works

raj59684 (author)jflores-32016-05-29

pls mail me the procedure and code .... nikkuraj786@gmail.com

jflores-3 (author)jflores-32011-03-11

the correct code is:If sckMain.CtlState <> 0 Then

sckMain.Accept(e.requestID)

sathiprady (author)jflores-32012-02-28

how is it possible can you explain

twenglish1 (author)jflores-32011-03-05

How did you do it?

gurgle528 (author)de7ox2010-09-13

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.

RoboticProgrammer (author)de7ox2009-09-22

Winsock1.Close() Winsock1.Accept(e.requestID)

twenglish1 (author)de7ox2009-05-13

i don't know how to get this program to work with VB2008 you need to make changes to the code, sorry

twenglish1 (author)2016-04-22

To everyone asking why it isnt working: this method only works on windows xp with microsoft visual basic 6!

vincent971005 (author)2016-04-20

anyone know what is the problem??

ThiernoB1 made it! (author)2016-04-17

didn't work for me. Am running window 10

ThiernoB1 (author)2016-04-17

didn't work for me. Am running window 10

joshi_jay (author)2014-12-30

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

MohamadH15 (author)joshi_jay2016-04-12

change the name property of txt2 into txtlog

MohamadH15 (author)2016-04-12

I need help here the program is running correctly but there is no lines between the messages help

nook6 made it! (author)2016-02-08

brilliant thank you

KevinH86 (author)2015-10-24

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Well guys You cannot connect to another computer through WAN until you port forward it and insert the correct IP..

You can use HAMACHI also but it will require the other person to use hamachi too..

Okay now see my tutorial:

You need to host the main IP on ur net or it should be a VPS in which u open a port or you port forward the port the chat will listen to.

After port forward keep the VPS/PC/OS on and make sure you dont have dynamic ip that changes each hour, now the chat server will be active and people can join the chat room with your IPv4 Online IP, type ipconfig in cmd.exe or just whatismyipaddress.com and you are done..

Add the port you forwarded in the box then click Connect.

and the IP of the host..

~Regards

K. Hendrews

mykilld (author)2015-05-24

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

james.harms.125 (author)2015-05-06

FabendryA (author)2015-03-15

Where is the winsock control in Visual Studio 2013?

RajeshS6 (author)2015-02-04

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! (author)2015-01-06

thank you!

benzjoshua.labang made it! (author)2015-01-06

thank you!

sunny.thapa.7399 (author)2014-10-19

please help me with 40020 error..

sunny.thapa.7399 (author)2014-10-19

please help me with 40020 error..

sunny.thapa.7399 (author)2014-10-19

please help me with 40020 error..

ankita08 (author)2014-04-21

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?

dani.kaplun (author)ankita082014-10-14

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

JasperA (author)2014-09-22

maraming salamat

JasperA (author)2014-09-22

maraming salamat

fidnah (author)2013-12-24

hi
i have error 40020
invalid operation

Chiranjith (author)2013-07-07

can u plz let me know how to transfer files instead of text... please... it will b helpful for my project..

twenglish1 (author)2013-06-17

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

enashton (author)2013-06-17

I can seem to find the "Microsoft WinSock" in Visual Basic 2012, please resond with a way to find it.

thank you

palavesam91 (author)2013-05-28

every thing should be done but finally have a runtime error as "object required 424"

adityachavan142 (author)2013-05-08

its good but runtime error "object required" 424

adityachavan142 (author)2013-05-08

its good but error object required

Naitikkundalia (author)2013-03-29

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

Screamz332 (author)2013-03-23

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.

mukeshdube (author)2013-03-06

when i click host and then connect runtime error 40020 occurrs how i can overcome this problem

About This Instructable

283,068views

46favorites

License:

More by twenglish1:How to make a simple chat program in visual basic using the Microsoft Winsock ControlHow to Install and Boot Damn Small Linux on a USB Flash Drive
Add instructable to: