Home > other >  About the problem of asynchronous SOCKET transport files
About the problem of asynchronous SOCKET transport files

Time:10-04

Consult a great god, the question is as follows:

Program is not very complex, using asynchronous SCOKET to send and receive information, no packaging, just send the data for a simple assembly

Apart after receiving data analysis, the system in the text instructions, used to push all no problem, including transmission of text, an XML file (big) problem,

 
Public Function __ access file buffer (ByVal strFileName As String, ByVal nPartLen As an Integer, ByVal nPartNum As an Integer, ByRef nReadLenth As an Integer, ByRef nIsEof As Integer) As String
Dim strFile As String=__str software catalog & amp; "\ Update " & amp; StrFileName

Dim mbyte (nPartLen - 1) As Byte
Dim strbuff As String

NIsEof=0
Dim nReadPosition As Integer=nPartNum division * * nPartLen 'file location for request division size

Dim the fs As FileStream=File. OpenRead (strFile)

Fs. The Position=nReadPosition
NReadLenth=fs. Read (mbyte, 0, nPartLen)
Strbuff=Encoding. The Default. Get string (mbyte)

If nReadLenth & lt; NPartLen Then nIsEof=1 'if read the length size is less than the division, is the end of the file
Fs. The Close ()
Return strbuff
End the Function


 
Sub send file buffer ()
'the file transfer. @ FileName @ Sectorlen @ part1
Dim strFileName=MSG (1)
Dim nIsEof As Integer=0
Dim nReadLenth As Integer=0
Dim strbuff As String=__ access file buffer (strFileName, MSG (2), MSG (3), nReadLenth, nIsEof)

Dim STRB As New StringBuilder
STRB. Append (" file transfer. ")
STRB. Append (__ data separator 1)
STRB. Append (" 2 ") 'the meaningless
STRB. Append (__ data separator 1)
STRB. Append (MSG) (3) 'segmentation index
STRB. Append (__ data separator 1)
STRB. Append (nReadLenth) the length of the 'read
STRB. Append (__ data separator 1)
STRB. Append (strbuff)
STRB. Append (__ data separator 1)
STRB. Append (nIsEof)
Call the sending server information ()
Return 1
End sub


 
'here is the server sends the message encapsulation process, the data is simple to assemble
Sub send information (ByVal CtlCmd As String, ByVal strParm As String, ByVal clientsocket As Socket)
Dim strMsg As New StringBuilder
'strMsg. Append (__ instruction to operator)
StrMsg. Append (CtlCmd)
StrMsg. Append (__ data separator 0)
StrMsg. Append (Format (DateTime. Now, "yyyyMMdd HHmmss"))
StrMsg. Append (" the parm: [")
StrMsg. Append (strParm)
StrMsg. Append (__ command terminator)

If clientsocket. Connected Then
Try
Clientsocket. Send (System. Text. Encoding. The Default. GetBytes (strMsg. ToString))
'SendDebugMsg strMsg. ToString ()
Catch the ex As Exception
RaiseEvent one rror (ex)
End the Try
The Else
Clientsocket. Close ()
End the If
End Sub

=============
SOCKET send and recive buffer is byte (1024), file transfer, a division of size was originally 512

Actually in the process of transmitting XML files, text files will appear puzzling gibberish, usually in the transmission at the end of Chinese two-character is cut off, after I modify "division size=400" force to solve problems, seems to be problems disappear, at least I design goal of the several files is normal,

I now to expand the function to transmit EXE files or other functional, not normal, back to the file size is normal, but already can't open,

Ask where is the problem?
  • Related