send Function
Sends data on a socket.
Syntax
int send(
[in] SOCKET s,
[in] const char *buf,
[in] int len,
[in] int flags
);
Parameters
| Parameter | Type | Description |
|---|---|---|
s |
SOCKET |
A descriptor that identifies a connected socket. |
buf |
const char * |
A pointer to a buffer containing the data to be sent. |
len |
int |
The number of bytes to send from the buffer pointed to by the buf parameter.
|
flags |
int |
Flags that control the transmission of the outgoing data. |
Return Value
Success
The send function returns the total number of bytes sent, which may be less than the number specified by the len parameter.
Failure
If an error occurs, send returns SOCKET_ERROR. To get extended error information, call WSAGetLastError.
Remarks
The send function is used to write user data to the specified socket. The number of bytes sent can be less than the number requested by the application.
If the socket is connection-oriented, send will transmit data until either the buffer is exhausted or the remote end of the connection acknowledges all the intervening data.
If the socket is connectionless, send will transmit one datagram per call. The datagram will contain at most len bytes.
The flags parameter can be used to influence the behavior of the function beyond the default behavior.
Common values for flags include:
MSG_PEEK: Examine incoming data without removing it from the receive queue.MSG_OOB: Send out-of-band data.
Requirements
Minimum supported client: Windows 2000 Professional
Minimum supported server: Windows 2000 Server
Header: winsock2.h (include winsock2.h)
Library: Ws2_32.lib
DLL: Ws2_32.dll
See Also
sendto, recv, closesocket, WSASocket
Example Code
// A basic example demonstrating the use of the send function.
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
// Link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
int iResult;
SOCKET ConnectSocket = INVALID_SOCKET;
struct sockaddr_in clientService;
char *sendbuf = "This is a test message.";
char recvbuf[64] = "";
int recvbuflen = 64;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// Create a socket for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Resolve the server address and port
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(27015); // Example port
InetPton(AF_INET, L"127.0.0.1", &serverAddr.sin_addr);
// Establish a connection to the server
iResult = connect( ConnectSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr) );
if (iResult == SOCKET_ERROR) {
printf("connect failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Connected to server.\n");
// Send a message to the server
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %d\n", iResult);
// Receive data back from the server
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Bytes received: %d\n");
printf("Received: %.*s\n", iResult, recvbuf);
} else if (iResult == 0) {
printf("Connection closing...\n");
} else {
printf("recv failed: %d\n", WSAGetLastError());
}
// cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}