Adding TCP/IP-based LAN buffer Peek and Flush to ATEasy WinSock Functions

Knowledge Base Article # Q200335

Read Prior Article Read Next Article
Summary This article will show a simple ATEasy example showing how to peek and flush WinSock buffer data
Login to rate article


In order to assert more control in LAN data exchange, you may find it useful to peek at the available buffer data. Peeking doesn't empty the buffer as with the WsReceive ATEasy function. To maintain communication, you will eventually need to flush a buffer. The article discusses how to use Windows Sockets 2 API in ATEasy combined with ATEasy internal WinSock functions.
The example shows how to set up a simple client-server connection to send, receive, peek and flush TCP/IP data.
For more information on using DLLs,  see ATEasy Help. In the example we use Ws2_32.dll and the API defined in Winsock2.h. See the recv function as detailed here: MSDN: recv function

Creating a Server Socket and Connecting to the Client

You can create simple server and client sockets with ATEasy internal library WinSock functions. In this example the client and the server will run on the same machine and communicate with each other. You can also use the internal library to resolve the server IPv4 address to its handle to bind to the created server socket. Lastly, the server socket is set to listen and a separate thread for the client is created.

! Create a server socket
hServerSocket=WsCreate(aWsTcpIp, 0)

! Bind to for example localhost --> TCP socket
lStatus=WsBind(hServerSocket, 10, WsGetNameAddress(""))

! Place socket to listen

! New thread to connect client and send test data
CreateThread(ClientConnectAndSend, 1)

Figure 1. Server socket creation, binding and setting to listen at example address "". Client thread creation.

Creating a Client Socket and Connecting to the Server

The example uses a separate thread for the client. In the client's thread, a client socket is created and connected to the server. After connection, data packet "42\x0" with a null byte terminator is sent to the server.
! Client socket created
hClientSocket=WsCreate(aWsTcpIp, 0)

! Connect client to server
WsConnect(hClientSocket, 10, WsGetNameAddress(""), lTimeout)

! Send data
lStatus=WsSend(hClientSocket, lTimeout,,"42\x0")

Figure 2. Client socket creation, connection and sending bytes to server

Creating a Client Socket and Connecting to the Server and Sending Data

Once the incoming client connection is accepted and data received, a null-terminated peek buffer is initialized with 1 kB of null bytes. The peeked data will be used to Pass/Fail the example's test. Note that ATEasy socket handle are the same as Windows socket handle and can be interchanged.
! Accept incoming hServerSocket connection
hPipe=WsAccept(hServerSocket, 10)

! Wait until data is sent
if hPipe>0
   while m_bSent=False
! Allocate 1 kB for sPacket

! Peek (recv with MSG_PEEK flag) buffer data - not receiving data
lStatus=recv(hPipe,sPacket, 1024, MSG_PEEK)
if lStatus<>3 OR sPacket<>"42"


Figure 3. Accepting client connection and its sent data. Peeking the buffer with recv does not flush sent data from client.

Server Peeking and Receiving the Data

Once the data is peeked, the sent data can be received and flushed. Flushing can be done by receiving a byte of data at a time until none are available. One last peek is done to confirm no more data is available.
! Receive data
lStatus=WsReceive(hPipe, 1, ,sPacket, len(sPacket))
if lStatus<>2 OR sPacket<>"42"

! Ensure buffer is empty - 1 byte at a time
while lStatus>0
   lStatus=WsReceive(hPipe, 1, ,sPacket, 1)

! Peek (recv with MSG_PEEK flag) buffer data to ensure it has been emptied
lStatus=recv(hPipe,sPacket, 1024, MSG_PEEK)
if sPacket<>""

Figure 4. Once more ATEasy WinSock module is used to receive data. A peek is performed to confirm buffer is void of data


ATEasy provides an intuitive interface for conducting WinSock communication. Peeking and flushing sockets can be done with importing Windows Sockets 2 DLL Ws2_32.dll by using the Winsock2 header per above mentioned MSDN. Recv and Recvfrom functions can be used to peek and flush sockets efficiently in ATEasy.

Example Files

ATEasy example project files used in this article:
Article Date 6/5/2020
Keywords LAN, TCP/IP, WinSock, buffer, recv, peek, flush, communication, WinSock

Login to rate article

1 ratings | 5 out of 5
Read Prior Article Read Next Article