[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lwip-users] TCP connection problem
From: |
Pomeroy, Marty |
Subject: |
Re: [lwip-users] TCP connection problem |
Date: |
Fri, 12 Jul 2013 16:35:16 -0400 |
>> Thanks for reply, Marty.
>> But how can I definitively close the old connection and begin a new
connection ?
Flavio
You're welcome, but you can't force a socket fully closed quickly. To
connect on the same port, you just have to wait four minutes. Microsoft
says you can get that down to 30 seconds (Google up TcpTimedWaitDelay),
but I have never tried.
This is how the standard Transition State table runs for TCP Ports. You
cannot re-use the same port until the WAIT expires, so that any old
packets still floating around don't get delivered on the newly opened
port with the same number.
I'm using the sockets interface in lwIP, and open a listener socket on
my server, which assigns an available port number when you connect. In
case you can use a listener on the server and lwIP sockets, I posted the
code below.
Anyone else have an alternative?
Marty
static unsigned int s_connectSocket = INVALID_SOCKET;
static int s_SetupTcpClient()
{
sockaddr_in sockAddr;
unsigned char ipBytes[4] = {192, 168, 20, 85}; // Server IP
unsigned long *ip = (unsigned long *)&ipBytes;
s_connectSocket = lwip_socket( AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s_connectSocket == -1)
{
// failed
return EXH_USER_ACTION_SOCKET_NO_CONNECT;
}
sockAddr.sin_len = sizeof(struct sockaddr_in);
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = PP_HTONS( DEFAULT_PORT);
sockAddr.sin_addr.s_addr = *ip;
int err = lwip_connect( s_connectSocket, (const struct sockaddr *)
&sockAddr, (socklen_t) sizeof(struct sockaddr_in));
if (err == ERR_OK)
{
return 0;
}
else
{
err = lwip_close( s_connectSocket);
return 1;
}
}
...
pktLen = recv(s_connectSocket, s_recvBuf, expectedSize, 0);