loading
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

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

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

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

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

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

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

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
<p>debug please help me</p>
<p>I'm using visual studio 2013.</p><p>When I run the program, the following error message is given.</p><p>&quot;</p><p>An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in AxInterop.MSWinsockLib.dll<br><br>Additional information: Exception from HRESULT: 0x800A9C54&quot;</p><p>my email:dewmitha.mahiti@gmail.com</p><p>Please help me!</p>
<p>exe file not opend pls tell how to run the programe</p>
<p>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?</p>
please send me the code i'm creating a system with chatbox tnx in advance jhordan.meneses@gmail.com
<p>cool</p>
<p>please send me code at hackaholic08@gmail.com</p>
Hi, i am coding in VB2008, it worked so far but this part doesn't work @ all for me :<br/><br/> sckMain.state &lt;&gt; sckClose<br/><hr/><br/>sckMain.accept requestid<br/><hr/><br/>Please e-mail me with a solution asap as i really like this proggy u made! <br/>de7ox@hotmail.com <br/>Thanks!<br/>
I created a chat program without winsock and its works
<p>pls mail me the procedure and code .... nikkuraj786@gmail.com</p>
the correct code is:If sckMain.CtlState &lt;&gt; 0 Then<br> <br> sckMain.Accept(e.requestID)
how is it possible can you explain
How did you do it?
Dim proCedure As String<br> sckMain.Close()<br> sckMain.Accept(e.requestID)<br> sckMain.Accept(e.requestID)<br> proCedure = txtLog.Text &amp; &quot;Connected to: &quot; &amp; sckMain.RemoteHostIP &amp; vbCrLf<br> proCedure = txtLog.Text<br>you will need to fix the lines, but this is what I have.
Winsock1.Close() Winsock1.Accept(e.requestID)
i don't know how to get this program to work with VB2008 you need to make changes to the code, sorry
To everyone asking why it isnt working: this method only works on windows xp with microsoft visual basic 6!
<p>anyone know what is the problem??</p>
<p>didn't work for me. Am running window 10</p>
<p>didn't work for me. Am running window 10</p>
<p>Help me with runtime error &quot;object required&quot; 424 as soon as possible.</p>
<p>change the name property of txt2 into txtlog</p>
I need help here the program is running correctly but there is no lines between the messages help
<p>brilliant thank you</p>
<p>~~~~~~~~~~~~~~~~~~~~~~~~~~~~</p><p>Well guys You cannot connect to another computer through WAN until you port forward it and insert the correct IP..</p><p>You can use HAMACHI also but it will require the other person to use hamachi too..</p><p>Okay now see my tutorial:</p><p>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.</p><p>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..</p><p>Add the port you forwarded in the box then click Connect.</p><p>and the IP of the host..</p><p>~Regards</p><p>K. Hendrews</p>
<p>It only works on 2 users? I tried to connect another pc but it failed</p>
<p>Where is the winsock control in Visual Studio 2013?</p>
<p>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.</p>
<p>thank you!</p>
<p>thank you!</p>
<p>please help me with 40020 error..</p>
<p>please help me with 40020 error..</p>
<p>please help me with 40020 error..</p>
<p>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?</p>
<p>I think this program will only work in localhost so you need Hamachi to create virtual localhost and connect different computers to each other</p>
<p>maraming salamat</p>
<p>maraming salamat</p>
hi <br>i have error 40020 <br>invalid operation
can u plz let me know how to transfer files instead of text... please... it will b helpful for my project..
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
I can seem to find the &quot;Microsoft WinSock&quot; in Visual Basic 2012, please resond with a way to find it. <br> <br>thank you
every thing should be done but finally have a runtime error as &quot;object required 424&quot;
its good but runtime error &quot;object required&quot; 424
its good but error object required <br>
I tried your code and it works fine so thank you for it. <br> <br>I want to perform following task in your program :- <br>1)Group chat <br>2)Add smilies <br>3)Sending files <br> <br>Please note that i don't want to make more changes <br> <br>I use richtextbox in my program. <br> <br>I request you to upload pdf to perform above task
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.
when i click host and then connect runtime error 40020 occurrs how i can overcome this problem
thank you now i already know it... <br>
when i click host and then connect runtime error 40020 occurrs how i can overcome this problem

About This Instructable

276,127views

45favorites

License:

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