Enviroment: M52259 demo board with MQX3.6 on CW 7.2
I have connect the M52259 board to my PC and a UMTS router thru the ethernet port.
I have to make a TCP connection to a server thru the UMTS router.
I can make the connection to my PC and from my PC to the server (thru the UMTS router using telnet), but I can't connect the board directly to the remote server (connection timeout).
Can be a problem with packet latency or else to estabilish the connection?
Thanks,
Diego Derganz
Lists attatched below:
void TCPClient(void)
{
sockaddr_in addr;
_ip_address ipaddr;
uint_32 error;
uint_32 option;
char buffer[TCPCLIENT_RECV_BUF_SIZE];
// sockaddr server_socket_addr;
uint_32 socket_handle;
// _ip_digital digit;
uint_32 opt_val;
uint_32 opt_len;
ipaddr = Server_addr;
printf("\n[TCPClient]: Launching TCPClient...");
opt_val = 3 * 60 * 1000;
opt_len = sizeof(uint_32);
// set remote host(TCPServer) socket address
// server_socket_addr.sin_family = AF_INET;
// server_socket_addr.sin_port = port;
// server_socket_addr.sin_addr.s_addr = ip;
// loop to establish connection
do {
// Create a stream socket for TCPClient.
socket_handle = socket(AF_INET, SOCK_STREAM, 0);
if (socket_handle == RTCS_SOCKET_ERROR) {
printf("\n[TCPClient]: Failed to create socket.");
_task_block();
}
option = 10000; // 10 sec
error = setsockopt(socket_handle, SOL_TCP, OPT_RETRANSMISSION_TIMEOUT, &option, sizeof(option));
// Reduce buffer size of socket to save memory
option = 512;
error = setsockopt(socket_handle, SOL_TCP, OPT_TBSIZE, &option, sizeof(option));
option = 512;
error = setsockopt(socket_handle, SOL_TCP, OPT_RBSIZE, &option, sizeof(option));
option = 1000;
error = setsockopt(socket_handle, SOL_TCP, OPT_TIMEWAIT_TIMEOUT, &option, sizeof(option));
// set 3mins for connection Timeout
option = 3 * 60 * 1000;
error = setsockopt(socket_handle, SOL_TCP, OPT_CONNECT_TIMEOUT, &option, sizeof(option));
if (error != RTCS_OK) {
printf("\n[TCPClient]: Failed to setsockopt for OPT_CONNECT_TIMEOUT, CODE = 0x%x.", error);
_task_block();
}
// try to connect TCPServer
addr.sin_port = 2600;
addr.sin_addr.s_addr = ipaddr;
error = connect(socket_handle, &addr, sizeof(addr));
if (error != RTCS_OK) {
if (error == RTCSERR_TCP_TIMED_OUT) {
printf("\n[TCPClient]: Connection timeout.");
} else if (error == RTCSERR_TCP_CONN_RESET) {
printf("\n[TCPClient]: Connection reset by peer."); // When connect to PC but TCPServer shuntdown, will alert this error
}
shutdown(socket_handle, FLAG_ABORT_CONNECTION);
printf("\n[TCPClient]: Retry to connect TCPServer 5 seconds later...");
_time_delay(5 * 1000);
}
} while (error != RTCS_OK);
// // display connection message
// ip_to_digital(ip, &digit);
// printf("\n[TCPClient]: Connected to TCPServer, IP = %d.%d.%d.%d, Port = %d.",
// digit.high, digit.middlehigh, digit.middlelow, digit.low, port);
printf("\n[TCPClient]: Connected.");
// clean buffer to ensure '\0' filled for recv
memset(buffer, 0, sizeof(buffer));
while (TRUE) {
// Send msg to TCPServer
if (send(socket_handle,
"I'm TCPClient. Please reply to me.", strlen("I'm TCPClient. Please reply to me") + 1,
0) == RTCS_ERROR) {
printf("\n[TCPClient]: Failed to send, CODE = 0x%x.", RTCS_geterror(socket_handle));
_task_block();
}
// wait to receive msg from TCPServer
if (recv(socket_handle, buffer, sizeof(buffer), 0) == RTCS_ERROR) {
error = RTCS_geterror(socket_handle);
// Close TCPClient if TCPServer close first.
if (error == RTCSERR_TCP_CONN_CLOSING) {
shutdown(socket_handle, FLAG_ABORT_CONNECTION);
printf("\n[TCPClient]: Closed.");
break;
// Close TCPClient if TCPServer abort.
} else if (error == RTCSERR_TCP_CONN_RESET) {
shutdown(socket_handle, FLAG_ABORT_CONNECTION);
printf("\n[TCPClient]: Connection reset by peer.");
printf("\n[TCPClient]: Closed.");
break;
} else {
printf("\n[TCPClient]: Failed to recv, CODE = 0x%x.", error);
_task_block();
}
} else {
printf("\n[TCPClient]: Received \"%s\".", buffer);
// clean buffer to ensure '\0' filled for next recv.
memset(buffer, 0, sizeof(buffer));
}
}
}
Solved! Go to Solution.
Now I have resolve the TCP/IP connection thru my router UMTS.
1) I have put a windows size = TCP_DEFAULT_RBSIZE in SYN packet.
2) Because I don't have found how to remove the trailing byte of the Ethernet frame I have put some option byte in the TCP frame to avoid this problem.
I think that it's a UMTS router problem, but can you put some define to patch (windows size in SYN packet and remove trailing byte in Ethernet frame) in the next version? So if another have the same problem can only modify the define.
Someone know how to remove the trailing bytes in the Ethernet frame?
Best regards,
Diego Derganz
Now I have resolve the TCP/IP connection thru my router UMTS.
1) I have put a windows size = TCP_DEFAULT_RBSIZE in SYN packet.
2) Because I don't have found how to remove the trailing byte of the Ethernet frame I have put some option byte in the TCP frame to avoid this problem.
I think that it's a UMTS router problem, but can you put some define to patch (windows size in SYN packet and remove trailing byte in Ethernet frame) in the next version? So if another have the same problem can only modify the define.
Someone know how to remove the trailing bytes in the Ethernet frame?
Best regards,
Diego Derganz
Hi,
Possible to share your code change? (line, file?)
Thx
Christophe
I'm not sure if this is similar fix, but here is source code change for MQX 4.0.2 to put window with SYN packet.
Using wireshark I have see that my router reject the SYN packet of TCP connection with windows size = 0 and I have put a patch on that.
Now I can make the TCP connection from device to the server (thru the router UMTS) only using my PC in the middle like a gateway.
The only thing that my PC do is to remove the trailing bytes added in the ethernet protocol.
Can someone tell me where I have to modify to remove the trailing bytes?
Best regards,
Diego Derganz
Now I have try M52259EVB board with CW 10.4 and MQX 4.0.1
I have try web_hvac demo with webserver disabled and shell enabled.
I have enabled the telnet client, present in MQX, with a little modification to modify the default port.
Log:
Shell (build: Jun 26 2013)
Copyright (c) 2013 Freescale Semiconductor;
shell>
shell> ipconfig
Eth#: 0
Link: on
MAC : 00:00:5e:a8:01:02
IP4 : 192.168.1.2 type: MANUAL
MASK: 255.255.255.0
GATE: 192.168.1.1
DNS1: 8.8.8.8
Link status task stopped
shell>
shell> telnet 192.168.1.3 2600
Connecting to [192.168.1.3] port 2600:
Connection closed
shell> ping *server*
Pinging 147.xx.xx.xx:
Press [q] to cancel.
Reply from [147.xx.xx.xx]: time=205ms
Reply from [147.xx.xx.xx]: time=195ms
Reply from [147.xx.xx.xx]: time=185ms
Request timed out
shell> telnet *server* 2600
Connecting to *server* [147.xx.xx.xx] port 2600:
Connection failed, error 0x1649
Connection closed
shell>
shell> ping 192.168.1.1
Pinging 192.168.1.1:
Press [q] to cancel.
Request timed out
Reply from [192.168.1.1]: time=10ms
Request timed out
Reply from [192.168.1.1]: time=10ms
shell>
It's normal that I have some request timed out in ICMP request?
How can I log all the traffic on the ethernet on the serial console?
Thanks,
Diego Derganz
Hi Diego,
The first thing you need to confirm is that your PC or other device can see the board. That way you will ensure that the problem is not in the communication but at the application level. To probe this you can use a PING command from windows to the IP address of your board. If there is a free path between the board and your PC then you will get the PING answered. Next you will need to verify that the packages from your board are reaching your PC. You can use a ethernet sniffer for this (like wireshark). Next I would remove all the socket iptions and leave the default values until you can probe that there is a communication and that you can perform connections to the server.
My two cents...
Best Regards,
Garabo
Hi Garabo,
the device normally is directly connected with the UMTS router to have access to the internet.
The device has a web server and has to connect sometime with an external server.
Because it can't work I have put another router in the middle to connect device (192.168.1.2), UMTS router (192.168.1.1) and my PC (192.168.1.3).
I can:
- see my device [web server] thru the internet (UMTS router redirect the connection to the device)
- see my device [web server] thru the local LAN (my PC)
- create TCP connection to internet server from my PC (test with telnet)
- create TCP connection to my PC from the device (local LAN)
I can't create the TCP connection from the device to the internet server thru the UMTS router.
Maybe there is some problem during the TCP handshake thru the UMTS router?
The gateway is well configured and I can resolve the DNS queries and receive date and time from SNTP service.
So the only difference from local LAN and internet server is that I have specify an IP outside the netmask, it's correct? I have to modify something else?
Any suggestions for the application layer?
Best regards,
Diego Derganz
Any suggestions on how to debug the TCP connection to understand why it does not work and I get connection timeout?