![]() 3D 3D Photo Gallery (Part 1) 3D Photo Gallery (Part 2) Audio Poor Man's MIDI Make A Metronome iPod Tricks (Part 1) iPod Tricks (Part 2) iPod Tricks (Part 3) Laugh Track Machine Audio Player with Reverb Shepard Melody RB Phone Home Build a Drum Machine Custom Controls and Windows Double Click Listbox Draggable Metal Window Double Click Canvas Custom Buttons Custom Buttons Part II iTunes-style Listboxes Custom Controls General RB Scrolling Windows Using Mesage Dialogs Case-Sensitive Word Finder Introduction to Stacks Wiggle Window JPEG in PDF Listbox Checkboxes Background Applications Listbox Auto-Find Virtual Volumes Time Tracker Software Distribution (Part 1) Software Distribution (Part 2) Software Distribution (Part 3) Software Distribution (Part 4) Exceptions Tips and Tricks Text Clippings Made Easy Graphics Drawing a Simple Gradient The SpriteSurface: Space Game Image Spinner Cropping Graphics (Part 1) Cropping Graphics (Part 2) Cropping Graphics (Part 3) Cropping Graphics (Part 4) Shimmer Graphics Lissajous Figures Simple Screen Capture Vector Graphics Kaleidoscope Images Stegonography Spirals! Image Table RB Magnifying Lens Screen Capture Color Picker Tutorial Hacks Ghost Grab Speedy Mouse Extension iTunes Plugins iTunes Skinner Mac OS X Global Hot Key Event (Carbon Events) Login Welcomer (Carbon Events) Add/Remove Buttons Resizable Sheets Mac OS X Preferences Window Using Sheets in REALbasic Build a Bundle (Part 1) Build a Bundle (Part 2) Dock Your Passwords Mac OS X Debugging REALbasic Mac OS X Icon Tutorial Animate Your Dock RB and the Command Line Menus Window Menu Templates Menu Listbox Menu Novelty Guessing Game Calendar Trivia Tile Mixer Zip Code Finder Happy Valentine's Day Merlin Simulator (Part 1) Merlin Simulator (Part 2) Merlin Simulator (Part 3) Buzzword Machine AppleSoft BASIC Printing Print to PDF Registration Registration Code Validation Network Registration Codes Resources Picture Extractor (Part 1) Picture Extractor (Part 2) Serial Caller ID (Part 1) Caller ID (Part 2) Caller ID (Part 3) Speech Speech Recognition Socket Communication Easy Peer-to-Peer File Sharing MacPAD Version Checking Display Web Image In Canvas HTML IMG Tags Version Tracking Even Smarter Instant Messaging Web Tiler JavaScript and REALbasic Stock Ticker (Part I) Stock Ticker (Part 2) AIM Mate XML Manipulation Simple XML Introduction Video Big Brother Video Capture Note: All articles without a byline were written by Erick Tejkowski. When cleaning the site I removed them because the code differed from page to page, and I have yet to put them back in.
Tell us about a bad link. |
Peer-to-Peer File Sharing
Sockets EasyTCPSocket Design
The first step in the process to use the application will be for the user to specify their peer’s IP Address and click the Connect button. Only one user will have to do this since when one connects, the other will be connected automatically. Then one of the users will click on the Send… button and select a file to send. Once a file is selected, the file name and size are sent to the other user and they have the choice of accepting or denying the file. If denied, the sender is alerted with a dialog. If accepted, the sending application will then begin the file transfer. Once the file has been completely transferred, the receiving application will allow the user to click on the Launch button which will then open the transferred file. To disconnect, either user simply has to click on the Connect button which is now labeled Disconnect. The names of the buttons in the project from top to bottom are as follows: BtnConnect, BtnAccept, BtnDeny, BtnLaunch, and BtnSend. The name of the EditField for the IP Address is EFPeerIPAddress, the two ChasingArrows controls are ProgressConnect and ProgressSend, and the three StaticTextFields from top to bottom are StatReceivingFileName, StatReceivingProgress, and StatSendingFileName. The name of the socket is PeerSock, and it is an instance of the EasyTCPSocket class. To create an EasyTCPSocket, drag a TCPSocket to the window and change its superclass from TCPSocket to EasyTCPSocket using the properties palette.
Connecting and Disconnecting
In the Action event of the Connect button (which should be set to a Toggle BevelButton), we’ll need to test the value of the button and from there determine whether we’re supposed to be connecting or disconnecting. When the value is true we should connect and when it is false we should disconnect.
Before any connection can be made we’ll have to set the IP address to connect to, and that is done by setting PeerSocket.Address to the Text value of the EFPeerIP EditField. After that, we call the Connect method of the socket. All that is required for a socket to disconnect from another is a simple call to the Disconnect method. When a socket connection is successful the Connected event of that socket is fired. This is true of both the socket initiating the connection and the socket which is listening for a connection.
If a socket connection fails, then the Error event of that socket is fired and the appropriate error code is specified as the code parameter to the event. In addition to connection errors and unexpectedly dropped connections, the Error event is also triggered when sockets successfully disconnect from each other. The error code 102 in the error event is the error code for a successful disconnection. Knowing this, we test for 102 in the Error event and let the interface respond appropriately. When any other error code is encountered, we display a dialog box to inform the user. Error code values can be found under the SocketCore class in the Language Reference manual and online reference.
Selecting and Sending a Send Request
When the Send… button is clicked on, an OpenDialog box will pop up and allow the user to select a file. If a file is selected we’ll open it using a BinaryStream and read all of the contents of the file and put it into the SendingData property. Next, we assemble the contents of the message to be sent to the peer application which requests permission to transfer the selected file. The format of the message to be sent will be the file name, the size of the file in number of bytes (using LenB on the SendingData string), and the MacType and MacCreator of the file, all separated by EndOfLine characters. (The type and creator of the file aren’t used in the Accept/Deny process, but it’s easier to send them now instead of later.) When data is sent using the EasyTCPSocket command, not only is a message sent, but a command value is also sent. The integer values for the commands are purely whatever you make them. In this project I’ve decided on using three commands which have the integer values 1, 2, and 3. These are the kCmd constants we created earlier. Since we first need to request permission to send the file, we’ll use the kCmdSendRequest value as the command parameter when using PeerSocket.SendMessage.
Acceptance and Denial
When a kCmdSendRequest command is received, the Accept and Deny buttons must be enabled so that the user can accept or deny the file, and to know which file is being sent, the StatReceivingFileName field is used to display the name, size, type, and creator of the file. To accept or deny the file is a simple response using the kCmdSendRequestResponse command with a data value of “Accepted” or “Denied”.
If denied, we also clear the StatReceivingFileName field since we’re not going to be receiving it.
Sending and Receiving The File This case statement is an addition to the one existing in the ReceivedMessage event.
To represent the progress of sending the file we use the set the Visible property of the ProgressSend ChasingArrows to true. We set it to false in the SendComplete event since it is called whenever an outgoing message has been sent. Although this event will fire at times when we’re not actually sending the file (it will fire any time the SendMessage method is used), since it only sets the Visible property to false it will have no affect at any time other than when the file is being sent.
Upon receiving a kCmdSendData command we’ll reassemble the file using the properties previously received from the kCmdSendRequest message. Again using a BinaryStream, a file with the name ReceivingName will be created on the Desktop and the data from the message will be written to the file. The Launch button is then enabled and the StatReceivingProgress field shows that the file was successfully received.
Finished
As always, you can download the project for this tutorial. |
|||||
|
Please support ResExcellence by Visiting our Sponsors. One click makes a difference. |
||||||
|
|