Information Technology Reference
In-Depth Information
{
// Socket failed
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(100); // port=100
if (bind(s, (struct sockaddr FAR *)&sin, sizeof (sin))==SOCKET_ERROR)
{
// Bind failed
}
if (listen(s,4)<0)
{
// Listen failed
}
sin_len = sizeof(sin);
s=accept(s,(struct sockaddr FAR *) & sin,(int FAR *) &sin_len);
if (s<0)
{
// Accept failed
}
while (1)
{
// get message to send and put into sendbuff
send(s,sendbuf,strlen(sendbuf),80);
}
return(0);
}
recv()
The recv() function receives data from a socket. It waits until data arrives and its syntax is:
int recv(SOCKET s, char FAR *buf, int len, int flags)
where
s
Connected socket descriptor.
buf
Incoming data buffer.
len
Buffer length.
flags
Specifies the method by which the data is received.
If recv() succeeds then the return value is the number of bytes received (a zero identifies
that the connection has been closed). Otherwise, it returns SOCKET_ERROR , and the specific
error code can be tested with WSAGetLastError .
The flags parameter may have one of the following values:
MSG_PEEK Peek at the incoming data. Any received data is copied into the buffer, but
not removed from the input queue.
MSG_OOB
Process out-of-band data.
#include <windows.h>
#include <winsock.h>
Search WWH ::




Custom Search