Visual Basic 6.0 - Winsock Chat server and client.

This tutorial is how to make a CHAT APPLICATION - CLIENT + SERVER

image: client+server

Server code (commented) >

' ###########################################################################
' ++[[COMMENTS]]++
' Special thanks to Steven Dorman, Rob Carr and my GF!
' ++[[COMMENTS OVER]]++
###########################################################################



Public old As String ' old as string (old is last msg)
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long ' sound
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer ' keystate (vbKeyReturn) simiulates the enter key, for pressing enter to send message
Private Sub Command1_Click()
On Error Resume Next ' resume if error
Dim a As String ' a dimmed as string
old = outtext.Text ' old = outtext
a = "<" & nick.Text & "> " & outtext.Text ' defining what a is, nick.text and outtext(text message)
sock.SendData a ' sending a (above) as data SendData
DoEvents ' Do the events above
chat.AddItem "<" & nick.Text & "> " & outtext.Text ' The chat window will add the item nick.text and the text message (outtext.text)
outtext.Text = "" ' outtext = blank
If chat.ListCount = 25 Then ' if the chat box gets full then
chat.Clear ' clear the chat box
End If
End Sub
Private Sub Command2_Click()
chat.Clear ' clear the chat box
End Sub
Private Sub Command3_Click()
Dim a As String ' a dimmed as string
a = InputBox("Enter a note", "Note") ' inputbox will popup for add item
chat.AddItem a ' add item (a) which is the input box
End Sub
Private Sub Command4_Click()
On Error Resume Next
Label2.Caption = "No one is connected" ' sets the label2 as no one is connected
hip.Caption = "0.0.0.0" ' the hip caption is set to 9
Dim a, b As String 'dim a, b as string
a = "User has disconected from chat" ' a = user disconnected
sock.SendData a ' send data (above) (a)
DoEvents ' do events above
sock.Close ' close the winsock connection
b = InputBox("Enter Port of the server", "Connection settings") ' displays input box for the port to use
sock.LocalPort = b ' gets the port input from (B) above
sock.Listen ' listens for connections using winsock (called sock)
End Sub
Private Sub Command5_Click()
Open "C:\GeekSpotServer.nfo" For Output As #1 ' open .nfo file
Print #1, nick.Text ' save the nick.text to the .nfo file
Close #1 ' close and save the .nfo file
MsgBox "Name successfully changed, saved .nfo" ' display message box saved!
End Sub
Private Sub Command6_Click()
outtext.Text = old ' sets the last message in the message box
End Sub
Private Sub Command7_Click()
On Error Resume Next
Dim a As String ' a dimmed as string
old = outtext.Text ' old = outtext.text
a = "<" & outtext.Text & "> " ' a = message < message >
sock.SendData a ' send data (a) above which is the message
DoEvents ' do events above
chat.AddItem "<" & outtext.Text & "> " ' additem to the chat box/window
outtext.Text = "" ' outtext is set to blank
If chat.ListCount = 25 Then ' if the chat window gets full then
chat.Clear ' clear the chat window
End If
End Sub
Private Sub Form_Load()
On Error Resume Next
Dim b As String ' dim b as string
b = InputBox("Enter Port of the server", "Connection settings") ' input box for port
sock.LocalPort = b ' localport = (B) that is set above
sock.Listen ' listen for connection on the port (B)
yip.Caption = sock.LocalIP ' caption of yip is set to the local IP
Dim a As String ' dim a as tring
Open "C:\GeekSpotServer.nfo" For Input As #1 ' open .nfo
Input #1, a ' input a into .nfo
Close #1 ' close and save .nfo file
nick.Text = a ' nick.text is (a)
If App.PrevInstance Then ' if the application is already open display
MsgBox "GeekSpot Server running already!" ' this message box
End If
End Sub
Private Sub Form_Terminate()
On Error Resume Next
Dim a As String ' a dimmed as string
a = "User has disconected from chat" ' (a) is disconnected message
sock.SendData a ' send data (a) the disconnected message
DoEvents ' do the events above
sock.Close ' close winsock connection to server
End
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
Dim a As String ' (a) dimmed as string
a = "User has disconected from chat" ' (a) is the disconnected message
sock.SendData a ' send (a) disconnected message
DoEvents ' do events above
sock.Close ' close the winsock connection
Variable = sndPlaySound(App.Path & "\logoff.wav", 1) ' play the sound logoff.wav, once
End
End Sub
Private Sub outtext_KeyPress(KeyAscii As Integer)
If GetAsyncKeyState(vbKeyReturn) Then ' if enter key is pressed
Command1_Click ' click command1 which is SEND MESSAGE (sndmsg)
End If
End Sub
Private Sub sock_ConnectionRequest(ByVal requestID As Long)
If sock.State <> sckClosed Then sock.Close ' if winsock state = more than or less than close then close the connection
sock.Accept requestID ' accept the request id
Label2.Caption = "CONNECTED" ' label12 will display connected
chat.AddItem "connected" ' add connected to the chat window
hip.Caption = sock.RemoteHostIP ' caption of hip is displayed as the remote IP
Variable = sndPlaySound(App.Path & "\alert.wav", 1) ' play sound alert.wav once
End Sub
Private Sub sock_DataArrival(ByVal bytesTotal As Long)
Dim dat As String ' (dat) dimmed as string
sock.GetData dat ' get data (dat)
If dat = "User has disconected from chat" Then ' if dat = disconnected message
chat.AddItem dat ' add the above (dat)
Variable = sndPlaySound(App.Path & "\logoff.wav", 1) ' play sound logoff.wav once
Else ' otherwise
chat.AddItem dat ' chatt additem (dat)
Variable = sndPlaySound(App.Path & "\type.wav", 1) ' play sound type.wav once
End If
End Sub

