WINC1500 IoT Software APIs: connect

WINC1500 IoT Software API

WINC1500 IoT Software APIs  19.5.2
WINC Software API Reference Manual

Functions

NMI_API sint8 connect (SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
 

Detailed Description

Establishes a TCP connection with a remote server. The asynchronous connect function must be called after receiving a valid socket ID from the socket function. The application socket callback function is notified of a successful new socket connection through the event SOCKET_MSG_CONNECT. A successful connect means the TCP session is active. The application is then required to make a call to the recv to receive any packets transmitted by the remote server, unless the application is interrupted by a notification of socket connection termination.

Function Documentation

◆ connect()

NMI_API sint8 connect ( SOCKET  sock,
struct sockaddr pstrAddr,
uint8  u8AddrLen 
)
Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]pstrAddrAddress of the remote server.
[in]pstrAddrPointer to socket address structure "sockaddr_in" sockaddr_in
[in]u8AddrLenSize of the given socket address structure in bytes. Not currently used, implemented for BSD compatibility only.
Precondition
The socket function must be called to allocate a TCP socket before passing the socket ID to the bind function. If the socket is not bound, you do NOT have to call bind before the "connect" function.
See also
socket recv send close
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Example

The example demonstrates a TCP application, showing how the asynchronous call to the connect function is made through the main function and how the callback function handles the SOCKET_MSG_CONNECT event.

UDP example

struct sockaddr_in Serv_Addr;
SOCKET TcpClientSocket =-1;
int ret = -1
TcpClientSocket = socket(AF_INET,SOCK_STREAM,0);
Serv_Addr.sin_family = AF_INET;
Serv_Addr.sin_port = _htons(1234);
Serv_Addr.sin_addr.s_addr = inet_addr(SERVER);
printf("Connected to server via socket %u\n",TcpClientSocket);
do
{
ret = connect(TcpClientSocket,(sockaddr_in*)&Serv_Addr,sizeof(Serv_Addr));
if(ret != 0)
{
printf("Connection Error\n");
}
else
{
printf("Connection successful.\n");
break;
}
}while(1)

TCP example

if(u8Msg == SOCKET_MSG_CONNECT)
{
if(pstrConnect->s8Error == 0)
{
uint8 acBuffer[GROWL_MSG_SIZE];
uint16 u16MsgSize;
printf("Connect success!\n");
u16MsgSize = FormatMsg(u8ClientID, acBuffer);
send(sock, acBuffer, u16MsgSize, 0);
recv(pstrNotification->Socket, (void*)au8Msg,GROWL_DESCRIPTION_MAX_LENGTH, GROWL_RX_TIMEOUT);
u8Retry = GROWL_CONNECT_RETRY;
}
else
{
M2M_DBG("Connection Failed, Error: %d\n",pstrConnect->s8Error");
close(pstrNotification->Socket);
}
}
Generated on Thu Jan 26 2017 22:15:21 for WINC1500 IoT Software APIs by   doxygen 1.8.13