This is how you make a basic Client/Server Chat
(Server Side)
For this code you require
- 2 Textboxes (You can call them anything you want but i'm going to call them txthst (Chat history) txtmsg (Chat Message))
- 1 Command Button (You can call it what ever you want but i'm going call it cmdsnd (To Send Messages))
- 1 Winsock Control (You can call it what ever you want but i'm going to call it ws (To listen))
MainFrm.frm
Menu
- Exit
CODE
Private Sub Form_Load()
ws.LocalPort = 12345
ws.Listen
End Sub
Private Sub Winsock_ConnectionRequest(ByVal RequestID As Long)
ws.Close
ws.Accept RequestID
End Sub
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
ws.GetData strData
txthst.Text = txthst.Text & vbCrLf & strData
txthst.SelStart = Len(txthst.Text)
End Sub
Private Sub cmdsnd_Click()
Winsock.SendData txtChat.Text
DoEvents
txthst.Text = txthst.Text & vbCrLf & txtmsg.Text
txtmsg.Text = ""
End Sub
(Client Side)
For this code you require
- 2 Textboxes (You can call them anything you want but i'm going to call them txthst (Chat history) txtmsg (Chat Message))
- 1 Command Button (You can call it what ever you want but i'm going call it cmdsnd (To Send Messages))
- 1 Winsock Control (You can call it what ever you want but i'm going to call it ws (To Connect to the server))
CODE
Private Sub Form_Load()
ws.RemoteHost = "127.0.0.1"
ws.RemotePort = 12345
ws.Connect
End Sub
Private Sub cmdSend_Click()
ws.SendData txtmsg.Text
DoEvents
txthst.Text = txthst.Text & vbCrLf & txtmsg.Text
txtmsg.Text = ""
End Sub
Private Sub Winsock_Connect()
MsgBox "Connected"
End Sub
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
ws.GetData strData
txthst.Text = txtmsg.Text & vbCrLf & strData
txthst.SelStart = Len(txthst.Text)
End Sub
Private Sub Winsock_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)
MsgBox "Error: " & Description
End Sub
There you go people and entier tutorial on a basic person to person chat system using winsock and you can expand on this in many different ways. Example adding images to show the different states of the winsock control. (I may show this later)


