needs alot more than that .. you need to use the Winsock Functions ..
im working on a specific winsock Send/Receive File app, ill post the code when done ..
Meanwhile this is what im basing it off .. got this (or a version of it) off this forum somewhere... if its your code .. thanks .. cant remember who exactly posted it ..![]()
I stripped it down from a VNC type app i was messing with .. so in this case it waits for a connection from a remote client .. then once it accepts the connection it simply sends the file to the client (tmp.jpg in this instance).. very basic sample but this may give you an idea ..
Both projects use a Winsock1 and a Timer1 .. Load Send Program First .. Receive Program Connects on Form Load. Port is 1003 and default IP is 192.168.1.2 for both of them ... (testing on local PC!). Missing alot of other features which i have in my other winsock app/s but it will work as is, for an example. An image called tmp.jpg is required in the Send Project's path to test .. or change the file name in the code.
EDIT: i uploaded the 2 projects to make it easier..
Send Project:
VB Code:
Option Explicit Private iFileNum As Integer, lPacketSize As Long Private Sub Form_Load() On Error GoTo Err Winsock1.Close Winsock1.LocalPort = 1003 Winsock1.Listen Me.Caption = "Listening: Port 1003" Exit Sub Err: MsgBox "Socket Error!" & vbNewLine & _ Err.Description Unload Me Exit Sub End Sub Private Sub Form_Unload(Cancel As Integer) Winsock1.Close Timer1.Interval = 0 Timer1.Enabled = False End Sub Private Sub Winsock1_Close() If Winsock1.State = sckClosing Then Winsock1.Close End If End Sub Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) If Winsock1.State <> sckClosed Then Winsock1.Close End If Winsock1.Accept requestID SendFile Winsock1, App.Path & "\tmp.jpg" End Sub Public Sub SendFile(SocketObject As Winsock, ByVal FilePath As String, Optional ByVal PacketSize As Long = 1024) Dim Buffer() As Byte lPacketSize = PacketSize ' save the PacketSize for the timer Timer1.Enabled = False ' make suze timer is not enabled iFileNum = FreeFile ' get free file number Open FilePath For Binary Access Read As iFileNum ' open file ' if file size is smaller than PacketSize, then send the whole file, but not more ReDim Buffer(lngMIN(LOF(iFileNum), PacketSize) - 1) Get iFileNum, , Buffer ' read data SocketObject.SendData Buffer ' send data End Sub Public Function lngMIN(ByVal L1 As Long, ByVal L2 As Long) As Long If L1 < L2 Then lngMIN = L1 Else lngMIN = L2 End If End Function Private Sub Winsock1_SendComplete() Timer1.Enabled = False Timer1.Interval = 1 Timer1.Enabled = True End Sub Private Sub Timer1_Timer() Dim Buffer() As Byte, BuffSize As Long Timer1.Enabled = False If iFileNum <= 0 Then Exit Sub If Loc(iFileNum) >= LOF(iFileNum) Then ' FILE COMPLETE Close iFileNum ' close file iFileNum = 0 ' set file number to 0, timer will exit if another timer event BuffSize = 0 Winsock1.Close Winsock1.LocalPort = 1003 Winsock1.Listen Me.Caption = "Listening: Port 1003" Exit Sub End If 'if the remaining size in the file is smaller then PacketSize, the read only whatever is left BuffSize = lngMIN(LOF(iFileNum) - Loc(iFileNum), lPacketSize) ReDim Buffer(BuffSize - 1) ' resize buffer Get iFileNum, , Buffer ' read data Winsock1.SendData Buffer ' send data ' Show progress Me.Caption = "Sending: " & Format(Loc(iFileNum) / CDbl(LOF(iFileNum)) * 100#, "#0.00") & "% Done" ' timer event will be called again when last packet is sent, close the file then End Sub
Receive Project;
VB Code:
Option Explicit '// PROGRAM SETTINGS Const TCP_IP As String = "192.168.1.2" Const TCP_PORT As Long = 1003 Private ReceiveData As String Private FileBytes As Long '// CLOSE WINSOCK Private Sub Winsock1_Close() Dim strTempFile As String Dim intFile As Integer If Winsock1.State = sckClosing Then Winsock1.Close End If strTempFile = App.Path & "\tmp.jpg" intFile = FreeFile Open strTempFile For Binary As #intFile Put #intFile, , ReceiveData Close #intFile ReceiveData = "" Me.Caption = "Done" End Sub '// DATA ARRIVES Private Sub Winsock1_dataArrival(ByVal bytesTotal As Long) Dim strData As String Winsock1.GetData strData ReceiveData = ReceiveData & strData FileBytes = FileBytes + bytesTotal Me.Caption = "Downloading: " & FileBytes & " bytes" End Sub '// ERROR Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, _ ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) Winsock1.Close MsgBox "Error Connecting!" End Sub '// FORM LOAD Private Sub Form_Load() Timer1.Enabled = True Timer1.Interval = 100 With Winsock1 .RemoteHost = TCP_IP .RemotePort = TCP_PORT .Connect End With End Sub '// UNLOAD FORM Private Sub Form_Unload(Cancel As Integer) Winsock1.Close Timer1.Interval = 0 Timer1.Enabled = False End Sub




Reply With Quote