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
Printable View
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
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.
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
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.
i do this is it look
VB Code:
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
VB Code:
If strRecivedData = "N:" Then txtchat.Text = strRecivedData & " has joined chat." server(SocketCheck).SendData ""
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
VB Code:
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
I wrote this to test it. Try it by itself. It won't allow dups.
VB Code:
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
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
VB Code:
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
i edited my previous post
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:
to thisVB Code:
Call addit(arr(0))
VB Code:
Call addit(arr(1))
:wave:
and mark your other thread as resolved, too
ok thanks i will
So, it's ok, now?
well sorta check the pm i sent you for full details
You are going to have to post your code. We'll look at it later on.
k i will post the server and client
Server
VB Code:
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
VB Code:
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
Can't you post the project(s)?
ya i will dont mind some stuff thats in the clientQuote:
Originally Posted by dglienna
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 :)
ok thanks i just hope he responds becuase i havnt seen him in awhile
your missing frmpon in your client app....
pINO
Quote:
Originally Posted by Pino
just delete the refrneces to it has no use in the program
hmmm.... whats sockect check is that also now obsolite?
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...
VB Code:
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.
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
:) :)