Results 1 to 27 of 27

Thread: Winsock help:

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Winsock help:

    Okay, ive never used winsock before..But i have a tictactoe game and im wondering how i could make it mulitplayer. I have an array of 9 labels, id just like to know how i could send the data to the other player what label and what letter to select. I think i could get the rest from there.

    Thanks

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Winsock help:

    Are they in an array? I'd just send (r,c,0) or (r,c,1). Have each side send over a WINNER message to the other side. Do you have Pino's Winsock sample?
    Get it runing on your computer first and see how it works.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    no i dont have his example. can i have a link?

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Winsock help:

    I don't have a link, but I downloaded it the other day. Just run each program in its own IDE at the same time. Make sure you unzip it into two separate folders (make sure the Use Folder Names button is checked)

    This is PINO's code.
    Attached Files Attached Files

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    wait, so i have to run two different programs to get it to work? Theres no way that it can be both server and client(i guess?). Id really like to have one .exe instead of two..

  6. #6
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Winsock help:

    You could combine the server and client together, see it alot. Here is a file that has the client and server in one.
    Attached Files Attached Files

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    not what i want..i want to be able to whoever connects first to be server, and who ever is connected to, to be the client (or something similar) i dont want the option to be server or client.

  8. #8
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Winsock help:

    Well, that was more of an example. You would have to have a way, of notifying the other program who is did what first. Without a connection, I dont know how to really do that. I think I might know one way, let me go find the source.

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    i know it was an example, but i dont know how id do it so thats why im asking for help..

  10. #10
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Winsock help:

    What you might be able to do, is when the program loads, start listening for a connection. Then have a command button that connects via like a client. So it would work like this
    1.Program starts, listening for connections
    2. Who ever clicks on connect first, will start the connection
    3. Display a messagebox saying "Connected" so both users know they are connected, and wont try to connect.

    If you dont know how to listen, and request a connection, read Pinos tut.

  11. #11
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Winsock help:

    So it would look something like this:

    VB Code:
    1. Sub Form_Load()
    2. Winsock.LocalPort = 15151
    3. Winsock.Listen
    4. End Sub
    5.  
    6. Private Sub cmdConnect_Click()
    7. Winsock.RemoteHost = "127.0.0.1" 'Ip of the user
    8. Winsock.RemotePort = 15151
    9. Winsock.Connect
    10. End Sub
    11.  
    12. Private Sub winsock_connect()
    13. MsgBox "We are connected"
    14. End Sub

    I think that might do what you wanted. Not really sure though.

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Winsock help:

    Open the client, and start it running,
    Start the server in another instance of VB and start it running. Click the button.

    Go back to the client and click connect. See connected message.

    Now you are connected. Have both windows side by side.


    You could have the same game be client or server, just the first one that hits listen is the server. There has to be a way for someone to tell the other to start listening for traffic. If you put a server in the middle, it can listen to more than one client and pass traffic back and forth.

  13. #13
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Winsock help:

    He said he only wanted one program that does both. Not one client and one server.

  14. #14

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    well, i surprised myself here. Winsock is really damn easy!

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     With Winsock1
    5.         .LocalPort = "12423"
    6.         .RemoteHost = "127.0.0.1"
    7.         .RemotePort = "12313"
    8.         .Connect
    9.     End With
    10. End Sub
    11.  
    12. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    13. Winsock1.Close
    14. Winsock1.Accept requestID
    15. End Sub
    16.  
    17. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    18.     Dim dataStr As String
    19.         Winsock1.GetData dataStr
    20.         MsgBox dataStr
    21. End Sub
    22.  
    23. Private Sub Command1_Click()
    24.     Winsock1.SendData Text1.Text
    25. End Sub
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Winsock1.SendData Text1.Text
    5. End Sub
    6.  
    7. Private Sub Form_Load()
    8.     With Winsock1
    9.         .LocalPort = "12313"
    10.         .Listen
    11.     End With
    12. End Sub
    13.  
    14. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    15. Winsock1.Close
    16. Winsock1.Accept requestID
    17. End Sub
    18.  
    19. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    20.     Dim dataStr As String
    21.         Winsock1.GetData dataStr
    22.         MsgBox dataStr
    23. End Sub

    Now, i think i might be able to figure out how to do the 2 in one, we shall see!

  15. #15
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Winsock help:

    Very interesting. Like to see what you get as a result from adding them together.

    Yea its easy afterawhile. As dglienna knows, I spent like a week of hardwork, trying to figure out connecting -.^

  16. #16

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    yeah, why doesnt this work:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     With Winsock1
    5.         .LocalPort = "12423"
    6.         .Listen
    7.     End With
    8. End Sub
    9.  
    10. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    11. Winsock1.Close
    12. Winsock1.Accept requestID
    13. End Sub
    14.  
    15. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    16.     Dim dataStr As String
    17.         Winsock1.GetData dataStr
    18.         MsgBox dataStr
    19. End Sub
    20.  
    21. Private Sub Command1_Click()
    22. [B]Winsock1.Connect "127.0.0.1", "12313"[/B]
    23.     Winsock1.SendData Text1.Text
    24. End Sub

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Winsock help:

    You might be able to have winsock1 and winsock2 running in the same program, where winsock1 is always the client and winsock2 is always the server. Only one wil work at a time, and talk to the opposite side of the conversation.

    You need the Server to control who goes first, for instance. It doesn't have to actually go first, but the choice would be sent to it, that gets sent to either itself or the client. The "Your Move" would light up on the correct panel.

  18. #18
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Winsock help:

    Why are you attempting to connect again before you send your data? Its not required if you already have a running connection.

  19. #19

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    okay would this work if i wasnt using loopback?

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     With Winsock1
    5.         .LocalPort = "12313"
    6.         .RemoteHost = "127.0.0.1"
    7.         .RemotePort = "12423"
    8.         .Listen
    9.     End With
    10. End Sub
    11.  
    12. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    13. Winsock1.Close
    14. Winsock1.Accept requestID
    15. End Sub
    16.  
    17. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    18.     Dim dataStr As String
    19.         Winsock1.GetData dataStr
    20.         MsgBox dataStr
    21. End Sub
    22.  
    23. Private Sub Command1_Click()
    24.        With Winsock2
    25.         .LocalPort = "12313"
    26.         .RemoteHost = "127.0.0.1"
    27.         .RemotePort = "12423"
    28.         .Connect
    29.         End With
    30.     Winsock2.SendData Text1.Text
    31. End Sub

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     With Winsock1
    5.         .LocalPort = "12423"
    6.         .RemoteHost = "127.0.0.1"
    7.         .RemotePort = "12313"
    8.         .Listen
    9.     End With
    10. End Sub
    11.  
    12. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    13. Winsock1.Close
    14. Winsock1.Accept requestID
    15. End Sub
    16.  
    17. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    18.     Dim dataStr As String
    19.         Winsock1.GetData dataStr
    20.         MsgBox dataStr
    21. End Sub
    22.  
    23. Private Sub Command1_Click()
    24.     With Winsock2
    25.         .LocalPort = "12423"
    26.         .RemoteHost = "127.0.0.1"
    27.         .RemotePort = "12313"
    28.         .Connect
    29.     End With
    30.     Winsock2.SendData Text1.Text
    31. End Sub

  20. #20
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Winsock help:

    Well from what I see, that should work.

  21. #21

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    anyone else? i want to make sure i did it right, although i do think i did something wrong

  22. #22
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Winsock help:

    Best way to check it is just to run it locally. If it works, then you did it right, and it will work as long as it can pass the firewalls in the middle.

  23. #23

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    i cant run it locally, because they are both connecting to 127.0.0.1 : /

  24. #24
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Winsock help:

    I'm telling you I ran it on my laptop using 127.0.0.1 for the client and the server.
    I told you how to do that up near the top using two versions of the IDE or compile it and run two .exes

    Try PINO's version so you're convinced that it works.

  25. #25

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Winsock help:

    okay, what happened was they were connecting on the same ports (i guess ) but anyways why wont this work?? me and a friend are trying this:


    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     With Winsock1
    5.         .LocalPort = "12313"
    6.         .Listen
    7.     End With
    8. End Sub
    9.  
    10. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    11. Winsock1.Close
    12. Winsock1.Accept requestID
    13. End Sub
    14.  
    15. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    16.     Dim dataStr As String
    17.         Winsock1.GetData dataStr
    18.         MsgBox dataStr
    19. End Sub
    20.  
    21. Private Sub Command1_Click()
    22.        With Winsock2
    23.         .LocalPort = "12314"
    24.         .RemoteHost = ip here
    25.         .RemotePort = "12423"
    26.         .Connect
    27.         End With
    28.     Winsock2.SendData Text1.Text
    29. End Sub

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

    Re: Winsock help:

    I dont think your fully udnerstanding sockets,

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     With Winsock1
    5.         .LocalPort = "12313"
    6.         .Listen
    7.     End With
    8. End Sub
    9.  
    10. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    11. Winsock1.Close
    12. Winsock1.Accept requestID
    13. End Sub
    14.  
    15. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    16.     Dim dataStr As String
    17.         Winsock1.GetData dataStr
    18.         MsgBox dataStr
    19. End Sub
    20.  
    21. Private Sub Command1_Click()
    22.        With Winsock2
    23.       '  .LocalPort = "12314" * remove this *
    24.         .RemoteHost = ip here
    25.         .RemotePort = "12423"
    26.         .Connect
    27.         End With
    28.     Winsock2.SendData Text1.Text
    29. End Sub

    If that doesnt work i'll make a simple example.

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

    Re: Winsock help:

    Ok check out the example,

    It contains 3 parts,

    System Messages - For information regarding wincok errors etc,

    Incomming Data - Data recieved from another app

    Sent Data - data we have sent to the other app.

    To test this you will need to compile there is an exe with the source which has been compiled for you. Run it twice and click listen (first) on one and connect on another then send data between them, source is also included.

    Pino
    Attached Files Attached Files

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