TCP Bidirectional connection from PC to Device

AWS

Staff member
Jan 10, 2007
Florida
I am trying to connect to Datalogic Memor from my desktop using TCP connection. I wrote a small client to run on the device
anda server that runs on the PC. I have ActiveSync installed on mymachine. All I am trying to do is send some string data from my deviceto my application on the PC. I was told that one of the requirementswas that we can't ask clients to open any ports on their PCs.

Hencethey asked me to see if I can initiate the connection from the PC andhave the server running on the device, and send all the required dataas a response, that way I may be able to avoid opening the connections.

IfI try to do that, I simply can't connect to my device using TCP IP. Theerror I get basically says that it can't find the device using thespecified IP. If I go to cmd prompt and use IPCONFIG on the device, itshows me 192.168.55.101 as the IP. If I try to ping it from my PC, myrequests get timed out.

Everything works fine if I use mydevice as a client and PC as a server, but the problem with that is Iwill need to specify port
in order to establish a connection and we are not supposed to do that.

I searched on the web to find anything that will let me connect to the device and send the data to the device but apart from
TCP IP I couldn't really get much.


Here's the client code on my PC that fails to connect to my server on the device.

TcpClient tcpClient = new TcpClient()
*********** tcpClient.Connect("192.168.55.101", 8000)
*********** NetworkStream networkStream = tcpClient.GetStream()
*********** if (networkStream.CanWrite && networkStream.CanRead)
*********** {
*************** //' Do a simple write.
*************** Byte[] sendBytes = Encoding.ASCII.GetBytes("Sample string")
*************** networkStream.Write(sendBytes, 0, sendBytes.Length)
*************** // Read the NetworkStream into a byte buffer.***********
*************** byte[] bytes = new byte[tcpClient.ReceiveBufferSize]
*************** networkStream.Read(bytes, 0, Convert.ToInt32(tcpClient.ReceiveBufferSize))
*************** // Output the data received from the host to the console.
*************** string returndata = Encoding.ASCII.GetString(bytes)
*************** MessageBox.Show("Server returned: " + returndata)
*********** }
//tcpClient.Close()

Here's the server code:
*********** int portNumber = 8000
*********** TcpListener tcpListener = new TcpListener(IPAddress.Any, portNumber)
*********** tcpListener.Start()
*********** MessageBox.Show("Waiting for connection...")
*********** try
*********** {
*************** //a TcpClient initialized for communication.
*************** TcpClient tcpClient = tcpListener.AcceptTcpClient()
*************** MessageBox.Show("Connection accepted.")
*************** // Get the stream
*************** NetworkStream networkStream = tcpClient.GetStream()
*************** // Read the stream into a byte array
*************** byte[] bytes = new byte[tcpClient.ReceiveBufferSize]
*************** networkStream.Read(bytes, 0, Convert.ToInt32(tcpClient.ReceiveBufferSize))
*************** // Return the data received from the client to the console.
*************** string clientdata = Encoding.ASCII.GetString(bytes, 0, Convert.ToInt32(tcpClient.ReceiveBufferSize))
*************** MessageBox.Show("Client data: " + clientdata)
*************** string responseString = "Connected to server."
*************** Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString)
*************** networkStream.Write(sendBytes, 0, sendBytes.Length)
*************** MessageBox.Show(("Message Sent: " + responseString))
************* //Close TcpListener and TcpClient.
*************** tcpClient.Close()
*************** tcpListener.Stop()
*********** }
*********** catch (Exception ex)
*********** {
*************** MessageBox.Show(ex.Message)
*********** }**********

Pleaselet me know if you know any efficient way to achieve data transfer fromdevice to PC using TCP either with a bidirectional connection or ifthere is any better way to achieve this by any different means.

Thanks


More...

View All Our Microsoft Related Feeds
 

Similar threads

S
Replies
0
Views
28
Soundharya R
S
D
Replies
0
Views
19
DouglasWilliams6
D
Back
Top Bottom