Socket writing data to a file

vish707

Member
Joined
Apr 16, 2007
Messages
12
Programming Experience
Beginner
Dear everyone,

I have an asynchrnous socket server accepting multiple connections from remote sites. I have a function called "Writedatafile" which reads the packet, and writes the data from each packet to a separate binary file.

- In the code section of :
VB.NET:
PrivateSub readCallback(ByVal ar As IAsyncResult)
.
.
WriteDataFile(state) ' I am calling my function here to write data from a socket.
 
handler.BeginReceive(state.Rbuf, state.Index, state.BufferSize, 0, _
AddressOf readCallback, state)
.
.
 
-The writedatafile function is as follows:
 
'------------------------------------------------------------------------------
'Name: WriteDataFile
'Purpose: Write the data to a file and display them in the form
'Inputs: StateObject 
'Returns: 0 success; -1 error
'------------------------------------------------------------------------------
PrivateFunction WriteDataFile(ByVal state As StateObject)
'Pos is the index of begintag1
'P1 is the index of data that has not been written to the file
'i is the index of which is to be checked now
Dim i, Pos, P1 AsInteger
Dim Checksum, TypeID, SiteNum AsInteger
Dim StartDate, StartTime, EndDate, EndTime, FileName AsString
P1 = 0 'The beginning pointer of the data that hasn't been written to file
Try
For i = state.Index To state.Length
'If the (i-1)<=P1 then there are 2 or less bytes to be read.
'This is too little, so cannot go further.
If state.Rbuf.GetValue(i) = END2 And (i - 1) > P1 Then'To locate the end tag 2
'(i-3) points to checksum 1 and (i-2) points to checksum 2
'GetValue(i-1) gets the first byte of end tag
'(i-3)>P1 is there to check if there are enough bytes to read
If state.Rbuf.GetValue(i - 1) = END1 And (i - 3) > P1 Then'To locate the end tag 1
Checksum = state.Rbuf.GetValue(i - 3) * 256 + state.Rbuf.GetValue(i - 2) 'Get the Checksum
Pos = i - Checksum + 1 'Get the beginning tag index
If Pos >= P1 Then'Has a whole packet
'Pos+1 is the index of begin tag 2
If state.Rbuf.GetValue(Pos) = BEGIN1 And state.Rbuf.GetValue(Pos + 1) = BEGIN2 Then'check begin tag
'Read the data from packet
'A byte is only 0-255
'A Value of 4 bytes is represented as shown
TypeID = state.Rbuf.GetValue(Pos + 4) * 256 ^ 3 _
+ state.Rbuf.GetValue(Pos + 5) * 256 ^ 2 _
+ state.Rbuf.GetValue(Pos + 6) * 256 ^ 1 _
+ state.Rbuf.GetValue(Pos + 7)
SiteNum = state.Rbuf.GetValue(Pos + 8) * 256 ^ 3 _
+ state.Rbuf.GetValue(Pos + 9) * 256 ^ 2 _
+ state.Rbuf.GetValue(Pos + 10) * 256 ^ 1 _
+ state.Rbuf.GetValue(Pos + 11)
'Padding gives the required format of date / time representation
'I.e. 100307143010(DMYHMS)
StartDate = CType(state.Rbuf.GetValue(Pos + 24), String).PadLeft(2, "0") _
& CType(state.Rbuf.GetValue(Pos + 25), String).PadLeft(2, "0") _
& CType(state.Rbuf.GetValue(Pos + 26), String).PadLeft(2, "0")
StartTime = CType(state.Rbuf.GetValue(Pos + 27), String).PadLeft(2, "0") _
& CType(state.Rbuf.GetValue(Pos + 28), String).PadLeft(2, "0") _
& CType(state.Rbuf.GetValue(Pos + 29), String).PadLeft(2, "0")
EndDate = CType(state.Rbuf.GetValue(Pos + 30), String).PadLeft(2, "0") _
& CType(state.Rbuf.GetValue(Pos + 31), String).PadLeft(2, "0") _
& CType(state.Rbuf.GetValue(Pos + 32), String).PadLeft(2, "0")
EndTime = CType(state.Rbuf.GetValue(Pos + 33), String).PadLeft(2, "0") _
& CType(state.Rbuf.GetValue(Pos + 34), String).PadLeft(2, "0") _
& CType(state.Rbuf.GetValue(Pos + 35), String).PadLeft(2, "0")
'Create a File Name
SelectCase TypeID
Case 1
FileName = StartDate & StartTime & EndDate & EndTime & ".inf"
Case 2
FileName = "2config" & EndDate & EndTime & ".inf"
Case 3
FileName = "3fault" & EndDate & EndTime & ".inf"
CaseElse
FileName = ""
EndSelect
If FileName.Length > 0 Then
IfNot Directory.Exists(MainPath & "\Collection\" & SiteNum) Then Directory.CreateDirectory(MainPath & "\Collection\" & SiteNum)
Dim fs AsNew FileStream(MainPath & "\Collection\" & SiteNum & "\" & FileName, FileMode.Create)
Dim w AsNew BinaryWriter(fs) ' Create a writer for the data.
w.Write(state.Rbuf, Pos, i - Pos + 1) 'Write the data to a file.
w.Close()
fs.Close()
'Update the list1 with new values
Dim item1 AsNew ListViewItem(EndDate)
item1.SubItems.Add(EndTime)
item1.SubItems.Add(SiteNum)
item1.SubItems.Add(state.IP)
item1.SubItems.Add(state.Port)
item1.SubItems.Add(i - Pos + 1)
item1.SubItems.Add("Connected")
mFrm.List1.Items.Add(item1)
'Write to the log file
WriteLogs(GetDateTimeStr() & " Binary File " & (i - Pos + 1) & " bytes created in folder " _
& MainPath & "\Collection\" & SiteNum & "\")
If Pos - P1 > 0 Then'If it has no corrupt packet
'Pos-P1 is the size of the packet
FileName = (state.Rbuf.GetValue(P1 + 4) * 256 ^ 3 _
+ state.Rbuf.GetValue(P1 + 5) * 256 ^ 2 _
+ state.Rbuf.GetValue(P1 + 6) * 256 _
+ state.Rbuf.GetValue(P1 + 7)) & "corrupt" _
& state.Rbuf.GetValue(Pos + 24) _
& state.Rbuf.GetValue(Pos + 25) _
& state.Rbuf.GetValue(Pos + 26) _
& state.Rbuf.GetValue(Pos + 27) _
& state.Rbuf.GetValue(Pos + 28) _
& state.Rbuf.GetValue(Pos + 29) & ".inf"
Dim efs AsNew FileStream(MainPath & "\Collection\" & SiteNum & "\" & FileName, FileMode.Create)
Dim ew AsNew BinaryWriter(fs) ' Create the writer for data.
ew.Write(state.Rbuf, P1, Pos - P1) ' write corrupt data to the file
ew.Close()
efs.Close()
'Write to the log file
WriteLogs(GetDateTimeStr() & " Binary File " & (Pos - P1) & " bytes created in folder " _
& MainPath & "\Collection\" & SiteNum & "\")
EndIf
P1 = i + 1
EndIf
EndIf
EndIf
EndIf
EndIf
Next
'If there is data that can't be written to a file at last,then copy it to the benginning of the buffer.
state.Rbuf.Copy(state.Rbuf, P1, state.Rbuf, 0, state.Length - P1)
state.Index = state.Length - P1
state.Length = state.Length - P1
Return 0
Catch ex As Exception
WriteErrors(ex.ToString())
MsgBox(ex.Message, MsgBoxStyle.Critical, "ERROR")
Return -1
EndTry
EndFunction

'What it does is that it reads the begin tag and end tag to see where one packet starts and ends and then starts reading data within the packet. Then based on the Type Id from the header data section of the packet, it creates the files. And also it shows the details like IP, PORT, Data Size on the list panel on the screen.

PROBLEM:

- I get multiple connections, but i cannot see anything on the list panel on the screen and it doesnot create any files as well.

PLEASE CAN SOMEONE HELP ME AS TO WHERE AM I GOING WRONG. IF SOME ONE HAS A CODE FOR READING A PACKET using VB.NET, please can you assist?

Thanks


 
Top Bottom