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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How transfer binary files from winsock 1

Status
Not open for further replies.

kctoo

Programmer
May 24, 2003
13
HK
HELP Me

Configuration:
OS Chinese windowsXP
English Visual Foxpro v6.0

I try to use fread(),ASC(),CHR() function. I still didn't work it.

Below code from Microsoft:
* Transmitter Code.

* INIT event of Comm OLE control
* 28800 baud, no parity, 8 data, and 1 stop bit.
* In RS-232, maximum speed of 28800 baud can be used

This.Settings = "28800,N,8,1"
This.InputLen = 1
This.CommPort = 1
This.PortOpen = .T.

* ONCOMM event of Comm OLE Control
* The following code is needed to make sure that next set of characters
* can be transmitted (CommEvent = 2 is triggered from the receiver side)

IF This.CommEvent = 2
This.input
IF gnTop <= gnEnd
gcString = FREAD(gnFileHandle, 1) && Store to memory
q=asc(gcstring)
* change ASCII to character (size of 3) to preserve the lead char
thisform.olecontrol1.output = str(q,3)
gnTop = gnTop + 1
ENDIF
ENDIF

* INIT event of form

PUBLIC gnFileHandle
PUBLIC gnEnd
PUBLIC gnTop
PUBLIC q

* You should replace 'c:\sample.hlp' with your own binary file
STORE FOPEN('c:\sample.hlp') TO gnFileHandle && Open the file
STORE FSEEK(gnFileHandle, 0, 2) TO gnEnd && Move pointer to EOF
STORE FSEEK(gnFileHandle, 0) TO gnTop && Move pointer to BOF
gntop=1
q=&quot;&quot;

-----------------------

Property of OleControl1

RThreshold = 1 * triggers when at least one char is on the buffer
SThreshold = 3

-----------------------

* Receiver Code.

* INIT event of OleControl1
* 28800 baud, no parity, 8 data, and 1 stop bit.

This.Settings = &quot;28800,N,8,1&quot;
This.InputLen = 3
This.CommPort = 1
This.PortOpen = .T.

* OnComm event

IF This.CommEvent = 2 AND This.InBufferCount > 0
qq=CHR(VAL(This.Input))
=FWRITE(gnFileHandle,qq)
this.output = CHR(26)
ENDIF

* INIT event of form

PUBLIC gnFileHandle

* You should replace 'c:\sample.hlp' with your own file name
IF FILE('c:\sample.hlp') && Does file exist?
gnErrFile = FOPEN('c:\sample.hlp',12) && If so, open read-write
ELSE
gnErrFile = FCREATE('c:\sample.hlp') && If not, create it
ENDIF

= FCLOSE(gnErrFile) && Close the file

STORE FOPEN('c:\sample.hlp',1) TO gnFileHandle && Open the file

* CLICK event of button

*This tells the transmitting side to start sending the file
Thisform.Olecontrol1.output = CHR(26)

-----------------------

Property of OleControl1

RThreshold = 3

 
What activex are you using? If I look at my Winsock activex, most if the setting you are using I don't see:

This.Settings = &quot;28800,N,8,1&quot;
This.InputLen = 3
This.CommPort = 1
This.PortOpen = .T.




Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike Gagnon

Have you any sample code for using winsock about binary files transfer ?

kctoo
 
kctoo


Code:
#DEFINE crlf CHR(13)+CHR(10)

oWinsock = CREATEOBJECT(&quot;MSWinsock.Winsock.1&quot;)
oWinsock.Protocol = 0     && 0=TCP Protocol, 1=UDP
oWinsock.RemoteHost = &quot;mail.hyperlinx.net&quot;
oWinsock.RemotePort = 25 && smtp port.
oWinsock.Connect
=Inkey(.25)
OWinsock.senddata(&quot;Helo myself.com&quot; + crlf) && You can change this string to anything else..
line1 = &quot;mail from:&quot; + Chr(32) + &quot;mike.gagnon@slpcanada.com&quot; + CrLf
line2 = &quot;rcpt to:&quot; + Chr(32) + &quot;mike.gagnon@slpcanada.com&quot;+ CrLf
line3 = &quot;Date:&quot; + Chr(32) + DTOC(DATE()) + CrLf
line4 = &quot;From:&quot; + Chr(32) + &quot;m.gagnon@nowhere.com&quot; + CrLf
line5 = &quot;To:&quot; + Chr(32) + &quot;m.gagnon@nowhere.com&quot; + CrLf
line6 = &quot;Subject:&quot; + Chr(32) + &quot;Test winsock mail&quot; + CrLf
line7 = &quot;This is a test body of the message&quot; + CrLf
line9 = &quot;X-Mailer: Kannan's Test mail&quot; + CrLf
line8 = line4 + line3 + line9 + line5 + line6 + line7
oWinSock.senddata(line1)
=Inkey(.25)
oWinsock.senddata(line2)
=Inkey(.25)
oWinsock.senddata(line8 + &quot;.&quot; + crlf)
=Inkey(.25)
oWinsock.senddata(&quot;quit&quot;)
=Inkey(.25)
oWinsock.close()

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
hi mgagnon ,

it send the text is ok. Now i don't know
how to send attachment files or binary files in winsock ?
Finally, It occur incorrect files size after the files transimission.

** Server side ***
Dataarrival event
LPARAMETERS bytestotal
thisform.tcpserver.Object.GetData(@strData)
mstring=mstring+strdata

** Save Attachmetn Command Buttion
gnErrFile = FCREATE('C:\HANDBOOK\ARCHB01.GIF')
=FWRITE(gnErrFile,mString)
=FCLOSE(gnErrFile)

kctoo
 
kctoo

I think from what I read, Winsock is more a low-level transporter, you may consider a higher level one that permits file attachements, like FTP as Dave S. suggested, or HTTP, Rick Strahl has a good article on file transfers (
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
If you're going to use the Winsock control for a binary file, you may need to use the option type parameter in the GetData() call:

object.GetData data, [type,] [maxLen]

Since the documentation is so lacking, I don't know what the different values are for type, but if you can figure out what vbByte or vbInteger translates to, you may want to try them.


Dave S.
[cheers]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top