I've designed an application for use as a chat system to ease the communication between friends at work, taking this opportunity to learn VB programming.
So here goes:
my application is designed as a client - server utility, ergo there are two parts: a client and a server.
I've included in each of the forms a winsock control, have set the localport on the server to be 666, and the remoteport on the client to be 666, I've set the server to listen and so far it works just fine. But only for ONE client. So, naturally, I was looking for a way to enable multiple connections to the same server, because that is what I want my application to be, a chat environment.
Ok, in order to allow multiple connections to the same server, I've set the index property of the server's winsock control to 0 (zero), and on the "incoming call", this socket loads another instance of itself, with another index, such as 1, and so on. Well, so far so good...
But THE PROBLEM really is that when I connect any second client, the text string send from the first client to the server does not show in the server's textbox, altough it should... here is what I did: I compiled and made .EXEs both out of the client only, and out of the client-server together. Code follows, so if anyone can think why do I have this problem, I would be in debt if I get any tips how to solve it, or work around it... Thank you, in advance, ARES.
Code
==========================================
- for the CLIENT:
==========================================
Private Sub Command1_Click()
If tcpClient.State = sckConnected Then
tcpClient.SendData Text3.Text & ":" & Text2.Text
Text1.Text = Text3.Text & ":" & Text2.Text & vbCrLf & Text1.Text & vbCrLf
Text2.Text = ""
Else:
MsgBox "There is no connection.", vbCritical + vbOKOnly, "Connect first"
End If
End Sub
Private Sub Command2_Click()
Frame3.Caption = "Nick: " & Text3.Text
nick = Text3.Text
End Sub
Private Sub Command3_Click()
If tcpClient.State <> sckClosed Then
MsgBox "You're already connected", vbOKOnly + vbCritical, "Already connected"
Else:
tcpClient.RemoteHost = Text4.Text
tcpClient.RemotePort = 666
tcpClient.Connect
client.Caption = "Jn Chat Client. Status: " & tcpClient.State
client.Refresh
End If
End Sub
Private Sub Form_Load()
' The name of the Winsock control is tcpClient.
' Note: to specify a remote host, you can use
' either the IP address (ex: "121.111.1.1" or
' the computer's "friendly" name, as shown here.
nick = tcpClient.LocalHostName
client.Caption = "Jn Chat Client. Status: " & tcpClient.State
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
tcpClient.GetData strData
Text1.Text = strData & vbCrLf & Text1.Text
End Sub
===========================================
end code for the client app.
===========================================
Code
====================================
-for the SERVER
====================================
Private nick As String
Private intMax As Long
Private Sub Command1_Click()
If tcpServer(intMax).State <> sckConnected Then
MsgBox "Nu este nici un client conectat. Aveti nevoie de ajutor profesional, caci ati inceput sa vorbiti singur.", vbCritical + vbOKOnly, "Awaiting clients"
Else:
tcpServer(intMax).SendData nick & ":" & Text2.Text
Text1.Text = Text3.Text & ":" & Text2.Text & vbCrLf & Text1.Text
Text2.Text = ""
End If
End Sub
Private Sub Command2_Click()
Frame3.Caption = "Nick: " & Text3.Text
nick = Text3.Text
End Sub
Private Sub Command3_Click()
Text1.Text = List1.Text & vbCrLf & Text1.Text
End Sub
Private Sub Form_Load()
intMax = 0
tcpServer(0).LocalPort = 666
tcpServer(0).Listen
client.Show
Frame3.Caption = "Nick: " & tcpServer(0).LocalHostName
Text3.Text = "SERVER"
nick = "SERVER"
End Sub
Private Sub tcpServer_ConnectionRequest _
(index As Integer, ByVal requestID As Long)
If index = 0 Then
intMax = intMax + 1
Load tcpServer(intMax)
tcpServer(intMax).LocalPort = 666
tcpServer(intMax).Accept requestID
'Load txtData(intMax)
End If
'If tcpServer.State <> sckClosed Then tcpServer.Close
'tcpServer.Accept requestID
List1.AddItem tcpServer(intMax).RemoteHost
End Sub
Private Sub tcpServer_DataArrival(index As Integer, ByVal bytesTotal As Long)
Dim strData As String
tcpServer(intMax).GetData strData
Text1.Text = strData & vbCrLf & Text1.Text
End Sub
===========================================
end code for the SERVER
===========================================
I hope you guys can help me, because I really want to learn VB, and this is my bottleneck. My brain isn't thinking correctly right now, so... I'll get back at it tomorrow.
So here goes:
my application is designed as a client - server utility, ergo there are two parts: a client and a server.
I've included in each of the forms a winsock control, have set the localport on the server to be 666, and the remoteport on the client to be 666, I've set the server to listen and so far it works just fine. But only for ONE client. So, naturally, I was looking for a way to enable multiple connections to the same server, because that is what I want my application to be, a chat environment.
Ok, in order to allow multiple connections to the same server, I've set the index property of the server's winsock control to 0 (zero), and on the "incoming call", this socket loads another instance of itself, with another index, such as 1, and so on. Well, so far so good...
But THE PROBLEM really is that when I connect any second client, the text string send from the first client to the server does not show in the server's textbox, altough it should... here is what I did: I compiled and made .EXEs both out of the client only, and out of the client-server together. Code follows, so if anyone can think why do I have this problem, I would be in debt if I get any tips how to solve it, or work around it... Thank you, in advance, ARES.
Code
==========================================
- for the CLIENT:
==========================================
Private Sub Command1_Click()
If tcpClient.State = sckConnected Then
tcpClient.SendData Text3.Text & ":" & Text2.Text
Text1.Text = Text3.Text & ":" & Text2.Text & vbCrLf & Text1.Text & vbCrLf
Text2.Text = ""
Else:
MsgBox "There is no connection.", vbCritical + vbOKOnly, "Connect first"
End If
End Sub
Private Sub Command2_Click()
Frame3.Caption = "Nick: " & Text3.Text
nick = Text3.Text
End Sub
Private Sub Command3_Click()
If tcpClient.State <> sckClosed Then
MsgBox "You're already connected", vbOKOnly + vbCritical, "Already connected"
Else:
tcpClient.RemoteHost = Text4.Text
tcpClient.RemotePort = 666
tcpClient.Connect
client.Caption = "Jn Chat Client. Status: " & tcpClient.State
client.Refresh
End If
End Sub
Private Sub Form_Load()
' The name of the Winsock control is tcpClient.
' Note: to specify a remote host, you can use
' either the IP address (ex: "121.111.1.1" or
' the computer's "friendly" name, as shown here.
nick = tcpClient.LocalHostName
client.Caption = "Jn Chat Client. Status: " & tcpClient.State
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
tcpClient.GetData strData
Text1.Text = strData & vbCrLf & Text1.Text
End Sub
===========================================
end code for the client app.
===========================================
Code
====================================
-for the SERVER
====================================
Private nick As String
Private intMax As Long
Private Sub Command1_Click()
If tcpServer(intMax).State <> sckConnected Then
MsgBox "Nu este nici un client conectat. Aveti nevoie de ajutor profesional, caci ati inceput sa vorbiti singur.", vbCritical + vbOKOnly, "Awaiting clients"
Else:
tcpServer(intMax).SendData nick & ":" & Text2.Text
Text1.Text = Text3.Text & ":" & Text2.Text & vbCrLf & Text1.Text
Text2.Text = ""
End If
End Sub
Private Sub Command2_Click()
Frame3.Caption = "Nick: " & Text3.Text
nick = Text3.Text
End Sub
Private Sub Command3_Click()
Text1.Text = List1.Text & vbCrLf & Text1.Text
End Sub
Private Sub Form_Load()
intMax = 0
tcpServer(0).LocalPort = 666
tcpServer(0).Listen
client.Show
Frame3.Caption = "Nick: " & tcpServer(0).LocalHostName
Text3.Text = "SERVER"
nick = "SERVER"
End Sub
Private Sub tcpServer_ConnectionRequest _
(index As Integer, ByVal requestID As Long)
If index = 0 Then
intMax = intMax + 1
Load tcpServer(intMax)
tcpServer(intMax).LocalPort = 666
tcpServer(intMax).Accept requestID
'Load txtData(intMax)
End If
'If tcpServer.State <> sckClosed Then tcpServer.Close
'tcpServer.Accept requestID
List1.AddItem tcpServer(intMax).RemoteHost
End Sub
Private Sub tcpServer_DataArrival(index As Integer, ByVal bytesTotal As Long)
Dim strData As String
tcpServer(intMax).GetData strData
Text1.Text = strData & vbCrLf & Text1.Text
End Sub
===========================================
end code for the SERVER
===========================================
I hope you guys can help me, because I really want to learn VB, and this is my bottleneck. My brain isn't thinking correctly right now, so... I'll get back at it tomorrow.