[ / SERVER CODE ]

[CLIENT CODE]

Private Sub Command1_Click()
On Error Resume Next
Form2.Show
Dim a, b As String
a = InputBox("Enter IP of the server", "Connection settings")
b = InputBox("Enter Port of the server", "Connection settings")
Form2.sock.Connect a, b
Me.Hide
End Sub

Private Sub Command2_Click()
MsgBox "Example by GeekSpot", vbMsgBoxRight, "That's right"
MsgBox "Thanks to Steven Dorman", vbMsgBoxRight, "Thanks mate"
End Sub

Private Sub Command3_Click()
End
End Sub


Private Sub Form_Load()

End Sub

Public old As String ' old as string (old is last msg)
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long ' sound
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer ' keystate (vbKeyReturn) simiulates the enter key, for pressing enter to send message

Private Sub Command1_Click()
On Error Resume Next ' resume if error
Dim a As String ' a dimmed as string
old = outtext.Text ' old = outtext
a = "<" & nick.Text & "> " & outtext.Text ' defining what a is, nick.text and outtext(text message)
sock.SendData a ' sending a (above) as data SendData
DoEvents ' Do the events above
chat.AddItem "<" & nick.Text & "> " & outtext.Text ' The chat window will add the item nick.text and the text message (outtext.text)
outtext.Text = "" ' outtext = blank
If chat.ListCount = 25 Then ' if the chat box gets full then
chat.Clear ' clear the chat box
End If
End Sub

Private Sub Command2_Click()
chat.Clear ' clear the chat window
End Sub

Private Sub Command3_Click()
Dim a As String ' a dimmed as string
a = InputBox("Enter a note", "Note") ' inputbox will popup for add item
chat.AddItem a ' add item (a) which is the input box
End Sub

Private Sub Command4_Click()
On Error Resume Next
Dim a As String ' a dimmed as string
a = "User has disconected from chat" ' a = disconnected message
sock.SendData a ' send data to server (a) which is disconnected message

DoEvents ' do events above
sock.Close ' close the connection to the server (sock.close)



