Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyone know how to send a file through winsock?

Status
Not open for further replies.

chiph

Programmer
Jun 9, 1999
9,878
0
0
US
This message being cross-posted from the WinSock mailing list:<br>
---------------------------------------------------<br>
SkunkBoy28@AOL.COM asked:<br>
Anyone know how to send a file through winsock?<br>
<br>
<br>
Which of the following methods do you plan to use?<br>
<br>
1) Use the winsock active-x control<br>
2) Set up all the declare statements for the wsock32.dll and do it all manually<br>
3) Use the wininet.dll which exports several ftp functions<br>
4) Use a winsock function called TransmitFile (only available on NT)<br>
<br>
<br>
I'm going to cross-post your question to and maybe we can follow up on it further there. The Winsock mailing list is more concerned with networking issues, rather than application-level issues like sending files.<br>
<br>
Chip H.<br>
<br>
 
Here is reply to SkunkBoy28:<br>
-------------------------------------<br>
<br>
OK. I haven't ever used the Active-X control, I've always done the native method calls. But here goes:<br>
(PS. I'm typing this into my email program, not cutting & pasting from VB, so expect some typos)<br>
<br>
1) Drop an instance of the Microsoft Winsock Active-X control on a form. Default name is Winsock1.<br>
<br>
2) Set the RemoteHostIP (or the RemoteHost if you know the host's DNS name) and RemoteHostPort to what the remote host is listening on<br>
<br>
3) In a CommandButton click event, call the Winsock1.Connect method. If connection is successful, you should see the Connect event fire.<br>
<br>
4) Open the file you want to send as binary:<br>
myfilenumber = FreeFile()<br>
Open myfile.abc For Binary Access Read Lock Read As #myfilenumber<br>
<br>
5) Loop through the file, sending 1,400 bytes at a time (typical network packet size is 1467 bytes):<br>
Public bGoodSend as boolean<br>
Dim szTemp as String<br>
Dim i as long<br>
Dim j as long<br>
Dim k as long<br>
<br>
i = 0<br>
j = LOF(myfilenumber)<br>
<br>
while (i &lt; j)<br>
k = iif (j - i &lt; 1500, j - i, 1500)<br>
szTemp = String(k, " ")<br>
Get myfilenumber,, szTemp<br>
bGoodSend = False<br>
Winsock1.SendData szTemp<br>
<br>
While (not bGoodSend)<br>
DoEvents<br>
Wend<br>
<br>
i = i + j<br>
Wend<br>
<br>
6) In the Winsock1.SendComplete event, set the bGoodSend variable to true<br>
<br>
7) Afterwards, close things down. <br>
Winsock1.close<br>
close(myfilenumber)<br>
<br>
<br>
This is a fairly cheesy implementation. If I were serious, I'd have what is known as a finite-state-machine, which essentially a big loop controlled by a variable that indicates what state I'm in. Note that the WinSock control has a state property which only tells you what state the socket is in, not the state of your application. So, I'd have a state for "Not Connected", one for "Connected", one for "Sending", one for "Waiting for SendComplete", etc. The state would change depending on the events and inputs you code.<br>
<br>
Also note that this simple example assumes there's something on the other side listening for your file. You would need to send some info along before your sends to tell the other side "Here comes file myfile.abc, which is 34,000 bytes, last changed on August 8th". And for each send, you would want to say "Here's more data". Plus, the other side would need to know when you are done sending so he can finish writing to disk, so send a "I'm Done" message.<br>
<br>
One more thing: Since you're sending the file as binary, remember that the byte order on Intel machines is different than on a lot of other computers. So the file would get there OK, but probably wouldn't be readable. A good idea is to run it through the htnl() function that's in the WSOCK32.DLL which translates double-words (a 'long' in VB) from host byte order to network byte order. The other machine would run it through the reverse nthl() function, which might or might not do anything, depending on it's CPU design. Of course, if you plan to stay in the Intel world on both ends, you can skip this step.<br>
<br>
So, not as easy as it looks, is it? You really might want to look at the ftp exports from the WinInet, it'll save you some time if the other machine has a ftp daemon listening.<br>
<br>
I'm going to cross-post this to if you already haven't.<br>
Chip H.<br>
<br>
-----Original Message-----<br>
From: SkunkBoy28@aol.com <br>
Sent: Monday, September 13, 1999 5:14 PM<br>
To: Chip Holland<br>
Subject: Re: if you use vb6 please read<br>
<br>
---------------------------------------------------------------------------<br>
i want to use Methid 1) Use the winsock active-x control<br>
<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top