下面两段代码,附带其执行档.
//
//Server.c
//
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma comment(lib,"ws2_32")
int main(int argc, char* argv[])
{
WSADATA wsda; // Structure to store info
char szMessage[512];
int iMessageLen;
int ret;
//char szAddress[64];
int iPort;
SOCKET s; // Our TCP socket handle
SOCKADDR_IN addr, // The local interface address
remote_addr; // The sender's address
int iRemoteAddrLen; // Contains the length of remte_addr, passed to recvfrom
if( argc >= 2 )
{
iPort = atoi( (char *)&argv[1][0] );
}
else
{
iPort = 2020;
}
iMessageLen = 512; // Set to the length of szMessage buffer
// Load version 1.1 of Winsock
WSAStartup(MAKEWORD(2, 2), &wsda);
// Create an UDP socket
printf("Creating socket Server < UDP Mode >...");
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// Error?
if(s == SOCKET_ERROR)
{
printf("Error/nCall to socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); failed with:/n%d/n", WSAGetLastError());
exit(1);
}
printf("OK/n");
// Fill in the interface information
printf("Binding socket...");
addr.sin_family = AF_INET;
addr.sin_port = htons(iPort);
addr.sin_addr.s_addr = INADDR_ANY;
if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) == SOCKET_ERROR)
{
printf("Error/nCall to bind(s, (struct sockaddr *) &addr, sizeof(addr)); failed with:/n%d/n", WSAGetLastError());
exit(1);
}
printf("OK/n");
// Ready to receive data
printf("Waiting for receiving packets <Press Ctrl-C to exit>.../n");
iRemoteAddrLen = sizeof(remote_addr);
memset( szMessage, 0, iMessageLen );
while( true )
{
ret = recvfrom(s, szMessage, iMessageLen, 0, (struct sockaddr *) &remote_addr, &iRemoteAddrLen);
if(ret == SOCKET_ERROR)
{
printf("Error/nCall to recvfrom(s, szMessage, iMessageLen, 0, (struct sockaddr *) &remote_addr, &iRemoteAddrLen); failed with:/n%d/n", WSAGetLastError());
exit(1);
}
else
{
szMessage[iMessageLen] = '/0'; // Convert to cstring
printf("Received from %s with %2d bytes: /"%s/"/n", inet_ntoa(remote_addr.sin_addr), ret, szMessage);
memset( szMessage, 0, iMessageLen );
}
}
closesocket(s);
WSACleanup();
return 0;
}
//
// Client.c
//
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma comment(lib,"ws2_32")
int main(int argc, char* argv[])
{
WSADATA wsda; // Structure to store info
struct hostent *host; // Used to store information
char szMessage[256];
int ret;
char szAddress[64];
int iPort;
SOCKET s; // Our TCP socket handle
SOCKADDR_IN addr; // The host's address
// Copy the IP address
if( argc >= 3 )
{
strcpy(szAddress, (char *) &argv[1][0]);
iPort = atoi( (char *)&argv[2][0] );
}
else if( argc > 1 )
{
strcpy(szAddress, (char *) &argv[1][0]);
iPort = 2020;
}
else
{
strcpy(szAddress, "127.0.0.1");
iPort = 2020;
}
printf("Ports: %d/n", iPort);
// Load version 1.1 of Winsock
WSAStartup(MAKEWORD(2,2), &wsda);
// Create a UDP socket
printf("Creating socket Client < UDP Mode >...");
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// Error?
if(s == SOCKET_ERROR)
{
printf("Error/nCall to socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); failed with:/n%d/n", WSAGetLastError());
exit(1);
}
printf("OK/n");
// Fill in the host information
addr.sin_family = AF_INET;
addr.sin_port = htons(iPort);
addr.sin_addr.s_addr = inet_addr(szAddress);
if(addr.sin_addr.s_addr == INADDR_NONE) // The address wasn't in numeric form, resolve it
{
host = NULL;
printf("Resolving host...");
host = gethostbyname(szAddress); // Get the IP address of the server and store it in host
if(host == NULL)
{
printf("Error/nUnknown host: %s/n", szAddress);
exit(1);
}
memcpy(&addr.sin_addr, host->h_addr_list[0],
host->h_length);
printf("OK/n");
}
puts( "Please input anything to be sent < please Ctrl-C to exit >..." );
// Ready to send data
while( true )
{
gets( szMessage );
if( strstr(szMessage, "exit" ) != 0 )
{
break;
}
ret = sendto(s, szMessage, strlen( szMessage ), 0, (struct sockaddr *) &addr, sizeof(addr));
if(ret == SOCKET_ERROR)
{
printf("Error/nCall to sendto(s, szMessage, iMessageLen, 0, (SOCKADDR_IN *) &addr, sizeof(addr)); failed with:/n%d/n", WSAGetLastError());
exit(1);
}
//printf("Sent to %s with %d bytes: /"%s/"/n", szAddress, ret, szMessage);
}
closesocket(s);
WSACleanup();
return 0;
}