Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

TCP Socket latency un-deterministic

Status
Not open for further replies.

alzomor

Advanced Member level 2
Joined
Jun 9, 2005
Messages
674
Helped
39
Reputation
78
Reaction score
8
Trophy points
1,298
Location
Germany
Activity points
6,406
Hi,

I am using a mobile application Android/IOS to as a remote control for home Automation.
The mobile application opens a TCP socket for each command and send the command then close the TCP Socket.
connection is over WiFi and/or 3G

The problem is that the time latency for opening the socket is not deterministic, each time the applications opens the socket it take different time.and some times it's more than 5 Seconds.
Which is not comfortable for remote control application?

My Questions are
1-Is it normal that the TCP socket open process takes un-deterministic time?
2-Can I reduce this time?
3-Is it Possible to use UDP instead of TCP? IS it reliable for such application?

Regards
Hossam Alzomor
 

setting up a TCP connection is complex
1. why do you open/close the TCP connection for each transmission
2. UDP is a connectionless datagram service and you have no guarantee that the message will be received. The receiver could send an acknowledgement so if no acknowledgement is received within a timeout period the transmitter can resend
3. is the communication local? if so use a system such as zigbee
 

Thanks Horace for your reply

1. I am using MicroChip - Roving Networks- RN171 WiFi module for Wifi Connection, it has only one TCP port, so I need to close the TCP connection to allow other users to connect to the RN171 .
2. The Receiver Sends a reply on each received command.
3. No it's not local , I may connect from remote location using 3G

My questions again
Is it normal to take long time for opening TCP the socket? it takes 1 to 3 seconds and some times go up to 10 seconds
Is it possible to reduce open Socket latency ?
How can I get the IP address of the sender in UDP in order to reply to it?

Best Regards
Hossam Alzomor
 

TCP sets up a virtual circuit between the transmitter and receiver which can be a complex and time consuming process - do a web search for "TCP routing" and "TCP virtual circuit".

I have used Microchips RN171 but only for simple applications. In applications with multiple clients I have used the MRF WiFi modules
**broken link removed**
however, this is more complex as you have to configure the TCP/IP stack in your code.

I assume your RN171 is part of a local network behind a NAT box. If the connection to the internet is using a static IP you may be able to use port forwarding to forward UDP packets to the RN171.

how many clients do you have?

TCP/IP over 3G would have similar problems
 

The no of clients goes up to 10 users some of them are local and some are connecting through port forwarding.

So How can I get the IP address of the sender when I get a UDP Packet, or if it's possible to reply directly to lats UDP Sender using RN171

Regards
Hossam Alzomor
 

the UDP packet contains the source IP address
here is a Java UDP server
Code:
// simple server waiting for datagrams and extracting ans displaying string

import java.io.*;
import java.util.*;
import java.net.*;

public class UDPserver extends Thread
{
public static void main(String args[])
{
   try
        {
 // get machine IP address - two methods
 int receivePort=999;                                                                                        // port to receive datagrams from
 Socket soc = new Socket("192.168.1.1", 80);
 System.out.println("UDP server on IP address " + soc.getLocalAddress().getHostAddress() + " port " + receivePort);
 soc.close();
 System.out.println("UDP sevrer on IP address " + InetAddress.getLocalHost().getHostAddress() + " port " + receivePort);
        InetAddress remoteIPaddress;                                    // IP address of remote host
        int remotePort;                                                 // port on remote host to send frames too
        byte[] buffer = new byte[65507];                                // array to put datagrams in
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);  // create packet for datagrams
              // open DatagramSocket to receive and a DatagramePacket to hold the datagrams
              DatagramSocket ds = new DatagramSocket(receivePort);
              // loop forever reading datagrams from the DatagramSocket
              while (true)
                {
                 ds.receive(dp);                                         // wait for next datagram
                 byte[] data = dp.getData();                             // get datagram contents
                 String s = new String(data, 0, dp.getLength());         // create a string from the data
                 System.out.println("\nFrom IP " + dp.getAddress() + " UDP string received '" + s + "'\n");
                }
              }
        catch (IOException se) {System.err.println("error " + se);}
        System.exit(1);                                                 // exit on failure
}
}
and client
Code:
// simple client sending a string in a datagram to a server

