Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Anyone know how to send a file through winsock?

Anyone know how to send a file through winsock?

Anyone know how to send a file through winsock?

This message being cross-posted from the WinSock mailing list:
SkunkBoy28@AOL.COM asked:
Anyone know how to send a file through winsock?

Which of the following methods do you plan to use?

1) Use the winsock active-x control
2) Set up all the declare statements for the wsock32.dll and do it all manually
3) Use the wininet.dll which exports several ftp functions
4) Use a winsock function called TransmitFile (only available on NT)

I'm going to cross-post your question to http://www.tek-tips.com, 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.

Chip H.

Replies continue below

Recommended for you

RE: Anyone know how to send a file through winsock?

Here is reply to SkunkBoy28:

OK. I haven't ever used the Active-X control, I've always done the native method calls. But here goes:
(PS. I'm typing this into my email program, not cutting & pasting from VB, so expect some typos)

1) Drop an instance of the Microsoft Winsock Active-X control on a form. Default name is Winsock1.

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

3) In a CommandButton click event, call the Winsock1.Connect method. If connection is successful, you should see the Connect event fire.

4) Open the file you want to send as binary:
myfilenumber = FreeFile()
Open myfile.abc For Binary Access Read Lock Read As #myfilenumber

5) Loop through the file, sending 1,400 bytes at a time (typical network packet size is 1467 bytes):
Public bGoodSend as boolean
Dim szTemp as String
Dim i as long
Dim j as long
Dim k as long

i = 0
j = LOF(myfilenumber)

while (i < j)
k = iif (j - i < 1500, j - i, 1500)
szTemp = String(k, " ")
Get myfilenumber,, szTemp
bGoodSend = False
Winsock1.SendData szTemp

While (not bGoodSend)

i = i + j

6) In the Winsock1.SendComplete event, set the bGoodSend variable to true

7) Afterwards, close things down.

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.

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.

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.

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.

I'm going to cross-post this to http://www.tek-tips.com, if you already haven't.
Chip H.

-----Original Message-----
From: SkunkBoy28@aol.com
Sent: Monday, September 13, 1999 5:14 PM
To: Chip Holland
Subject: Re: if you use vb6 please read

i want to use Methid 1) Use the winsock active-x control

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close