[En-Nut-Discussion] Problem to connect a tcp socket

Julien CHAUSSEC Julien.Chaussec at ifremer.fr
Wed Jul 20 09:58:49 CEST 2005


Hello everybody,
the problem i have is i can't connect a TCP socket to a server (who 
works). I use an example, of the ethernut software manual 2.3, page39 "a 
simple tcp client looks likethis:..." .
The NutTcpConnect() function return -1 and i really don't understand why.
The several printf show correct ip address (local & remote), but the 
local port number is 272 (i don't know why) and the remote port number 
is -28241 nevertheless i set the remote port number to 5010 with the 
NutTcpConnect() function!! The NutTcpError() function returns the value 
10064 --> "host is down".
I don't know what to do. If anyone have an idea?
Thank you.

#include <cfg/os.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>

#include <dev/nicrtl.h>
#include <dev/debug.h>

#include <sys/version.h>
#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/socket.h>

#include <arpa/inet.h>
#include <pro/dhcp.h>

#ifdef NUTDEBUG
#include <sys/osdebug.h>
#include <net/netdebug.h>
#endif

#include <sys/confnet.h>
#include <sys/confos.h>

/* adresse mac du contrôleur ethernet */
#define STDNET_MAC { 0x00, 0x0a, 0x59, 0x03, 0x03, 0x0a }

#define STDNET_IPADDR	"134.246.147.207"
#define STDNET_IPMASK	"255.255.255.0"

void main(void)
{
u_char null_mac[] = STDNET_MAC;
int init_ether = NutRegisterDevice(&DEV_ETHER, 0x8300, 5);
u_long ip_addr = inet_addr(STDNET_IPADDR);
u_long ip_mask = inet_addr(STDNET_IPMASK);
init_ether = NutNetIfConfig("eth0", null_mac, ip_addr,ip_mask);

char tab [50];
int rt;
u_short error;
for (;;)
	{
	TCPSOCKET *sock = NutTcpCreateSocket();
	if (sock==0)
	{break;
	}
	rt = NutTcpConnect( sock,inet_addr("134.246.147.85"), 5010);
	printf("%d\n\r",rt);

	/* display of the remote address ip*/
	printf("%s\n\r",inet_ntoa(sock->so_remote_addr));
	
	/* display of the remote port number*/
	printf("%d\n\r",sock->so_remote_port);
	
	/* display of the local address ip*/
	printf(" %s\n\r",inet_ntoa(sock->so_local_addr));
	
	/*  display of the local port number*/
	printf("%d\n\r",sock->so_local_port);
	
	error = NutTcpError(sock);
	printf(" %d\n\r", error);
	
	if(rt ==-1)	
	{	
	break;
	}	
	sprintf(tab,"bonjour \n\r");
	printf("tab=%s\n\r", tab);	
	rt = NutTcpSend(sock, &tab,10 );
	printf("envoi du mot bonjour(rt = %d)\n\r", rt);		
	NutSleep(1000);
	rt = NutTcpCloseSocket(sock);
     }
}




More information about the En-Nut-Discussion mailing list