import java.io.*;
import java.util.*;
import java.net.*;

public class UDPclient
{

private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])
{
   String remoteIPaddress="192.168.1.10";//127.0.0.1";
   int remotePort = 999;//9000;   
   System.out.println("enter text to transmit");
   while(true)
       try
          {
           String s = in.readLine();
           byte[] data = s.getBytes();                            // get array with data
           DatagramSocket theSocket = new DatagramSocket();       // create datagram socket
           // build the datagram packet
           DatagramPacket   theOutput = new DatagramPacket(data, data.length, InetAddress.getByName(remoteIPaddress), remotePort);
           System.out.println("sendFrame: sent " + s + " to host " + remoteIPaddress + " socket " + remotePort);
           theSocket.send(theOutput);                             // and send the datagram
          }  // end try
       catch (IOException e) {System.err.println("error " +  e); }
}
}
a server run
Code:
C:\temp\UDP>java UDPserver
UDP server on IP address 192.168.1.10 port 999
UDP sevrer on IP address 192.168.1.10 port 999

From IP /192.168.1.10 UDP string received 'hello server'
client run
Code:
C:\temp\UDP>java UDPclient
enter text to transmit
hello server
sendFrame: sent hello server to host 192.168.1.11 socket 999
I have not attempted to receive UDP frames using the RN171 but I assume there must be some way to access the transmitter IP address

is there any reason for using a microcontroller and RN171 for the server - why not a PC it would be much simpler
 

I highly appreciate your support.
I can handle UDP packets using JAVA , my problem is how to get the sender IP address when I get UDP packet on RN171.

it's not logic to put a PC for home automation , just small micro with wifi module can do the job
 

I will see if I can find my RN171 daughter board for the Explorer 16

can you run a UDP receiver on the RN171 and use Java to send it a datagram - see what you get?
 

I set up my Explorer16 with RN171 to connect to the local Wifi and the IP configuration to UDP
1. I could send UDP datagrams from the RN171 to a Java UDP server on my PC
2. I could send UDP datagrams from the UDPclient on my PC to the RN171

when receiving UDP datagrams only the data part of the datagram appears to be sent to the microcontroller
you may be able to get the transmitter IP by using the RN171 "GET IP" command
a simpler alternative is probably to send the transmiiter IP address as part of the data
 

I Setup RN171 with STM32 board for testing

Code:
set ip proto 3
set ip host 0.0.0.0
set ip port "my application port number"
set ip flags 0x47

Now When I send a UDP packet from PC to module the module is capable of replying to it.

This works on FW revision 2.36 but on FW revision 4.41 I am still facing a problem.

on Revision 4.41 when I sent
Code:
get ip
I get the ip of the 1st host which was connected to the RN171 and not the last one.

What is your FW revision? , it's mentioned in the prompt in command mode

Regards
Hossam Alzomor
 

4.00

I have not tested from two machines - will try today!

Looking online there appears to be many problems with V 4.00 and 4.01
 

tested on two remote PC using
Code:
// UDPchat2.java - simple peer-to-peer chat program using UDP
//           - given remote IP address can send strings using UDP datagrams 
//           - will also wait for datagrams and display contents
// remote IP and port are specified via command line - default IP is 127.0.0.1 (i.e. localhost) and port is 1000  
//
// when a datagram is received the transmit remote IP is set to the IP that sent he datagram
//

import java.io.*;
import java.util.*;
import java.net.*;

public class UDPchat2 extends Thread
{
   private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   int port=10000;                             // port to send/receive datagrams on
   InetAddress remoteIPaddress= InetAddress.getByName("192.168.1.64");//"127.0.0.1");              // IP to send datagrams too

   // constructor, parameter is command line parameters
   public UDPchat2(String args[]) throws Exception
    {
    // get remote IP address and port from command line parameters
    if (args.length > 0)    remoteIPaddress =  InetAddress.getByName(args[0]);    // get IPaddress
    if (args.length > 1)    port = Integer.parseInt(args[1]);                     // get port number
    System.out.println("chat program: IP address " + InetAddress.getLocalHost().toString() + " port " + port );
    
    start();        // start thread to receive and display datagrams

    // loop waiting for keyboard input, send datagram to remote IP                
    while(true)
      try
        {
        String s = in.readLine() + "  from " + InetAddress.getLocalHost().toString() +"\n";                                       // read a String
        System.out.println("Sending to " + remoteIPaddress + " socket " + port + " data: " + s);
        byte[] data = s.getBytes();                                     // convert to byte array
        DatagramSocket theSocket = new DatagramSocket();                // create datagram socket and the datagram
        DatagramPacket   theOutput = new DatagramPacket(data, data.length, remoteIPaddress, port);
        theSocket.send(theOutput);                                      // and send the datagram
       }
      catch (Exception e) {System.out.println("Eroor sending datagram " + e);}
    }

   // thread run method, receives datagram and display contents as a string
   public void run()                
        {
          try
              {
              // open DatagramSocket to receive 
              DatagramSocket ds = new DatagramSocket(port);
              // loop forever reading datagrams from the DatagramSocket
              while (true)
                 {
                 byte[] buffer = new byte[65507];                       // array to put datagrams in
                 DatagramPacket dp = new DatagramPacket(buffer, buffer.length); // DatagramPacket to hold the datagram
                 ds.receive(dp);                                        // wait for next datagram
                 String s = new String(dp.getData(),0,dp.getLength());  // get contenets as a String
                 System.out.println("UDP datagram length " + s.length()+ "  from IP " + dp.getAddress() + " received: " + s );
                 remoteIPaddress=dp.getAddress();                       // set the transmit address to this one
                 }
              }
          catch (SocketException se) {System.err.println("chat error " + se); }
          catch (IOException se) {System.err.println("chat error " + se);}
          System.exit(1);                                                       // exit on error
        }


public static void main(String args[]) throws Exception
{
   UDPchat2 c=new UDPchat2(args);
}

}
output from RN171
Code:
CMD
ggeett  iipp


IF=UP
DHCP=ON
IP=192.168.1.64:10000
NM=255.255.255.0
GW=192.168.1.254
HOST=192.168.1.66:10000
PROTO=UDP,
MTU=1524
FLAGS=0x7
TCPMODE=0x0
BACKUP=0.0.0.0
<4.00> eexxiitt


EXIT
hello  from 192.168.1.66
message2  from 192.168.1.66
xyz
hello from PC2  from 192.168.1.65
message from pc2  from 192.168.1.65
test
output from one of the PCs
Code:
c:\temp\UDP>java UDPchat2
chat program: IP address 192.168.1.66 port 10000
hello
Sending to /192.168.1.64 socket 10000 data: hello  from 192.168.1.66

message2
Sending to /192.168.1.64 socket 10000 data: message2  from 192.168.1.66

UDP datagram length 1  from IP /192.168.1.64 received: x
UDP datagram length 1  from IP /192.168.1.64 received: y
UDP datagram length 1  from IP /192.168.1.64 received: z
UDP datagram length 1  from IP /192.168.1.64 received:
UDP datagram length 1  from IP /192.168.1.64 received: t
UDP datagram length 1  from IP /192.168.1.64 received: e
UDP datagram length 1  from IP /192.168.1.64 received: s
UDP datagram length 1  from IP /192.168.1.64 received:
although it is receiving from two PCs text sent to the RN171 is sent to the PC whos IP is set using command "set ip host ...."
to reply to a particular PC you will have to send "set ip host ..." commands to the RN171 as required
it can be seen that the IP address sent to the RN171 are on a local networks behind a NAT box
if your remote machines are on local networks you will have to get the IP of the internet gateway etc etc
 

Hi

did you set the host IP address to 0.0.0.0, and ip flags to 47
in order to allow auto paring?

also I allow both TCP with UDP , in order to be able to access the RN through telnet for configurations
 

Hi

I did another work around to allow RN171 to reply to each remote host when getting UDP packet
after each UDP reply from RN171,
enter to command mode ,set host ip to 0.0.0.0, save and exit command mode.
This way I refresh the auto pair function
but I have two problems with this work around
1-it introduces about 800 ms delay for each command
2-it's not reliable , some times I didn't get the UDP reply from the RN171 and it happens one time that the module hangs and I did a power cycle in order to get it back.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top