|
|
|
|
![]() ![]() |
Nov 16 2005, 09:48 PM
Post
#1
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 21 Joined: 2-August 05 Member No.: 10,190 |
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... ................ |
|
|
|
Nov 17 2005, 12:07 AM
Post
#2
|
|
|
Administrator ![]() Group: Admin Posts: 1,480 Joined: 11-June 04 From: Somewhere in Time & Space. Member No.: 1 |
[THIS TUTORIAL IS FOUND TO BE COPIED! USER WARNED!]
|
|
|
|
Nov 17 2005, 10:24 PM
Post
#3
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 21 Joined: 2-August 05 Member No.: 10,190 |
hmmm....yes it is
so? where is the problem? |
|
|
|
Nov 17 2005, 11:35 PM
Post
#4
|
|
|
Member [Level 3] ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 90 Joined: 9-November 05 From: Canadian Wilderness Member No.: 14,034 |
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!!! |
|
|
|
Jun 17 2008, 06:57 AM
Post
#5
|
|
|
Trap Double Mocha Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 2,360 Joined: 21-September 07 Member No.: 50,369 |
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 |
|
|
|
Jul 4 2008, 04:31 PM
Post
#6
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 505 Joined: 14-November 05 From: Britannia! Member No.: 14,287 |
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?
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 7th October 2008 - 03:25 PM |