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

