Ok here we go. Here is an example sending from an application to
another application
the image contained in a picture box
Sorry not much refined but I am in the middle of a huge project. It
works anyway...
I have coded it so that yone can send object of any size (buffer can be
adjusted: could
use a few MB buffer) I used different small numbers just to try...
In case of problems post here. Others may contribute with some
important suggestion...
Sorry I am in a hurry...
-Tommaso
SERVER SIDE CODE
------------------------------------------------------------------------------
Dim SizeOfTheReceiveBuffer As Integer = 2048 'change
appropriately
Const Port As Integer = 60000 'change appropriately
Dim LocalIP As System.Net.IPAddress =
IPAddress.Parse("127.0.0.1")
Dim TcpListener As New TcpListener(LocalIP, Port)
Try
TcpListener.Start()
Catch ex As Exception
MsgBox("Port may be already in use by another app" & _
vbCrLf & ex.Message)
Exit Sub
End Try
Dim Socket As Sockets.Socket
Do
If TcpListener.Pending Then
Socket = TcpListener.AcceptSocket 'wait for client
call
Socket.ReceiveBufferSize = SizeOfTheReceiveBuffer
Dim ReceiveBuffer(SizeOfTheReceiveBuffer - 1) As Byte
'Receive object
Dim MemoryStream As New System.IO.MemoryStream
Dim BytesActuallyReceived As Integer
Do
BytesActuallyReceived =
Socket.Receive(ReceiveBuffer, SizeOfTheReceiveBuffer, SocketFlags.None)
If BytesActuallyReceived > 0 Then
MemoryStream.Write(ReceiveBuffer, 0,
BytesActuallyReceived)
Else
Exit Do
End If
Loop
Try
Dim o As Object
With New BinaryFormatter
MemoryStream.Position = 0
o = .Deserialize(MemoryStream)
Me.PictureBoxReceived.Image = DirectCast(o,
Image)
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
MemoryStream.Close()
End Try
Else
Application.DoEvents()
End If
Loop
Socket.Shutdown(SocketShutdown.Both)
Socket.Close()
TcpListener.Stop()
CLIENT SIDE CODE
--------------------------------------------------------------------------------
Dim SizeOfTheSendBuffer As Integer = 1024 'change appropriately
Const Port As Integer = 60000 'change appropriately
Dim ServerAddress As String = "127.0.0.1" 'change
appropriately if not local
Dim MyObject As Object = Me.PictureBox1.Image
Dim ObjectLength As Long
'Connect to the server
Dim Socket As New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Try
Socket.Connect(ServerAddress, Port)
Catch ex As Exception
MsgBox("(Server may be not responding)" & vbCrLf & _
ex.Message)
Exit Sub
End Try
Socket.SendBufferSize = SizeOfTheSendBuffer
If Not Socket.Connected Then
MsgBox("Unable to connect to host " & ServerAddress)
Else
Using MemoryStream As New MemoryStream
'Serialize object
Dim BinaryFormatter As New BinaryFormatter
With New BinaryFormatter
.Serialize(MemoryStream, MyObject)
ObjectLength = MemoryStream.Position
End With
Dim SendBuffer(SizeOfTheSendBuffer - 1) As Byte
Dim BytesActuallyRead As Integer
'Send pieces of the object
MemoryStream.Position = 0
Do While MemoryStream.Position < ObjectLength
BytesActuallyRead = MemoryStream.Read(SendBuffer,
0, SizeOfTheSendBuffer)
Socket.Send(SendBuffer, BytesActuallyRead,
SocketFlags.None)
Loop
MemoryStream.Close()
Socket.Close()
End Using
End If
Uma - Chellasoft ha scritto: