接收端,可以看做服务端
#include "stdafx.h"
#include <winsock2.h>
#include <stdlib.h>
#define DEFAULT_PORT 5150
#define DEFAULT_COUNT 25
#define DEFAULT_BUFFER_LENGTH 4096
int iPort = DEFAULT_PORT; // Port to receive on
DWORD dwCount = DEFAULT_COUNT, // Number of messages to read
dwLength = DEFAULT_BUFFER_LENGTH; // Length of receiving buffer
int main()
{
WSADATA wsd;
SOCKET s;
char *recvbuf = NULL;
int ret;
DWORD i;
int iSenderSize;
SOCKADDR_IN sender,
local;
// load Winsock
//
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
// Create the socket and bind it to a local interface and port
//
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == INVALID_SOCKET)
{
printf("socket() failed; %d\n", WSAGetLastError());
return 1;
}
local.sin_family = AF_INET;
local.sin_port = htons((short)iPort);
local.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)
{
printf("bind() failed: %d\n", WSAGetLastError());
return 1;
}
// Allocate the receive buffer
//
recvbuf = (char*)GlobalAlloc(GMEM_FIXED, dwLength);
if (!recvbuf)
{
printf("GlobalAlloc() failed: %d\n", GetLastError());
return 1;
}
// Read the datagrams
//
for(i = 0; i < dwCount; i++)
{
iSenderSize = sizeof(sender);
ret = recvfrom(s, recvbuf, dwLength, 0,
(SOCKADDR *)&sender, &iSenderSize);
if (ret == SOCKET_ERROR)
{
printf("recvfrom() failed; %d\n", WSAGetLastError());
break;
}
else if (ret == 0)
{
break;
}
else
{
recvbuf[ret] = '\0';
printf("[%s] sent me: '%s'\n",
inet_ntoa(sender.sin_addr), recvbuf);
}
}
closesocket(s);
GlobalFree(recvbuf);
WSACleanup();
return 0;
}
发送端,可以看做是客户端
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_PORT 5150
#define DEFAULT_COUNT 25
#define DEFAULT_CHAR 'a'
#define DEFAULT_BUFFER_LENGTH 64
BOOL bConnect = TRUE; // Connect to recipient first
int iPort = DEFAULT_PORT; // Port to send data to
char cChar = DEFAULT_CHAR; // Character to fill buffer
DWORD dwCount = DEFAULT_COUNT, // Number of messages to send
dwLength = DEFAULT_BUFFER_LENGTH; // Length of buffer to send
int main()
{
WSADATA wsd;
SOCKET s;
char *sendbuf = NULL;
int ret,
i;
SOCKADDR_IN recipient;
// load Winsock
//
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
// Create the socket
//
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == INVALID_SOCKET)
{
printf("socket() failed; %d\n", WSAGetLastError());
return 1;
}
// Resolve the recipient's IP address or hostname
//
recipient.sin_family = AF_INET;
recipient.sin_port = htons((short)iPort);
recipient.sin_addr.s_addr = inet_addr("127.0.0.1");
// Allocate the send buffer
//
sendbuf = (char*)GlobalAlloc(GMEM_FIXED, dwLength);
if (!sendbuf)
{
printf("GlobalAlloc() failed: %d\n", GetLastError());
return 1;
}
memset(sendbuf, cChar, dwLength);
//
// If the connect option is set, "connect" to the recipient
// and send the data with the send() function
//
if (bConnect)
{
if (connect(s, (SOCKADDR *)&recipient,
sizeof(recipient)) == SOCKET_ERROR)
{
printf("connect() failed: %d\n", WSAGetLastError());
GlobalFree(sendbuf);
WSACleanup();
return 1;
}
for(i = 0; i < dwCount; i++)
{
ret = send(s, sendbuf, dwLength, 0);
if (ret == SOCKET_ERROR)
{
printf("send() failed: %d\n", WSAGetLastError());
break;
}
else if (ret == 0)
break;
// send() succeeded!
}
}
else
{
// Otherwise, use the sendto() function
//
for(i = 0; i < dwCount; i++)
{
ret = sendto(s, sendbuf, dwLength, 0,
(SOCKADDR *)&recipient, sizeof(recipient));
if (ret == SOCKET_ERROR)
{
printf("sendto() failed; %d\n", WSAGetLastError());
break;
}
else if (ret == 0)
{
break;
}
// sendto() succeeded!
}
}
closesocket(s);
GlobalFree(sendbuf);
WSACleanup();
return 0;
}