Click here to Skip to main content
15,882,376 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello

I have been trying for so long to send a message from one computer to another.

I know the ip address of both, and i'm able to send an image from one to another with serialization.

The problem is that both computers need to send data to each other, like this:

com1) Hello, can you send me your image please?
com2) Yes, here you go
com2) image-->com1

I think the problem is that the tcpclient is busy taking in the message so it forgets to send the return message.

Please don't link sites to me, as i have literally read through every molecule on the internet to no avail. [code only please, must be vb.net]

I got this from MSDN, but what I don't understand is how you can be sure there will be a response from the client before the sub ends, so I didn't use it.

VB
Shared Sub Connect(ByVal server As [String], ByVal message As [String])
       Try
           ' Create a TcpClient.
           ' Note, for this client to work you need to have a TcpServer
           ' connected to the same address as specified by the server, port
           ' combination.
           Dim port As Int32 = 13000
           Dim client As New TcpClient(server, port)

           ' Translate the passed message into ASCII and store it as a Byte array.
           Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)

           ' Get a client stream for reading and writing.
           '  Stream stream = client.GetStream();
           Dim stream As NetworkStream = client.GetStream()

           ' Send the message to the connected TcpServer.
           stream.Write(data, 0, data.Length)

           Console.WriteLine("Sent: {0}", message)

           ' Receive the TcpServer.response.
           ' Buffer to store the response bytes.
           data = New [Byte](256) {}

           ' String to store the response ASCII representation.
           Dim responseData As [String] = [String].Empty

           ' Read the first batch of the TcpServer response bytes.
           Dim bytes As Int32 = stream.Read(data, 0, data.Length)
           responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
           Console.WriteLine("Received: {0}", responseData)

           ' Close everything.
           stream.Close()
           client.Close()
       Catch ex As Exception
           MsgBox("Error")
       End Try
   End Sub 'Connect


My code to send an image is from here: http://lagabuse.com/forum/index.php?topic=90829.0</a>[^], which was useful because the sender and receiver code are separate.

Please help me!
Posted
Updated 22-Aug-13 1:32am
v2

1 solution

You need to think in terms of what protocol is needed between server and client. At the simplest level it goes something like:
Server: listen for connection
Client: call server and wait for successful connection.
Client: send request to server.
Server: respond with "ready to receive".
Client: send some data.
Server: receive data and store as appropriate, send "ready for more".
Client and Server: repeat the above send/receive sequence.
Client: send "End of data".
Server: receive "End of data", return to idle mode.
Client: close connection.
Server: return to listening mode, or terminate, as approprioate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900