Клиент.
- Код: Выделить всё
Client = New TcpClient
Dim formatter As New SoapFormatter()
Dim q As MyCls.cls_Enum.s_QUOTE
Client.Connect(IPAddress.Parse("192.168.0.2"), 3333)
Dim stream As NetworkStream = Client.GetStream
formatter.Serialize(stream, q)
Собственно соединение с сервером проходит нормально.
Для каждого соединения создается экземляр вот этого класса.
- Код: Выделить всё
Public Class cls_Tcp_Client
Public Client As TcpClient
Public Id As String
Dim rb(2048 * 20) As Byte
Dim stream As NetworkStream
Dim ms As New MemoryStream
Public Sub New(ByVal nId As String, ByVal nClient As TcpClient)
Try
Id = nId
Client = nClient
stream = Client.GetStream
stream.BeginRead(rb, 0, rb.Length, New AsyncCallback(AddressOf Me.Recive), Nothing)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Sub Recive(ByVal ar As IAsyncResult)
Dim q As MyCls.cls_Enum.s_QUOTE
Dim formatter As New SoapFormatter
Dim i As Integer
Try
i = stream.EndRead(ar)
If i < 1 Then Return
If stream.DataAvailable Then
ms.Write(rb, 0, i)
stream.BeginRead(rb, 0, rb.Length, New AsyncCallback(AddressOf Me.Recive), Nothing)
Return
Else
ms.Write(rb, 0, i)
ms.Seek(0, SeekOrigin.Begin)
End If
q = DirectCast(formatter.Deserialize(ms), MyCls.cls_Enum.s_QUOTE)
stream.BeginRead(rb, 0, rb.Length, New AsyncCallback(AddressOf Me.Recive), Nothing)
Catch ex As Exception
MsgBox(Me.Id.ToString & ex.ToString)
End Try
End Sub
End Class
Проблемма.
Иногда работатею Иногда нет.
Выскакивает еррор
"System.Xml.XmlException: Unexpected end of file has occurred. The following elements are not closed: Line 1, position 366.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ThrowUnclosedElements()
at System.Xml.XmlTextReaderImpl.ParseAttributes()
at System.Xml.XmlTextReaderImpl.ParseElement()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Runtime.Serialization.Formatters.Soap.SoapParser.ParseXml()
at System.Runtime.Serialization.Formatters.Soap.SoapParser.Run()
at System.Runtime.Serialization.Formatters.Soap.ObjectReader.Deserialize(HeaderHandler handler, ISerParser serParser)
at System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(Stream serializationStream, HeaderHandler handler)
at System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(Stream serializationStream)
at MyCls.cls_Tcp_Client.Recive(IAsyncResult ar) in C:\SATI\Code\Net\MC\MC\TCP\cls_Tcp_Client.vb:line 36"
Даже не знаю что делать.
Вторая ночь.
Собственно задача.
Создать клиент сервер для обмена обьектами.