Click to See Complete Forum and Search --> : [RESOLVED] New Problem Getting Peoples Nicknames
BladeZ
Jul 14th, 2005, 02:42 AM
my program is now running smoothly with very little errors now and even multiple connections but i was wondering in have a list box on my form and i was wondering how i can make everyone who is connected to my server show up in it
-BladeZ
Guiseppe
Jul 16th, 2005, 03:14 PM
Simply let the clients send their nicknames to the server when they connect, and then let the server send the whole bunch of nicknames to all the clients every time something changes.
Then let the client just go through the string or array and add all the names to a listbox.
Don't really see a problem.
BladeZ
Jul 16th, 2005, 08:47 PM
yes i know that but how can i check to see if the data that is coming into the server is a nickname or a msg
dglienna
Jul 16th, 2005, 11:23 PM
Add #xxxxx# to the front of the data. So that every message has a nickname attached to it. You'd have to change the client and server. Look at woka's msn client in codebank. See how it works.
BladeZ
Jul 16th, 2005, 11:30 PM
i do this is it look
Public Sub winsock_connect()
MsgBox "You Are Now Connected "
Winsock.SendData "N:" & frmlogin.txtnick.Text
txtchat.Text = " "
End Sub
Private Sub cmdSend_click()
Winsock.SendData frmlogin.txtnick.Text & ": " & txtmsg.Text
'txtchat.Text = txtchat.Text & vbNewLine & frmlogin.txtnick.Text & ": " & txtmsg.Text
txtmsg.Text = ""
end sub
server does this
If strRecivedData = "N:" Then
txtchat.Text = strRecivedData & " has joined chat."
server(SocketCheck).SendData ""
dglienna
Jul 17th, 2005, 12:14 AM
When the user connects, you could try this:
This will call the addit() sub, and pass the current username. The addit() sub will loop through the current contents of the listbox, and if the name isn't there, then it will add it. You might want to do something if the user changes their nickname, so that both don't appear. You'd have to remove the old name, and then add the current nickname. Just add a removeit() sub,
Declare a dynamic array and add list1 as a listbox
DIM arr() as string ' to split up the data
If strRecivedData = "N:" Then
txtchat.Text = strRecivedData & " has joined chat."
arr() = split(strRecivedData,":")
call addit(arr(0))
server(SocketCheck).SendData ""
end if
end sub
Sub addit(x As String)
Dim a As Integer
For a = 0 To List1.ListCount - 1
If x = List1.List(a) Then Exit Sub
Next a
List1.AddItem x
End Sub
dglienna
Jul 17th, 2005, 12:22 AM
I wrote this to test it. Try it by itself. It won't allow dups.
Option Explicit
Private Sub Form_Load()
Call addit("david")
Call addit("noteme")
Call addit("david")
Call addit("david")
Call addit("noteme")
Call addit("david")
Call addit("sam")
Call addit("david")
End Sub
Sub addit(x As String)
Dim a As Integer
For a = 0 To List1.ListCount - 1
If x = List1.List(a) Then Exit Sub
Next a
List1.AddItem x
End Sub
BladeZ
Jul 17th, 2005, 12:37 AM
for some reason its not adding the people to the list do i make it a array?
can you tell me how to check if there is a N: infront of it and if there is do addit
Private Sub Server_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strRecivedData As String
Dim SocketCheck As Integer
server(Index).GetData strRecivedData
txtchat.Text = txtchat.Text & vbNewLine & strRecivedData
If strRecivedData = "N:" Then
txtchat.Text = strRecivedData & " has joined chat."
arr() = Split(strRecivedData, ":")
Call addit(arr(0))
server(SocketCheck).SendData ""
End If
For SocketCheck = 0 To SocketCount Step 1
If server(SocketCheck).State = sckConnected Then
server(SocketCheck).SendData strRecivedData
DoEvents
End If
Next SocketCheck
End Sub
BladeZ
Jul 17th, 2005, 12:43 AM
i edited my previous post
dglienna
Jul 17th, 2005, 12:49 AM
Did you split the line like I did? And did you declare Arr() as String so that it is accesssible to the sub? If you have different forms, you will have to declare it in a module. You could put the subroutine in the same module, too.
Oops. Wait a sec. You'll only get "N" in the list. One time.
Change this line:
Call addit(arr(0))
to this
Call addit(arr(1))
:wave:
and mark your other thread as resolved, too
BladeZ
Jul 17th, 2005, 12:54 AM
ok thanks i will
dglienna
Jul 17th, 2005, 01:02 AM
So, it's ok, now?
BladeZ
Jul 17th, 2005, 01:13 AM
well sorta check the pm i sent you for full details
dglienna
Jul 17th, 2005, 02:56 PM
You are going to have to post your code. We'll look at it later on.
BladeZ
Jul 17th, 2005, 10:14 PM
k i will post the server and client
Server
Option Explicit
Dim incommingData As String
Private intMax As Long
Dim SocketCount As Integer
Dim TotalUsersConnected As Integer
Dim arr() As String
Private Sub Server_ConnectionRequest(Index As Integer, ByVal requestID As Long)
SocketCount = SocketCount + 1
Load server(SocketCount)
server(SocketCount).Accept requestID
TotalUsersConnected = TotalUsersConnected + 1
lblnum.Caption = "Total users connected: " & TotalUsersConnected - 1
End Sub
Private Sub Server_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strRecivedData As String
Dim SocketCheck As Integer
server(Index).GetData strRecivedData
txtchat.Text = txtchat.Text & vbNewLine & strRecivedData
If strRecivedData = "N:" Then
txtchat.Text = strRecivedData & " has joined chat."
arr() = Split(strRecivedData, ":")
Call addit(arr(0))
server(SocketCheck).SendData ""
End If
For SocketCheck = 0 To SocketCount Step 1
If server(SocketCheck).State = sckConnected Then
server(SocketCheck).SendData strRecivedData
DoEvents
End If
Next SocketCheck
End Sub
Private Sub cmdexit_Click()
End
End Sub
Private Sub form_Load()
server(0).LocalPort = 7777
server(0).Listen
timsave.Enabled = True
End Sub
Private Sub timsave_Timer()
Open "C:\ServerLog.txt" For Append As #1
Print #1, txtchat.Text
Close #1
End Sub
Sub addit(x As String)
Dim a As Integer
For a = 0 To lstusers.ListCount - 1
If x = lstusers.List(a) Then Exit Sub
Next a
lstusers.AddItem x
End Sub
Client
Option Explicit
Dim incommingData As String
Private Sub Form_Load()
frmchat.Visible = False
frmlogin.Visible = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
Winsock.Close
End Sub
Public Sub winsock_connect()
MsgBox "You Are Now Connected "
Winsock.SendData "N:" & frmlogin.txtnick.Text
txtchat.Text = " "
End Sub
Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
Dim strDataRecived As String
Winsock.GetData strDataRecived
DoEvents
txtchat.Text = txtchat.Text & strDataRecived & vbnewline
End Sub
Private Sub cmdSend_click()
Winsock.SendData frmlogin.txtnick.Text & ": " & txtmsg.Text
'txtchat.Text = txtchat.Text & vbNewLine & frmlogin.txtnick.Text & ": " & txtmsg.Text
txtmsg.Text = ""
End Sub
Private Sub cmdexit_click()
Winsock.Close
End
End Sub
Private Sub winsock_Close()
Dim sServer As String
sServer = "Server has been disconnected"
txtchat.Text = sServer & vbNewLine
Winsock.Close
MsgBox "Server is disconnected", vbCritical, "Closing Chat System"
End
End Sub
dglienna
Jul 18th, 2005, 03:33 PM
Can't you post the project(s)?
BladeZ
Jul 18th, 2005, 05:05 PM
Can't you post the project(s)?
ya i will dont mind some stuff thats in the client
dglienna
Jul 18th, 2005, 07:18 PM
I'll let Pino look at it tonight. If he doesn't answer, I'll try it tomorrow, although winsock isn't my strong point :)
BladeZ
Jul 18th, 2005, 07:28 PM
ok thanks i just hope he responds becuase i havnt seen him in awhile
Pino
Jul 19th, 2005, 02:28 PM
your missing frmpon in your client app....
pINO
BladeZ
Jul 19th, 2005, 02:32 PM
your missing frmpon in your client app....
pINO
just delete the refrneces to it has no use in the program
Pino
Jul 19th, 2005, 02:39 PM
hmmm.... whats sockect check is that also now obsolite?
Pino
Jul 19th, 2005, 02:44 PM
Ok i think you should have a good look at my Vb useful functions, it explains many of the strinfunctions
change your data arrival event in you server to this...
Private Sub Server_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strRecivedData As String
Dim SocketCheck As Integer
'Recive the message from the client
server(Index).GetData strRecivedData
txtchat.Text = txtchat.Text & vbNewLine & strRecivedData
'What this for statement does is go through all the winsocks
'that we have open and make sure that they are connected to
'a client. If they are then send the message to the client
If Left(strRecivedData, 2) = "N:" Then
txtchat.Text = strRecivedData & " has joined chat."
arr() = Split(strRecivedData, ":")
Call addit(arr(1))
End If
For SocketCheck = 0 To server.UBound - 1
'If the winsocks state is Connected then send the message
'to that client.
If server(SocketCheck).State = sckConnected Then
server(SocketCheck).SendData strRecivedData
DoEvents
End If
Next SocketCheck
End Sub
works ok, but i have to be honest some of the code is some what messy.
Anyhows that works now.
BladeZ
Jul 19th, 2005, 04:18 PM
pino that code works very well but now it sometimes doesnt sned text to all users eg. User 1 send HI User 2 Sees HI user 3 doesnt see anything
!!!EDIT!!! nvm got it working
Pino
Jul 20th, 2005, 02:49 AM
:) :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.