Results 1 to 18 of 18

Thread: Winsock Help

  1. #1

    Thread Starter
    Member EnYcE's Avatar
    Join Date
    Sep 2005
    Posts
    33

    Winsock Help

    Hi, I was working on a chat program with multiple connections where every client connects to one server which is always running. But, I could not get the messages sent from the server to the client since they are different programs.

    I've attached my client code and my server code if someone wants to help. (The 2 codes are not long btw.)

    Thanks,
    EnYcE
    Attached Files Attached Files

  2. #2
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Winsock Help

    VB Code:
    1. Option Explicit
    2. Dim Index As Integer
    3. Dim intConnection As Integer
    4.  
    5. Private Sub Form_Load()
    6. Winsock(0).LocalPort = 10127
    7. Winsock(0).Listen
    8. End Sub
    9.  
    10. Private Sub Winsock_ConnectionRequest(Index As Integer, ByVal RequestID As Long)
    11. intConnection = intConnection + 1
    12. Load Winsock(intConnection)
    13. Winsock(intConnection).Accept RequestID
    14. End Sub
    15.  
    16. Private Sub Winsock_DataArrival(intConnection As Integer, ByVal bytesTotal As Long)
    17. Dim strData As String
    18. Winsock(intConnection).GetData strData
    19. End Sub

    But, I could not get the messages sent from the server to the client since they are different programs.
    You don't have any code to send messages to the client.

    -Sir Loin

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Winsock Help

    Ok, this part of you code, I'm not sure if it would work.

    VB Code:
    1. Private Sub Winsock_DataArrival(intConnection As Integer, ByVal bytesTotal As Long)
    2. Dim strData As String
    3. Winsock(intConnection).GetData strData
    4. End Sub

    Change to

    VB Code:
    1. Private Sub Winsock_DataArrival(index As Integer, ByVal bytesTotal As Long)
    2. Dim strData As String
    3. Winsock(index).GetData strData
    4. 'display your data????
    5. msgbox strData
    6. End Sub

    To send data to all connected clients,

    VB Code:
    1. For i = 0 to winsock.ubound - 1
    2.     if winsock(i).state = sckConnected then
    3.             winsock(i).senddata("YOUR DATA HERE")
    4.     end if
    5.    doevents
    6. Next I

    Hope that helps!

  4. #4

    Thread Starter
    Member EnYcE's Avatar
    Join Date
    Sep 2005
    Posts
    33

    Re: Winsock Help

    Where do I put this under? :

    For i = 0 to winsock.ubound - 1
    if winsock(i).state = sckConnected then
    winsock(i).senddata("YOUR DATA HERE")
    end if
    doevents
    Next I
    Thanks,
    EnYce

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Winsock Help

    where ever you want to sendata

  6. #6

    Thread Starter
    Member EnYcE's Avatar
    Join Date
    Sep 2005
    Posts
    33

    Re: Winsock Help

    What do you mean? Since server and client are 2 different programs and I want to send the messages to the client then how will I put that under server? There's no way of saying send data to a msgbox in a different program.....unless you know how?

    Thanks,
    EnYcE

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Winsock Help

    Do you understadn waht winsock is :lol?

    Have a look at the basic guide in my sig, then we can move on

  8. #8

    Thread Starter
    Member EnYcE's Avatar
    Join Date
    Sep 2005
    Posts
    33

    Re: Winsock Help

    o_O...... Ya i do.....i can do 2-way chat as your tut explained. Now I just dont get this ubound and lbound stuff that I read in places.....

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Winsock Help

    Right ok,

    Basicly your client application will not change whatsoever!

    Heres how we will set up your chat application,

    Server listens with Winsock(0), when clients connects it loads a new winsock and connects to the client Winsock(x).

    Now then the client sends chat data to the server just liek in your 2 way chat, and the server acts as a big trampoline (haha) and all it does is rebounds the text to all of the connected clients.

    so....

    In your servers dataarrival event,

    VB Code:
    1. Private Sub Winsock_DataArrival(index As Integer, ByVal bytesTotal As Long)
    2. Dim strData As String
    3. Winsock(index).GetData strData
    4.  
    5. For i = 0 to winsock.ubound - 1
    6.     if winsock(i).state = sckConnected then
    7.             winsock(i).senddata strData
    8.     end if
    9.    doevents
    10. Next I
    11.  
    12. end sub

    Make any more sence? I have a project I wrote a while ago i'lls ee if I can find it for you

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Winsock Help

    Just to add,

    UBound is absicly the number of winsock you have in your control array (The number of clients that have connected)

    LBound is the bottom value in the winsock array

    Pino

  11. #11

    Thread Starter
    Member EnYcE's Avatar
    Join Date
    Sep 2005
    Posts
    33

    Re: Winsock Help

    Ok thanks, this does make more sense. I'll wait for that project you made.

  12. #12

    Thread Starter
    Member EnYcE's Avatar
    Join Date
    Sep 2005
    Posts
    33

    Re: Winsock Help

    Ok 2 problems......

    1. I can see my friend's msgs but he cant see mine.........
    2. The msgs come up twice. For example, i type "HELLO" and it comes up twice...

    ANy idea what's wrong?

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Winsock Help

    Ok i can only find the server, but it should help you out

    1. I can see my friend's msgs but he cant see mine.........
    Are you both using a client program?


    2. The msgs come up twice. For example, i type "HELLO" and it comes up
    this is because in your client application you are sending the text to the window as well as sending it to the server,

    Post me your application
    Attached Files Attached Files

  14. #14
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Winsock Help

    ok well i've got to go now if you want post your project and i'll fix it up good for you. and post it back tommorow

  15. #15

    Thread Starter
    Member EnYcE's Avatar
    Join Date
    Sep 2005
    Posts
    33

    Re: Winsock Help

    yes we are both using the clients. Here are the attachments. And Thank for everything. I Included Server and Client. Should be no prob opening it.

    Thanks,
    EnYcE
    Attached Files Attached Files

  16. #16
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Winsock Help

    Ok think i've sorted it for you

    Let me know if you have any problems
    Attached Files Attached Files

  17. #17

    Thread Starter
    Member EnYcE's Avatar
    Join Date
    Sep 2005
    Posts
    33

    Re: Winsock Help

    OmG THANKS! It works! But 1 question...

    1. You put that I should change the listbox thing. How can I put it so when someone connects it shows the nickname in the listbox? I really can't figure that out.

    Thanks for everything!,
    EnYcE

  18. #18
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Winsock Help

    Ok, well the way I do things like this is when a client connects to the server you send a command like,

    "!,NickNameHere"

    Then when the server recieves data it checks to see if the left most character is a ! if it is, we have a new user and the server send sit to the clients

    It gets a bit complex but look at the server example i posted (look at my data arrival event)

    Pino

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width