Sending images using a NetworkStream

ozonic

Member
Joined
Jun 17, 2006
Messages
21
Programming Experience
Beginner
I am new to VB.NET and I have a bit of a problem. I've got a Client/Server program using the TcpClient and TcpListener. I am able to send strings from the Client to the Server using a NetworkStream and converting the strings from text to bytes using the following instructions:

'Convert string to bytes
Dim Buffer As [Byte]() = System.Text.Encoding.ASCII.GetBytes("String to send")

'Send contents of buffer (bytes)
NetworkStream.Write(Buffer, 0, Buffer.Length)

My question is: How can I possibly send an image from the client (TcpClient) to the server (TcpListener)? Would I have to convert the image into bytes in the same way as I've done with the strings, and if so how do I do that? If that's not possible, do you have any other suggestions?

I would greately appreciate any ideas.
 
Well, in case you didn't understand,
A) you need multiple connections
B) and if you want anything to happen at the same time this have to be done with asynchronous threading.
These are the two requirements.
 
Multiple Connections

So John, when you say I need multiple connections, is this what you mean on the client side? Sorry I know it can be simple yet so difficult.

Requirement A

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ip [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.IPAddress = Net.IPAddress.Parse([/SIZE][SIZE=2][COLOR=#a31515]"172.0.0.1"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]tcp = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Net.Sockets.TcpClient[/SIZE]
[SIZE=2]tcp.Connect(ip, 5000)[/SIZE]
[SIZE=2]ns = tcp.GetStream[/SIZE]
[SIZE=2]tcp2.Connect(ip, 5000)[/SIZE]
[SIZE=2]ns2 = tcp2.GetStream[/SIZE]

Requirement B

So asynchronous threading would be using the example that you posted in post #15?
 
Last edited:
The Begin-/End- method pairs are asynchronous (almost everywhere you find such in the library), you could define your own delegate and call it BeginInvoke, you could create a new Thread instance, you could use ThreadPool.QueueUserWorkItem, you could use the BackgroundWorker. As you see much threading options are available and they are all very easy to use, but first you should get familiar with them by reading some tutorials about multithreading in VB.Net and of course try to code with them.
 
So I realized that after looking around through the net/sockets forum that I could listen on the same port that is already in use by running the listen.Start() command, and was able to successfully connect another connection between the same client and the same server.

Wow, something so simple yet so complicated when one doesn't understand. Thanks for the help.
 
The listener.Start() command only opens the socket ready for accepting connections, you normally use a timer or looping in a thread to check if listener has any Pending connections and if so accept them (and usually throws the communication off to its own thread or the connection into a pool of clients that are handled at some point later).
 
Back
Top