VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Amazing Winsock Chat Tutorial

by Yariv Sarafraz (2 Submissions)
Category: Internet/HTML
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (20 Votes)

This tutorial will walk you through the process of making a decent, and fully functional winsock chat. Extremely easy to comprehend, and made for newbies.

Rate Amazing Winsock Chat Tutorial


.


Winsock Chat Tutorial [Tutorial
by Yariv Sarafraz] 
First off, I've
seen many winsock chat tutorials and examples on psc, and most of them are
vague. This tutorial will walk you through the process of making a decent,
functional winsock chat. So let's get started ...


 We'll start with the client code.


Client Code
Requirements: Winsock
Control, 3 textboxes, 1 rich textbox, 1 label, and 2 command buttons. Let's get started ...

The
rich textbox will be the
chat room itself, so let's name it 'txtchat'.  We'll name the first textbox
'txtsend',
the second we'll name 'txtport', and the third we'll name 'txtip'. Name the label
'lblstatus'. Add the 2 command buttons, as well as, the winsock control to the form.


Private Sub Form_Load()
txtport.Text
= "616" 'Assigning a port

txtip.Text = "127.0.0.1"
End
Sub


Private Sub Winsock1_Connect()

lblstatus.Caption  = "Connected to Server!"  'Reporting
status

txtchat.Text = txtchat.Text & "*** Connection Complete Achieved ... ***" 
'Reporting status to chat

End Sub


Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

If Winsock1.State <> sckClosed Then Winsock1.Close 
'If the winsock control is in use, close it

Winsock1.Accept requestID  'Allow
connection

End Sub


Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

lblstatus.caption = "Error Occurred."  
'If an error occurs, report it to the status bar
Command2.Enabled =
True

End Sub


Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim Data As String

Winsock1.GetData Data   'Get
whatever data the server has sent to you (the client)

txtchat.Text = txtchat.Text & vbNewLine & "Server: " & vbTab & Data 
'Whatever
data we receive, place into the chat textbox, txtchat.Text.

End Sub


Private Sub txtchat_Change()

txtchat.SelStart = Len(txtchat.Text)  'This
one line of code will responds to the fact text is being sent to txtchat.Text,
and as text enters the textbox, it automatically scrolls down to the last line.
If you don't know what I'm talking about, just try the code and you'll
understand.

End Sub


'Make command button 1's
caption 'Send Text'
Private Sub Command1_Click()

Data = txtsend.Text

If
lblstatus.Caption  = "Connected to Server!"  Then  'If
the status bar states that you're connected, then ...

txtchat.Text = txtchat.Text & vbNewLine & "Client: " & vbTab &
txtsend.Text  'Add the text you typed to the chatbox

Winsock1.SendData (Data) 'And send the text you typed to
the server

txtsend.Text = "" 'Clear txtsend.Text

Else 'But! If the status label says anything but
'Connected to Server!', do the following

txtchat.Text = txtchat.Text & vbNewLine & "* Connection Lost/Not
Found." 'Send to the chat window an error message

End If
End Sub


'Make command button 2's
caption 'Connect to Server'
Private Sub Command2_Click()
If
txtip.Text = "" then  'If the IP
text box (txtip.Text) is empty, then do the following ...
Msgbox
"Please enter an IP number."  'Send
a message box to report the problem
Exit Sub
Else  
'But if the IP text box (txtip.Text) is anything but
empty, do the following ...
Command2.Enabled
= False

 Winsock1.Close  'Close any current connection
Winsock1.Connect
txtip.text, txtport.text  'Connect to the given IP and port.

lblstatus.Caption  = "Connecting ..."  'Report
status to status label
End Sub


Private Sub Form_Unload(Cancel As Integer)

On Error Resume Next  'If
an error were to occur, ignore it and keep working

Winsock1.SendData "Exit" 'See
the Winsock1_DataArrival for the server to understand what this is doing
here

Do Events 

Winsock1.Close  'Close
the connection

End  'Exit the program

End Sub


------------------------------:
Client code ends here! We now begin the Server code.


Server Code
Requirements: Winsock
Control, 1 textbox, 1 rich textbox, 1 label, and 2 command buttons. Let's get started ...

The
rich textbox will be the
chat room itself, so let's name it 'txtchat'.  We'll name the textbox
'txtsend'. Name the label
'lblstatus'. Add the 2 command buttons, as well as, the winsock control to the form.


'Make command button 1's
caption 'Listen for Connection'
Private Sub Command1_Click()

Winsock1.Close  'Close
any current winsock connection

Winsock1.LocalPort = 616  'Assigning
a port

Winsock1.Listen 'Listen for
connection

lblstatus.Caption = "Listening for connection ..." 
'Report status to status label


txtchat.Text = txtchat.Text & "*** Not Connected. Please stand by ... ***"  
'Report status to chat

End Sub


'Make command button 2's
caption 'Send Text'
Private Sub Command2_Click()

Data = txtsend.Text  'Define 'Data'

If
lblstatus.Caption  = "Connected."  Then  'If
the status bar states that you're connected, then ...

txtchat.Text = txtchat.Text & vbNewLine & "Server: " & vbTab &
txtsend.Text  'Add the text you typed to the chatbox

Winsock1.SendData (Data) 'And send the text you typed to
the client

txtsend.Text = "" 'Clear txtsend.Text

Else 'But! If the status label says anything but
'Connected.', do the following

txtchat.Text = txtchat.Text & vbNewLine & "* Connection Lost/Not
Found." 'Send an error message to the chat window

End If

End Sub


Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

If Winsock1.State <> sckClosed Then Winsock1.Close 
'If the winsock control is in use, close it

Winsock1.Accept requestID  'Allow
connection
lblstatus.Caption = "Connected."

End Sub


Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

lblstatus.caption = "Error Occurred."  
'If an error occurs, report it to the status bar

End Sub


Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim Data As String

Winsock1.GetData Data  'Get
the data sent by the client

If Data = "Exit" Then 'If
the data string is 'Exit' then do the following ...

Winsock1.Close 'Close the winsock connection

lblstatus.Caption = "Connection Forcefully Ended." 'The
status bar will display the fact that the server is disconnected

Else 'But, If the data string is anything but
'Exit', then ...

txtchat.Text = txtchat.Text & vbNewLine & "Client: " & vbTab & Data 
'Add data to txtchat.Text (the chatroom)

End If

End Sub


Private Sub txtchat_Change()

txtchat.SelStart = Len(txtchat.Text)  'This
one line of code will responds to the fact text is being sent to txtchat.Text,
and as text enters the textbox, it automatically scrolls down to the last line.
If you don't know what I'm talking about, just try the code and you'll
understand.

End Sub




-----------------------------------------------------------
That's it! You're
done. It's really not that complicated.


I am currently working on a RAT (Remote Admin Tool), and if you have any
knowledge concerning the programming of the 'Edit Server' part of a RAT, please
contact me, at:  [email protected],
or ICQ# 164953049


[  Enjoy the tutorial
-  Yariv Sarafraz  
]


Download this snippet    Add to My Saved Code

Amazing Winsock Chat Tutorial Comments

No comments have been posted about Amazing Winsock Chat Tutorial. Why not be the first to post a comment about Amazing Winsock Chat Tutorial.

Post your comment

Subject:
Message:
0/1000 characters