Dim c, b As String ' c is dimmed as string
c = InputBox("Enter the IP to connect to", "Connection settings") ' c = inputbox which will be the IP to connect to
b = InputBox("Enter the Port to use", "Connection settings") ' b = inputbox which will be the port that is connected to
Form2.sock.Connect c, b ' calling form2 and connecting using the winsock component, named sock. using inputbox IP (c) and the port (B)

End Sub

Private Sub Command5_Click()
Open "C:\GeekSpot.nfo" For Output As #1 ' opens the .nfo file saved in c:/ for ouput as #1
Print #1, nick.Text ' prints the nick.text to the .nfo file
Close #1 ' closes the text file
MsgBox "Name successfully changed, saved .nfo!" ' displays message box as saved
End Sub
Private Sub Command6_Click()
outtext.Text = old ' will set the outtext.text to last message
End Sub

Private Sub Command7_Click()
On Error Resume Next
Dim a As String ' a is dimmed as string
old = outtext.Text ' old = what outtext.text is
a = "<" & outtext.Text & "> " ' a = the message sent including < MESSAGE >
sock.SendData a ' winsock sending data (a) which is above
DoEvents ' do events above
chat.AddItem "<" & outtext.Text & "> " ' adds the outtext.text to the chat window, using additem
outtext.Text = "" ' outtext is blank
If chat.ListCount = 25 Then ' if the chat window gets full then
chat.Clear ' clear the chat window
End If
End Sub

Private Sub Form_Load()
On Error Resume Next
Dim a As String ' a is dimmed as string
Open "C:\GeekSpot.nfo" For Input As #1 ' opens the .nfo as #1
Input #1, a ' types (a) which is outtext to the .nfo file
Close #1 ' closes the .nfo tile
nick.Text = a ' the nick.text is set to the out text, which is blank (upon form loading)

If App.PrevInstance Then ' if the application is already open


MsgBox "GeekSpot chat is already running!" ' display the message box with the text saying it's open
End If

End Sub

Private Sub Form_Terminate()
On Error Resume Next
Dim a As String ' a is dimmed as string
a = "User has disconected from chat" ' (a) is the user has disconnected from chat
sock.SendData a ' senddata from winsock client to server (a) which is disconnect from chat

DoEvents ' do the events above
sock.Close ' close the winsock connection to the server
End
End Sub

Private Sub Form_Unload(Cancel As Integer) ' form unloads and cancels as number (integer)
On Error Resume Next
Dim a As String ' as is dimmed as string
a = "User has disconected from chat" ' when the use exits/unloads the form (a) is disconnected message then
sock.SendData a ' send (a) to server

DoEvents ' do above events
sock.Close ' close winsock connectiont o server
Variable = sndPlaySound(App.Path & "\logoff.wav", 1) ' play the sound logoff.wav
End
End Sub

Private Sub outtext_KeyPress(KeyAscii As Integer)
If GetAsyncKeyState(vbKeyReturn) Then ' if the enter key is pressed it will simulate the vbKeyReturn
Command1_Click ' clicks the command1 which is the send button on the form
End If
End Sub

Private Sub sock_DataArrival(ByVal bytesTotal As Long)
Dim dat As String ' dim dat as string
sock.GetData dat ' getting the data of (dat)
If dat = "User has disconected from chat" Then ' if (dat) = the message
chat.AddItem dat ' add item (dat) to the chat window
Variable = sndPlaySound(App.Path & "\logoff.wav", 1) ' play the sound logoff.wav
Else ' otherwise
chat.AddItem dat ' chat window adds (dat)
Variable = sndPlaySound(App.Path & "\type.wav", 1) ' play type.wav
End If
End Sub
[/ CLIENT CODE]

Download PROJECT FILE (REQUIRES REGISTRATION AT MY WEBSITE)

http://www.geekspot.co.uk/index.php?app=core&module=attach&section=attach&attach_id=1
Comments
2
Back to top