Jump to content

Recommended Posts

Posted

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

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...