WIZnet Developer Forum

How do I get client ip address in TCP mode?

I am running a W5500 in TCP mode as a server. I can successfully connect to the W5500 from a PC client and send a message to the W5500 using TCP. However, unlike the W5500 running in UDP mode, there is no (client) address in the receive buffer and so there is no way to respond to the client because its address is unknown. Obviously, I am missing something here!!

I would appreciate it if someone could tell me where to find the IP address of the client sending a message to the W5500.

Hello BitBanger46,
UDP is connectionless so you can retrieve the address of the incoming stream from first bytes of the same.
With TCP in order to communicate you have to have established a connection.
The following is an example of TCP server do in basic metalanguage that go in the continuous loop of your main program …

        sockstat=ReadSocketReg(sockreg, rSn_SR)
        Select sockstat
        Case SOCK_CLOSED
            If Socket(sockreg, cSn_MR_TCP, TCP_PORT, 0) Then 
                If Not Listen(sockreg) Then
                    DelayMS(1)
                End If
            End If
        Case SOCK_ESTABLISHED       
            rsize = W5500.DataAvailable(sockreg)
            If rsize > 0 Then
                 ' Here someone has connected and there is a flow of data.
                 ' You can read here Sn_DIPR0..Sn_DIPR3  for IP address and Sn_DHAR0 .. Sn_DHAR5 for MAC.
                 ' However you can read the receive buffer and create the answer in the transmit buffer 
                 ' without having to know the address of the caller.
                 ' If you need to communicate in HTTP remember to disconnect the socket 
                 ' just concluded the transmission otherwise receiving will go in timeout.
                 .....
            End If              
        Case SOCK_FIN_WAIT,
                SOCK_CLOSING,
                SOCK_TIME_WAIT,
                SOCK_CLOSE_WAIT,
                SOCK_LAST_ACK
            ' here has disconnected then close the socket
            ......
       End Select

When the connection is established in fact you do not need anything else unless you intend to open a second socket to those who contacted you earlier in the socket. However you can know the remote address via the 4 registers ( socket offset registers ) Sn_DIPR0…Sn_DIPR3 for the IP address and via Sn_DHAR0 … Sn_DHAR5 its MAC address.

In the C++ sources as provided in W5500 support site are routines that do this.

Thank you for the help!! I am somewhat embarrassed because I didn’t pay closer attention to the W5500 data sheet while I was coding the register access library for my application. I did test my code in a TCP/IP environment and it indeed works properly without having to load the DIPR and DPORT registers with the address of the client.

hello,
As you said you have successfully established TCP connection, I am doing the same. I am using W5500 as TCP server. And I am using hyperterinal as my TCP client.
But when my socket goes in LISTEN mode, it does not get any reply and even from the hyper terminal it is not able to connect. Means either the hyperterminal is not sending sync packet or my W5500 is not accepting it.
In hyperterminal it is showing UNABLE TO CONNECT.

Can you please give any solution for this ?

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.