How to Make a Simple Chat Program in Visual Basic Using the Microsoft Winsock Control

286,167

47

260

Published

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

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

6 People Made This Project!

Recommendations

  • Planter Challenge

    Planter Challenge
  • Make it Move Contest

    Make it Move Contest
  • Casting Contest

    Casting Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

260 Comments

Wrong protocol or connection state for the requested transaction or request

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

1 reply

Error is:

sckMain.senddata "[" & txtName.text & "]" & txtSend.text

debug please help me

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!

exe file not opend pls tell how to run the programe

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?

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

please send me code at hackaholic08@gmail.com

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!

8 replies

I created a chat program without winsock and its works

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

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

sckMain.Accept(e.requestID)

how is it possible can you explain

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.

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!