Jul 25, 2008

Transfer File Of Any Size Using Winsock Control - Winsock Help

Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

free web hosting

Transfer File Of Any Size Using Winsock Control - Winsock Help

JimiHex
This tutorial shows how to transfer file of any size using winsock control.

- Open VB;
- Select standard exe;
- Press Ctrl + t to show the add component window;
- Select winsock control and microsoft common dialog;
- Add one winsock control in the project;
- Name it winsock1;
- If you want to add chat then add another winsock and name it winsock2;
- Insert another winsock object if you want to add chat also;
- Add a microsoft common dialog box;
- Name it cd;
- We will use this winsock1 object to transfer the file and winsock2 for chat;

-------------

The basic idea :

To send a file of any size to any ip using winsock first we have to open the file in binary mode.
Then get chunks of data from it, chunk is a constant which is initialized to 8192, so we get 8192
bytes of data each time and send it using winsock to the client.
for example let "fname" be the string variable containg the file name then :

-------------
CODE

Private Const chunk = 8192

Dim fname As String  'get the name of the file
Dim data

Open fname For Binary As #1
Do While Not EOF(1)
data = Input(chunk, #1)
winsock1.SenData data
DoEvents
Loop

-------------

this will send 8192 bytes of data from the file until the file ends.


* But before sending data from file to client we must send info about the file
like..the name of the file...the extension...etc..

So when send is clicked first check wether a file is there i mean check wether something
is typed in the text box and if yes check wether the file exists.

If both the above conditions are met then get the filename with the extension.
send the file name to the client with "rqst" in front.
for eg. if the name of file is "text.txt" then send "rqsttext.txt" to the client.

The client will then get the file name and display a MsgBox with the name of the
file and the user will be given a choice wether to accept the file or not
if he\she selects yes then the client sends "okay" to the server and if he\she selects
no then it sends "deny" to the server..this data i.e. "okay" or '"deny" arrivers on winsock1's
local port the data is then checked using select case if its okay then "send" function
is called with file address as an argument and send button and all buttons and text boxes
associated with send file are disabled.
If the response from client is "deny" then a msgbox is shown on server saying that the
request to send the file .... as been denied..the user can send another request..or
ask the client's user to accept the file using the chat module...



- This is called when send is clicked

-------------
CODE

Private Sub send_Click()

'GET FILE NAME
'using getfilename function to get the file name
Dim fnamea As String
Dim fname As String

If text1.Text = "" Then
MsgBox "Please type the file name!!!",
Exit Sub
End If
fname = text1.Text  'checking wether the file exists
If Dir(fname) = "" Then
MsgBox "File Does not exist Exists"
Exit Sub 'exiting sub it file does not exists
End If

fnamea = GetFileName(text1.Text)
fname = text2.Text
Dim temp As String
temp = "rqst" & fnamea

'SEND

winsock1.SendData temp 'sending file name of file

End Sub

'now the request is sent to the client
'then the server has to wait for the client's response

' this event is called when data arrives on winsock1

Private Sub winsock1_dataarrival(ByVal bytestotal As Long)
Dim response As String
winsock1.GetData response, vbString
Select Case response
Case "okay"
send fname 'send function is called with file name as argument
Case "deny"
MsgBox "Your request to send the file " & fname & " has been denied",  'message when request is denied
End Select
End Sub

' The send function which actally sends the file


Private Sub send(fname As String)
Command2.Enabled = False
Command3.Enabled = False
text1.Enabled = False

Dim data As String
Dim a As Long
Dim data1 As String
Dim data2 As String


Open fname For Binary As #1

Do While Not EOF(1)
data = Input(chunk, #1)
winsock1.SendData data
DoEvents
Loop

winsock1.SendData "EnDf"
Close #1
Command2.Enabled = True
Command3.Enabled = True
text1.Enabled = True

End Sub


'Other supporting functions:

Function GetFileName(attach_str As String) As String
   Dim s As Integer
   Dim temp As String
   s = InStr(1, attach_str, "\")
   temp = attach_str
   Do While s > 0
       temp = Mid(temp, s + 1, Len(temp))
       s = InStr(1, temp, "\")
   Loop
   GetFileName = temp
End Function


-------------

On the client side :

set winsock1 to listen to a particular port say : 165
and winsock2 if you want chat too :166


winsock1 is listening to port 165 and winsock2 is listening to port 166
on the client side


so when connection request arrives :

CODE


Private Sub winsock1_ConnectionRequest(ByVal requestID As Long)
If winsock1.State <> sckConnected Then
winsock1.Close
winsock1.Accept requestID
End If
End Sub

and:

Private Sub winsock2_ConnectionRequest(ByVal requestID As Long)
If winsock2.State <> sckConnected Then
winsock2.Close
winsock2.Accept requestID
End If
End Sub



DATA ARRIVAL:

and when data arrives

-------------

CODE


Private Sub winsock1_DataArrival(ByVal bytestotal As Long)

Dim data As String
Dim data4 As String
Dim data2 As String
Dim data3 As String
Dim data5 As String
Dim data6 As String

Winsock1.GetData data, vbString

data2 = Left(data, 4)
Select Case data2
Case "rqst"  'file request arrives

data3 = Right(data, Len(data) - (4)) 'Get the file name

Dim msg1 As Integer  'Stores user's selection
msg1 = MsgBox(Winsock1.RemoteHost & " wants to send you file " & data3 & " accept ? ", vbYesNo)  'msgbox displayed


If msg1 = 6 Then  'if user selects yes
Winsock1.SendData "okay"
cd.FileName = data3
data5 = Split(data3, ".")(1)
data6 = "*." & data5
cd.DefaultExt = "(data6)"
data4 = App.Path & "\" & data3
MsgBox data5
cd.ShowSave

Open data4 For Binary As #1

Else
winsock1.SendData "deny"
Exit Sub
End If

Case "EnDf"
Label1.Caption = "File revieved.Size of file : " & sz & " Kb"
size = 0
sz = 0
Close #1
Case Else

size = size + 1
Label1.Caption = size * 8 & "Kb Recieved"
sz = size * 8
Put #1, , data
End Select
End Sub


-------------

This will take care of file transfer now for the chat:
we will be using winsock2 for chat:

On server side :

WHEN SEND IS CLICKED

-------------

CODE


Private Sub Command1_Click()
Dim chat As String
chat = text1.Text
List1.AddItem (chat)
winsock2.SendData chat

End Sub

' when data arrives

Private Sub winsock2_DataArrival(ByVal bytestotal As Long)
Dim cht As String
Winsock2.GetData cht, vbString
List1.AddItem (cht)

End Sub



-------------
the same will be on the client side also...

................

 

 

 


Reply

OpaQue
[THIS TUTORIAL IS FOUND TO BE COPIED! USER WARNED!]

Reply

JimiHex
hmmm....yes it is
so?
where is the problem?

Reply

Elegost
Copying tutorials is not allowed. You should have read the Rules Before you posted...

QUOTE
This forum is a forum to post tutorials that YOU have written. You are NOT allowed to post a tutorial copied from another site, regardless of any reference you make! (However, you may PARAPHRASE it with correct referencing). Your tutorial is going to be moderated (that means, anything you post won't be viewable until a moderator has accepted it). Do not re-post your tutorials if they don't show up! Any pictures you include must be thumbnails or links to the pictures. In other words, YOU CAN ONLY POST IN THIS FORUM IF YOU HAVE AN UNCOPIED TUTORIAL TO SHARE WITH US!!!



Reply

iGuest
To Send an image of around 600kb using udp in vb6
Transfer File Of Any Size Using Winsock Control

I want to send an image of 600kb and above using UDP protocol in vb6.I am reading it as binary data and tried to send it.But it is showing error that datagram is too large.Please help to get the solution

-question by Sunil

Reply

gameratheart
Sorry to say, but this tutorial is very vague to me. I'm a Visual Basic user and I thought I'd try out this tutorial, just for kicks, but I cannot understand exactly what I'm supposed to be doing. Can someone clarify the steps or link to a tutorial that explains this better?

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. vb winsock send big files - 12.18 hr back. (1)
  2. script with winsock control - 12.73 hr back. (1)
  3. winsock control - 13.75 hr back. (1)
  4. winsock help - 14.28 hr back. (1)
  5. winsock1.getdata - 15.21 hr back. (1)
  6. winsock getdata jscript - 17.01 hr back. (1)
  7. vb6 transfer file apache - 17.15 hr back. (1)
  8. visual basic 6 file transfer winsock - 17.23 hr back. (1)
  9. send file file udp vb6 - 20.07 hr back. (1)
  10. winsock getdata length - 20.31 hr back. (1)
  11. send large files with winsock - 23.00 hr back. (1)
  12. data transfer in winsock - 23.02 hr back. (1)
  13. winsock data size can be transfer - 25.88 hr back. (1)
  14. winsock file send vb - 31.98 hr back. (3)
Similar Topics

Keywords : transfer, file, size, winsock, control, winsock

  1. Debug Exe Files
    How to debug an exe file. (4)
  2. Make A Moderately-secure Password System Using Javascript
    using file redirection to hide the password. (4)
    JavaScript is very handy at making forms, allowing for much more customization and easier ways to
    send data. So making Login forms using JavaScript may seem to many to be a very feasable idea.
    However, JavaScript is very bad at protecting Passwords, as since the passwords are not encypted and
    the whole JavaScript code is in the page, a person could just view the Page Source and find out
    everything. Even if you use an external JavaScript, it would still be poor as the file name for the
    external JavaScript would still be revealed. But I have an answer! There is a rela....
  3. Install An Aef Forum Onto The Trap17
    From a zip file (11)
    Installing an AEF Forum on the Trap17 Server Preparation for Installing the AEF Forum
    The following items are required for the installation of the packaage onto your site: 1. - a copy
    of the AEF Forum zip package from http://anelectron.com/download.php 2. - a MySql Database 3. - a
    Database User 4. - a password for the Database User 5. - Privileges allowed for the Database User
    The details for ensuring that you have all of these items are as follows: 1. - a copy of the AEF
    Forum zip package from http://anelectron.com/download.php . Simply browse to t....
  4. How To Make A Simple File Based Shoutbox Using Php And Html
    (8)
    A simple tut to make a simple shoutbox. Let me jump right in. First of all you need the standard
    equipment for PHP, an IDE like XAMPP and an editor like PHP EDITOR 2OO7. Were going to make a
    simple guestbook using three files, webpage.php, shout.php and shout.txt. Webpage.php can be
    changed to whatver you want, it will be the page on which the guestbok is shown, you could even use
    this code and add it to another php page n your site. Shout.php is the proccessing page and
    shout.txt is where the shouts are stored. Firstly we need to make the visual design of the box.....
  5. Flat-file Cms
    tutorial inspired by jlhaslip (4)
    Ok, for this tutorial i am only going to show you how to add updates to your site simply by storing
    the information into a text file, and then displaying it with predefined formatting... OK lets get
    down to business... Lets start out by making a PHP file and call it mycms.php put this code at
    the top of the page. What this will do is allow us to edit the selected update when it comes time
    and show and hide the add an update field and validate the form.. <script
    language="javascript"> function ShowHide(id1, id2) { if (id1 != '') expMenu(id1)....
  6. *nix File Permissions - An Overview
    (6)
    I was originally going to post this in a reply, but felt it would deviate from the topic.
    Here's a brief overview of the three numbers in a permission "code": -The first number is for
    the owner of the file. If you set a file at 600, the owner will have read and write access and
    everyone else is locked out. -The second number is for the users group (users are placed into groups
    to get special rights sometimes). Generally you will not give write access to a user's group.
    -The third number is for the rest of the world, including web users. Setting any value that wi....
  7. Starting Or Stopping Apache And Mysql Server Via Batch File
    (0)
    Hi guys, this is a litte tutorial about how we start and stop the Apache and MySQL in Windows NT
    (2000, XP, 2003) via a batch file script. As we know in Windows NT based system Apache and MySQL
    installed as Windows Services. So we can stop and start it using NET command. For more information
    about DOS command, type HELP at command prompt. I assuming that your MySQL service name is "mysql"
    and your Apache (Apache 2.0.x) service name is "apache2". If you want to chek it click Start > Run >
    services.msc > OK. Windows IS NOT Case Sensitive. Let's get started!. 1. ....
  8. How To Fix Codecs And Movie File Problems
    ...a short but very useful tutorial (0)
    How to Fix Codecs and Movie File Problems For all of you who are having trouble with
    codecs, either not being able to play a movie you've downloaded or not having any
    sound/picture, this guide should help you. The Easy Quick-Fix Method For the lazy people
    amoung you who want the simplest possible solution, may I present Video LAN Player (VLC) ,
    it's a good video/audio player and comes with all the codecs you will need and has many
    features making it my player of choice. The (New) Other Method The Combined Community Codec
    Pack (C....
  9. The Many Ways To Bypas File Hosting Annoyances
    (5)
    I've done a lot of research on this subject because it is much more common for people to upload
    files using file hosting services such as megaupload and rapidshare. They continue to try to push on
    their premium accounts on to the daily users who don't really want to put up any money for
    downloading which should be free.So i've compiled a few techniques I've used to bypass the
    limits of free downloading accounts and leave you feeling just as content with yourself as if you
    had a premium account. Download Managers: Now with the prominent use of file hosts ....
  10. Unencrypted But Invisible File Storage
    It can have a password, it can be unlocked. (0)
    This method works, but unfortunately compression software can open the file without the password.
    Also, you can try creating a new user account. When it asks "Make files and folders private?" click
    Yes, make private. Name the new user account anything using NO spaces/uppercase letters, but not
    something like "privatefiles" or something like that. Try accessing the files in C:\Documents
    and Settings\ >. You cannot open the folder. Now to ensure that user is always hidden in My
    Computer, click Start > Run. 1. Type in command.com . This should bring up a blac....
  11. Using A Secure File Transfer Client
    A discussion of FTP, FTPS, SCP, and SFTP (0)
    Using a Secure File Transfer Client Almost everyone who creates a web site is faced with
    the problem of getting their files from their local computer to their web server. There are a few
    different protocols (methods) through which to accomplish this, and some have definite advantages
    over others. Here are the major ones, listed loosely in order of increasing security. Note: All
    of the programs recommended in this tutorial are for Windows only. Command line alternatives are
    accessible via the terminal for both Linux and OS X. FTP First a note on using ....
  12. How To: Make A Simple Php Site
    Making one file show up on all pages using php (21)
    I have looked all over the site and could not find anything that was like this simple, or just like
    this at all.. For some people i know that you are using a basic HTML site...and having a big menu
    if you want to add somthing you have to go into every one of the pages and add or remove or edit
    what you want to do, but with somthing verry simple all you would have to do is edit one file, and
    all of the pages that have the PHP script on them would suddenly change to what that one file is.
    So to start off if you are planning on using this little tirck, the page that you a....
  13. Networking Tutorial: File And Printer Sharing
    A basic computer networking tutorial (0)
    Network Setup: Newbie Install Click start, then right click computer Ensure that the full
    computer name is memorable and that each machine is on the same workgroup. After you've done
    this, you are ready to configure advanced features, such as file and printer sharing. Adding a
    Printer Locally Firstly, you need to configure your printer (on the machine which is
    directly connected to the printer) for printer sharing. Click start, control panel,
    printers/scanners, then right click on your local printer and click 'properties'. Browse to
    the shari....
  14. Phpbb Forum Site Transfer
    How to do it, step by step instructions (20)
    I'm sure many of you out there have used phpBB at some point. To those who enjoy running forums
    and online communities, specifically supporting phpBB, I am about to tell everyone how to restore
    the forums database from one website, to another. This is presuming you do not have any mods or
    hacks installed. Some of you may find this information useful. Here is the scenario: Let's say
    you have forums running phpBB version 2.0.17 (currently the latest one). You have decided that you
    want to move your forums to a whole new URL and provider, and as an added bonus, ch....
  15. How To: Change Your Website's Index File
    a simple trick using .htaccess (18)
    How To: Change Your Website's Index File a simple trick using the .htaccess file A simple
    tutorial which only involves editing one little file. Useful for those of us who have mime-typed
    extensions or who are creating lots of test design files and want an easy way to make the design
    they like best their default file. Create a file called .htaccess in the /public_html/ folder if
    you don't have it. I think one should be there already when you get your site so if it isn't
    you should create it anyway! In the file write the following: CODE Di....
  16. Shut Down, Restart, Log Off Xp Using A Batch File
    Undocumented feature for XP (0)
    This is a copy of a tutorial I created for astahost.com. I'm copying it here as a courtesy to
    trap17 members. /smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> QUOTE
    How to shutdown a XP Pro computer when you are connected using RDC (Remote Desktop Connection)
    using a batch file or RPC (Remote Procedure Command) This is useful if you can't get to the
    computer but have remote access to the computer via a network or the internet. First To show you
    what you will see at a command prompt (command.com) when you type: "shutdown /?" Wit....

    1. Looking for transfer, file, size, winsock, control, winsock

Searching Video's for transfer, file, size, winsock, control, winsock
advertisement



Transfer File Of Any Size Using Winsock Control - Winsock Help



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE