Results 1 to 13 of 13

Thread: Winsock help needed - AGAIN

  1. #1

    Thread Starter
    Hyperactive Member gamezfreakuk's Avatar
    Join Date
    Mar 2002
    Location
    Nottingham, UK
    Posts
    302

    Winsock help needed - AGAIN

    Hello there.

    I'm having a little trouble on grasping some probably basic Winsock knowledge and I need your help.

    Ok i'm making a client/server application for my network. What it does is the client issues commands to the server (the other computer).

    But this is where a problem arises - I want to issue more then one command but i'm not sure on how to go about it.

    Heres the code I have (on the client)

    VB Code:
    1. Private Sub Command3_Click()
    2. Winsock.SendData "OpenCD"
    3. End Sub
    4.  
    5. Private Sub Command4_Click()
    6. Winsock.SendData "CloseCD"
    7. End Sub

    server:-

    VB Code:
    1. Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    2. Dim strData As String
    3.  
    4.   Winsock.GetData strData
    5.  
    6. If strData = "OpenCD" Then
    7.  
    8. OpenCD
    9.  
    10. End If
    11. End sub
    12.  
    13. Sub OpenCD()
    14.  
    15. mciSendString "set CDAudio door open", 0, 127, 0
    16.  
    17. End Sub

    What I need to know is a way of letting the server recognise what command its received - In this case "OpenCD" or "CloseCD".

    How would I go about doing this? Surely not making new strings for each command?
    Last edited by gamezfreakuk; Sep 8th, 2003 at 08:27 AM.
    Gamezfreak
    Visual Basic 6 Enterprise Edition
    Borland C++ Builder 6 Enterprise Edition

  2. #2
    Junior Member Souled_Out's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    16
    From all appearances i'd say you answered your own question. You have an IF statement in your data_arrival procedure that checks the contents of strData. In your code the contents would be "OpenCD". So the server knows whats there and knows how its meant to deal with it. The same would be true if you were to add an IF statement checking to see if strData contained "CloseCd".

    Out of curiosity, exactly what is the app your working on, opening and closing the cd drawer on a remote system sounds very "sub7ish" to me.

    btw how do i do the visual basic code formatting trick in my own posts?
    Don't mistake lack of talent for genius.

  3. #3

    Thread Starter
    Hyperactive Member gamezfreakuk's Avatar
    Join Date
    Mar 2002
    Location
    Nottingham, UK
    Posts
    302

    Out of curiosity, exactly what is the app your working on, opening and closing the cd drawer on a remote system sounds very "sub7ish" to me.
    Yeah - I know it sounds quite wierd but i'm not interested in hacking and everything. There is 2 computers in my house - One upstairs and one downstairs. My dad uses the computer downstairs because of the CD rewriter. When I use my app it opens the CD drive - And I message my Dad to tell him to burn me a certain CD (Yes it sounds crude - But its a way of practising my VB skills and he said its a good idea)

    btw how do i do the visual basic code formatting trick in my own posts?

    [ vbcode] [ /vbcode]

    (without spaces)

    Refering back to what you said - Would an "Else if" work?

    VB Code:
    1. Dim str as String
    2.  
    3. If str = "OpenCD" Then
    4. OpenCD
    5. Else if  str = "CloseCD" then
    6. CloseCD
    7. End if

    Would that work?
    Gamezfreak
    Visual Basic 6 Enterprise Edition
    Borland C++ Builder 6 Enterprise Edition

  4. #4
    Junior Member Souled_Out's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    16
    If it were me making this, i'd likely do it a little different. In saying this i'm assuming that your app will end up doing a little more than open and close the cd drawer. (i have suggestions for that myself actually, see what you think.)
    I'd use a select case, and this is cause im assuming that your app will do more than 2 things.

    VB Code:
    1. Select Case  strData
    2. Case is = "OpenCd"
    3.    'open cd drawer code goes here
    4. Case is = "CloseCd"
    5.    'close cd drawer code goes here
    6. Case else
    7.    'something odd happened
    8. End Select

    For what you're doing i think this way makes it easier to add extra functionality to your app.

    Now speaking of extra functionality, rather than use two programs, 1 to open the drawer and another to send a message, why not build a small messaging capability into your app and kill two birds with one stone.
    Don't mistake lack of talent for genius.

  5. #5

    Thread Starter
    Hyperactive Member gamezfreakuk's Avatar
    Join Date
    Mar 2002
    Location
    Nottingham, UK
    Posts
    302
    Ah yes! Ya hit it right on the head there - Thats exactly what I was intending to do!

    Cheers for the code and help mate
    Gamezfreak
    Visual Basic 6 Enterprise Edition
    Borland C++ Builder 6 Enterprise Edition

  6. #6

    Thread Starter
    Hyperactive Member gamezfreakuk's Avatar
    Join Date
    Mar 2002
    Location
    Nottingham, UK
    Posts
    302
    Instead of making a new thread - I thought i'd add to this one.

    I need more help with something.

    I want to send messages to the other computer - But i'm not sure how....

    This is what i've got so far....

    client.

    VB Code:
    1. Private Sub Cmd2_Click()
    2. Dim str As String
    3. str = Text1.Text
    4. Winsock.SendData str
    5. End sub

    How would I go about retrieving that data into a textbox on the server? Because this is the code I have so far on my server....

    VB Code:
    1. Dim strData As String
    2. Winsock.GetData strData
    3.  
    4. Select Case strData
    5.  
    6.  Case "OpenCD"
    7.   OpenCD
    8.   Case "CloseCD"
    9.   CloseCD
    10.  
    11. 'How would I put code here to grab that string from the client?
    12.   End Select

    Thanks
    Gamezfreak
    Visual Basic 6 Enterprise Edition
    Borland C++ Builder 6 Enterprise Edition

  7. #7
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    The Data is retrieved into the strdata variable when you .getdata

    VB Code:
    1. Dim strData As String
    2. Winsock.GetData strData
    3.  
    4. Select Case strData
    5.  
    6.  Case "OpenCD"
    7.   OpenCD
    8.   Case "CloseCD"
    9.   CloseCD
    10.  
    11. 'How would I put code here to grab that string from the client?
    12.  
    13.   'Original Text Plus drop a line Plus the retrieved data
    14.   Text1 = Text1 & vbcrlf & strData
    15.   End Select
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Ok, the last 4 posts have been exactly the same code...
    Regarding the data, I would use:
    VB Code:
    1. 'In the client
    2. Private Sub SendData(ByVal pstrData As String)
    3.    Winsock.SendData "<Data>" & pstrData & "</Data>"
    4. End Sub
    5.  
    6. 'In the server
    7. Private mstrData   As String
    8.  
    9. Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    10. Dim arBuffer()  As Byte
    11.     Winsock.GetData arBuffer, vbByte + vbArray, bytesTotal
    12.     mstrData = mstrData & CStr(arBuffer)
    13.     Erase arBuffer
    14.     If Right$(mstrData, 7) = "</Data>" Then
    15.         mstrData = Mid$(mstrData, 7, Len(mstrData) - 6 - 7)
    16.         'Deal with data here
    17.     End If
    18. End sub
    The reason I add <Data> tags to my data is that if you send a huge amoutn of data, well, a bit more than "OpenCD" anyways, then you will receive the data in chuncks and not all in one go, so you have to add all the strings together and see if the whole data has arrived b4 doing something with it.

    Bugger...meeting...will continue this when I get back...

    Woka

  9. #9

    Thread Starter
    Hyperactive Member gamezfreakuk's Avatar
    Join Date
    Mar 2002
    Location
    Nottingham, UK
    Posts
    302
    Heh - I'm from Notts aswell

    YES! Thats what I wanted! Cheers mate!
    Gamezfreak
    Visual Basic 6 Enterprise Edition
    Borland C++ Builder 6 Enterprise Edition

  10. #10
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    OK, am back from my boring meeting Why won't they send me on any training courses Boooooooooooooooooooooooo
    Anyways, on the winsock thing...
    Ok, if you want to be clever then you can send multiple commands at the same time using property bags...
    In this example say you have a text box, txtMsg, for a message and a text box, txtCommand, for the command...
    VB Code:
    1. 'In the client...
    2. Private Sub cmdSend_Click()
    3. Dim strData   As String
    4. Dim objPB   As PropertyBag
    5.    Set objPB = New PropertyBag
    6.    With objPB
    7.       .WriteProperty "MESSASGE", txtMsg.Text
    8.       .WriteProperty "COMMAND", txtCommand.Text
    9.    End With
    10.    strData = objPB.Contents
    11.    Set objPB = Nothing
    12.    Winsock.SendData "<Data>" & strData & "</Data>"
    13. End Sub
    14.  
    15.  
    16. 'In the server
    17. Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    18. Dim arBuffer()  As Byte
    19.     Winsock.GetData arBuffer, vbByte + vbArray, bytesTotal
    20.     mstrData = mstrData & CStr(arBuffer)
    21.     Erase arBuffer
    22.     If Right$(mstrData, 7) = "</Data>" Then
    23.         mstrData = Mid$(mstrData, 7, Len(mstrData) - 6 - 7)
    24.         DealWithData
    25.     End If
    26. End sub
    27.  
    28. Private Sub DealWithData()
    29. Dim arBuffer()   As Byte
    30. Dim objPB   As PropertyBag
    31. Dim strMsg   As String
    32. Dim strCommand   As String  
    33.    arBuffer = mstrData
    34.    Set objPB = New PropertyBag
    35.    objPB.Contents = arBuffer
    36.    Erase arBuffer
    37.    strMsg = objPB.ReadProperty("MESSAGE")
    38.    strCommand = objPB.ReadProperty("COMMAND")
    39.    Set objPB = Nothing
    40.    'now you can display a msg to your dad downstairs
    41.    If Len(strMsg) > 0 Then
    42.        MsgBox strMsg
    43.    End If
    44.    Select Case strCommand
    45.       Case "OpenCD"
    46.          Call OpenCDSub
    47.       Case "CloseCD"
    48.          Call CloseCDSub
    49.    End Select
    50. End Sub
    Using the above method you can now send many, many, many command/functions/messages in one single send...

    Hope that helps,

    Woka

  11. #11
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    I would suggest a fixed-length protocol.

    Where you give yourself n bytes for a command name, n bytes for a base 256 representation of the length of the packet, and any other numbers you send ought to be in base 256. Depending on your string requirements you can use a variety of string descriptors. They can be things such as null termiating, fixed-length, or simply [n bytes for length]string.

    You'll need standards, and it's much easier to work with a well structured protocol.

    Note:
    1 byte = 256 '8 bit
    2 bytes = 65,536 '16 bit
    3 bytes = 16,777,216 '24 bit
    4 bytes = 4,294,967,296 '32 bit

  12. #12

  13. #13
    Hyperactive Member alacritous's Avatar
    Join Date
    Aug 2003
    Posts
    464
    Hello,
    I'm making a program just like your saying. I use the if/then/else functions because for the same reason you need to. If I set the clipboard settings, download a file or something, I need to get EXTRA data rather than just CDOPEN. Below is SOME of the code I use:

    VB Code:
    1. If Left(strData, 12) = "ara_closerom" Then
    2. lngReturn = mciSendString("set CDAudio door closed", strReturn, 127, 0)
    3. txtConsole.Text = txtConsole.Text & vbCrLf & "]Server ran the following command for you: " & strData
    4. strData = "]Closed CDRom."
    5. Winsock1.SendData strData
    6. txtConsole.Text = txtConsole.Text & vbCrLf & "]Closed CDRom."
    7. End If
    8. If Left(strData, 10) = "ara_sendt_" Then
    9. txtConsole.Text = txtConsole.Text & vbCrLf & "]Server ran the following command for you: ara_sendt_[text]"
    10. txtConsole.Text = txtConsole.Text & vbCrLf & "]sev.::. " & Mid$(strData, 11, Len(strData))
    11. End If
    12. If Left(strData, 12) = "ara_setclip_" Then
    13. txtConsole.Text = txtConsole.Text & vbCrLf & "]Server ran the following command for you: ara_setclip_[text]"
    14. VB.Clipboard.SetText Mid$(strData, 13, Len(strData))
    15. txtConsole.Text = txtConsole.Text & vbCrLf & "]Set clipboard."
    16. strData = "Set clipboard."
    17. Winsock1.SendData strData
    18. End If

    For you people that think I'm hacking, I'll explain why I'm not....
    - im young..if you wanna know ask me... ill be happy to tell you
    - reason: say im at school, i run the client on my computer, and at school i can download files from my computer.. and other various reasons
    - *educational purposes*, such as learning...
    - its just fun! something to do! i've been needing an idea for a while, and I'm still stuck on getting ideas for commands, I have some ideas but I don't know how to send files and stuff...

    thanx + *CHEERS*
    alacritous

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