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

How to make a simple chat program in visual basic using the Microsoft Winsock Control
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

How to make a simple chat program in visual basic using the Microsoft Winsock Control
How to make a simple chat program in visual basic using the Microsoft Winsock Control
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

How to make a simple chat program in visual basic using the Microsoft Winsock Control
we are now going to be changing the captions(what is says in the command buttons)1. click command12. look on the right side in the properties and find Caption3. change it to Hostdo the same for the rest but make them:command2 = Connectcommand3 = Closecommand4 = Sendnow we are going to change what the text boxes say1. click on the text box that says text12. go to the properties window and find the Text property3. make it blank4. do the same for the one that says Text25. now click text3 and change the text property to 127.0.0.16. now click text4 and change the text property to 12347. now click text5 and change the text property to Name127.0.0.1 is your local ip so you can test the program on your computer when its done1234 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

How to make a simple chat program in visual basic using the Microsoft Winsock Control
How to make a simple chat program in visual basic using the Microsoft Winsock Control
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

How to make a simple chat program in visual basic using the Microsoft Winsock Control
now it is time to write the code for the command buttonslets start with the first one cmdHost1. double click it, this will bring up the code window2. it should look like thisPrivate Sub cmdHost_Click()End Sub3. enter this codesckMain.LocalPort = txtPort.textsckMain.listennow 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 connectnow close that and double click on cmdConnectenter this codesckMain.RemoteHost = txtIP.textsckMain.RemotePort = txtPort.textsckMain.Connectthe first two lines change the RemoteHost and the RemotePort of the winsock (sckMain) to the ip and port we wantthe sckMain.Connect tells it to connect to someone listening on that port and ipnow for the close buttondouble click on cmdClose and typesckMain.closetxtLog = txtLog.text & "Connection closed!" & vbCrlfthe sckMain.close tells the winsock to disconnectthe second line adds Connection closed to txtLogdouble click on cmdSend and typesckMain.senddata "[" & txtName.text & "]" & txtSend.texttxtLog = txtLog.text & "[" & txtName.text & "]" & txtSend.text & vbcrlftxtSend.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 codefirst line sends the text in txtSend and your name in txtNamesecond line adds the message you sent and your name to txtLogthird line clears the text in txtSend

Step 6: Coding the Winsock

How to make a simple chat program in visual basic using the Microsoft Winsock Control
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 receivesok double click on the winsock control(sckMain)in the upper right corner of the code box it should say: errorclick the drop down box and select connection requestnow it should have made a new place to enter codein there type:If sckMain.state sckClose thensckMain.closeend ifsckMain.accept requestidtxtLog = txtLog.text & "Connected to: " & sckMain.RemoteHostIP & vbcrlffirst and second lines check if the winsock(sckMain) is closed and if it is not then it closes itthird line closes the if statementforth line tells the winsock (sckMain) to accept connectionsfifth line tells you that you are connected by adding a line to txtLognow we need to tell the winsock what to do with the data it recievesin the top right of the code window click the drop down box and select: DataArrivalnow enter this code:dim Data as stringsckMain.getdata Data, vbstringtxtLog = txtLog.text & data & vbcrlffirst line makes data a string the sting is where the data that is received is going to be put before it is added to txtLogsecond line gets the data and places it in the string data as a vbstringthird line adds the data received to txtLognow once again if it is confusing and you have questions post comment or message me

Step 7: Using the program/Conclusion

How to make a simple chat program in visual basic using the Microsoft Winsock Control
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
 
 

Tag cloud

make build easy simple arduino making homemade solar laser printed portable cheap mini building custom cardboard wooden create super lego turn paracord chocolate your paper light intel