|
-
Sep 6th, 2003, 07:58 AM
#1
Thread Starter
Hyperactive Member
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:
Private Sub Command3_Click()
Winsock.SendData "OpenCD"
End Sub
Private Sub Command4_Click()
Winsock.SendData "CloseCD"
End Sub
server:-
VB Code:
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock.GetData strData
If strData = "OpenCD" Then
OpenCD
End If
End sub
Sub OpenCD()
mciSendString "set CDAudio door open", 0, 127, 0
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
-
Sep 6th, 2003, 11:14 AM
#2
Junior Member
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.
-
Sep 6th, 2003, 03:21 PM
#3
Thread Starter
Hyperactive Member
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:
Dim str as String
If str = "OpenCD" Then
OpenCD
Else if str = "CloseCD" then
CloseCD
End if
Would that work?
Gamezfreak
Visual Basic 6 Enterprise Edition
Borland C++ Builder 6 Enterprise Edition
-
Sep 6th, 2003, 03:33 PM
#4
Junior Member
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:
Select Case strData
Case is = "OpenCd"
'open cd drawer code goes here
Case is = "CloseCd"
'close cd drawer code goes here
Case else
'something odd happened
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.
-
Sep 6th, 2003, 03:39 PM
#5
Thread Starter
Hyperactive Member
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
-
Sep 8th, 2003, 08:35 AM
#6
Thread Starter
Hyperactive Member
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:
Private Sub Cmd2_Click()
Dim str As String
str = Text1.Text
Winsock.SendData str
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:
Dim strData As String
Winsock.GetData strData
Select Case strData
Case "OpenCD"
OpenCD
Case "CloseCD"
CloseCD
'How would I put code here to grab that string from the client?
End Select
Thanks
Gamezfreak
Visual Basic 6 Enterprise Edition
Borland C++ Builder 6 Enterprise Edition
-
Sep 8th, 2003, 09:36 AM
#7
KING BODWAD XXI
The Data is retrieved into the strdata variable when you .getdata
VB Code:
Dim strData As String
Winsock.GetData strData
Select Case strData
Case "OpenCD"
OpenCD
Case "CloseCD"
CloseCD
'How would I put code here to grab that string from the client?
'Original Text Plus drop a line Plus the retrieved data
Text1 = Text1 & vbcrlf & strData
End Select
-
Sep 8th, 2003, 10:03 AM
#8
Ok, the last 4 posts have been exactly the same code...
Regarding the data, I would use:
VB Code:
'In the client
Private Sub SendData(ByVal pstrData As String)
Winsock.SendData "<Data>" & pstrData & "</Data>"
End Sub
'In the server
Private mstrData As String
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim arBuffer() As Byte
Winsock.GetData arBuffer, vbByte + vbArray, bytesTotal
mstrData = mstrData & CStr(arBuffer)
Erase arBuffer
If Right$(mstrData, 7) = "</Data>" Then
mstrData = Mid$(mstrData, 7, Len(mstrData) - 6 - 7)
'Deal with data here
End If
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
-
Sep 8th, 2003, 10:09 AM
#9
Thread Starter
Hyperactive Member
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
-
Sep 8th, 2003, 10:33 AM
#10
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:
'In the client...
Private Sub cmdSend_Click()
Dim strData As String
Dim objPB As PropertyBag
Set objPB = New PropertyBag
With objPB
.WriteProperty "MESSASGE", txtMsg.Text
.WriteProperty "COMMAND", txtCommand.Text
End With
strData = objPB.Contents
Set objPB = Nothing
Winsock.SendData "<Data>" & strData & "</Data>"
End Sub
'In the server
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim arBuffer() As Byte
Winsock.GetData arBuffer, vbByte + vbArray, bytesTotal
mstrData = mstrData & CStr(arBuffer)
Erase arBuffer
If Right$(mstrData, 7) = "</Data>" Then
mstrData = Mid$(mstrData, 7, Len(mstrData) - 6 - 7)
DealWithData
End If
End sub
Private Sub DealWithData()
Dim arBuffer() As Byte
Dim objPB As PropertyBag
Dim strMsg As String
Dim strCommand As String
arBuffer = mstrData
Set objPB = New PropertyBag
objPB.Contents = arBuffer
Erase arBuffer
strMsg = objPB.ReadProperty("MESSAGE")
strCommand = objPB.ReadProperty("COMMAND")
Set objPB = Nothing
'now you can display a msg to your dad downstairs
If Len(strMsg) > 0 Then
MsgBox strMsg
End If
Select Case strCommand
Case "OpenCD"
Call OpenCDSub
Case "CloseCD"
Call CloseCDSub
End Select
End Sub
Using the above method you can now send many, many, many command/functions/messages in one single send...
Hope that helps,
Woka
-
Sep 8th, 2003, 10:42 AM
#11
So Unbanned
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
-
Sep 8th, 2003, 10:44 AM
#12
...in more detail...?
Woka
-
Sep 8th, 2003, 08:51 PM
#13
Hyperactive Member
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:
If Left(strData, 12) = "ara_closerom" Then
lngReturn = mciSendString("set CDAudio door closed", strReturn, 127, 0)
txtConsole.Text = txtConsole.Text & vbCrLf & "]Server ran the following command for you: " & strData
strData = "]Closed CDRom."
Winsock1.SendData strData
txtConsole.Text = txtConsole.Text & vbCrLf & "]Closed CDRom."
End If
If Left(strData, 10) = "ara_sendt_" Then
txtConsole.Text = txtConsole.Text & vbCrLf & "]Server ran the following command for you: ara_sendt_[text]"
txtConsole.Text = txtConsole.Text & vbCrLf & "]sev.::. " & Mid$(strData, 11, Len(strData))
End If
If Left(strData, 12) = "ara_setclip_" Then
txtConsole.Text = txtConsole.Text & vbCrLf & "]Server ran the following command for you: ara_setclip_[text]"
VB.Clipboard.SetText Mid$(strData, 13, Len(strData))
txtConsole.Text = txtConsole.Text & vbCrLf & "]Set clipboard."
strData = "Set clipboard."
Winsock1.SendData strData
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|