HOME       >>       General Discussion

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 :

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

Private Const chunk = 8192Dim fname As String  'get the name of the fileDim dataOpen fname For Binary As #1Do While Not EOF(1)data = Input(chunk, #1)winsock1.SenData dataDoEventsLoop
-------------

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

-------------
Private Sub send_Click()'GET FILE NAME'using getfilename function to get the file nameDim fnamea As StringDim fname As StringIf text1.Text = "" ThenMsgBox "Please type the file name!!!",Exit SubEnd Iffname = text1.Text  'checking wether the file existsIf Dir(fname) = "" ThenMsgBox "File Does not exist Exists"Exit Sub 'exiting sub it file does not existsEnd Iffnamea = GetFileName(text1.Text)fname = text2.TextDim temp As Stringtemp = "rqst" & fnamea'SENDwinsock1.SendData temp 'sending file name of fileEnd 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 winsock1Private Sub winsock1_dataarrival(ByVal bytestotal As Long)Dim response As Stringwinsock1.GetData response, vbStringSelect Case responseCase "okay"send fname 'send function is called with file name as argumentCase "deny"MsgBox "Your request to send the file " & fname & " has been denied",  'message when request is deniedEnd SelectEnd Sub' The send function which actally sends the filePrivate Sub send(fname As String)Command2.Enabled = FalseCommand3.Enabled = Falsetext1.Enabled = FalseDim data As StringDim a As LongDim data1 As StringDim data2 As StringOpen fname For Binary As #1Do While Not EOF(1)data = Input(chunk, #1)winsock1.SendData dataDoEventsLoopwinsock1.SendData "EnDf"Close #1Command2.Enabled = TrueCommand3.Enabled = Truetext1.Enabled = TrueEnd 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 = tempEnd 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 :

Private Sub winsock1_ConnectionRequest(ByVal requestID As Long)If winsock1.State <> sckConnected Thenwinsock1.Closewinsock1.Accept requestIDEnd IfEnd Suband:Private Sub winsock2_ConnectionRequest(ByVal requestID As Long)If winsock2.State <> sckConnected Thenwinsock2.Closewinsock2.Accept requestIDEnd IfEnd Sub

DATA ARRIVAL:

and when data arrives

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

Private Sub winsock1_DataArrival(ByVal bytestotal As Long)Dim data As StringDim data4 As StringDim data2 As StringDim data3 As StringDim data5 As StringDim data6 As StringWinsock1.GetData data, vbStringdata2 = Left(data, 4)Select Case data2Case "rqst"  'file request arrivesdata3 = Right(data, Len(data) - (4)) 'Get the file nameDim msg1 As Integer  'Stores user's selectionmsg1 = MsgBox(Winsock1.RemoteHost & " wants to send you file " & data3 & " accept ? ", vbYesNo)  'msgbox displayedIf msg1 = 6 Then  'if user selects yesWinsock1.SendData "okay"cd.FileName = data3data5 = Split(data3, ".")(1)data6 = "*." & data5cd.DefaultExt = "(data6)"data4 = App.Path & "\" & data3MsgBox data5cd.ShowSaveOpen data4 For Binary As #1Elsewinsock1.SendData "deny"Exit SubEnd IfCase "EnDf"Label1.Caption = "File revieved.Size of file : " & sz & " Kb"size = 0sz = 0Close #1Case Elsesize = size + 1Label1.Caption = size * 8 & "Kb Recieved"sz = size * 8Put #1, , dataEnd SelectEnd 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

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

Private Sub Command1_Click()Dim chat As Stringchat = text1.TextList1.AddItem (chat)winsock2.SendData chatEnd Sub' when data arrivesPrivate Sub winsock2_DataArrival(ByVal bytestotal As Long)Dim cht As StringWinsock2.GetData cht, vbStringList1.AddItem (cht)End Sub

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

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

OpaQue

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


JimiHex

hmmm....yes it isso?where is the problem?


Elegost

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

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!!!



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


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?



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.