IPv6 implement UDP programming example _c language

Source: Internet
Author: User

Development test Environment: WIN7SP1, VS2005

UdpServer.cpp

Copy Code code as follows:

#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>

#pragma comment (lib, "Ws2_32.lib")

int main (int argc, char** argv)
{
using namespace Std;

cout<< "Server Start ..." <<endl;

WORD wversion = 0;
Wsadata Wsadata;
memset (&wsadata, 0, sizeof (wsadata));
Wversion = Makeword (2, 2);

if (0!= WSAStartup (wversion, &wsadata))
{
return-1;
}

if (2!= lobyte (wsadata.wversion) | | 2!= hibyte (wsadata.wversion))
{
WSACleanup ();
return-1;
}

struct Addrinfo hints;
struct addrinfo* res = NULL;
memset (&hints, 0, sizeof (hints));
hints.ai_family = Af_unspec;
Hints.ai_socktype = Sock_dgram;
Hints.ai_protocol = IPPROTO_UDP;
Hints.ai_flags = ai_passive;

if (0!= getaddrinfo ("192.168.0.19", "6677", &hints, &res))
{
WSACleanup ();
return-1;
}

Socket SK = socket (res->ai_family, Res->ai_socktype, Res->ai_protocol);
Bind (SK, RES->AI_ADDR, Res->ai_addrlen);

Char recvbuf[1024] = {' I '};
int lenrecv = 0;

struct Sockaddr_storage storage;
int len = sizeof (storage);
memset (&storage, 0, Len);

LENRECV = Recvfrom (SK, RECVBUF, 1024, 0, (struct sockaddr*) &storage, &len);
cout<< "Recv from Client:" <<recvBuf<< "Len:" <<lenRecv<<endl;
SendTo (SK, Recvbuf, LENRECV, 0, (struct sockaddr*) &storage, Len);

WSACleanup ();
Closesocket (SK);

return 0;
}

UdpClient.cpp

Copy Code code as follows:

#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>

#pragma comment (lib, "Ws2_32.lib")

int main (int argc, char** argv)
{
using namespace Std;

cout<< "Client Start ..." <<endl;

WORD wversion = 0;
Wsadata Wsadata;
memset (&wsadata, 0, sizeof (wsadata));
Wversion = Makeword (2, 2);

if (0!= WSAStartup (wversion, &wsadata))
{
return-1;
}

if (2!= lobyte (wsadata.wversion) | | 2!= hibyte (wsadata.wversion))
{
WSACleanup ();
return-1;
}

struct Addrinfo hints;
struct addrinfo* res = NULL;
memset (&hints, 0, sizeof (hints));
hints.ai_family = Af_unspec;
Hints.ai_socktype = Sock_dgram;
Hints.ai_protocol = IPPROTO_UDP;
Hints.ai_flags = Ai_canonname;

if (0!= getaddrinfo ("192.168.0.19", "6677", &hints, &res))
{
WSACleanup ();
return-1;
}

Socket SK = socket (res->ai_family, Res->ai_socktype, Res->ai_protocol);
Bind (SK, RES->AI_ADDR, Res->ai_addrlen);

Char recvbuf[1024] = {' I '};
Char sendbuf[1024] = {"ABCD"};
int len = res->ai_addrlen;
int lenrecv = 0;

SendTo (SK, SendBuf, strlen (SendBuf), 0, res->ai_addr, Len);
LENRECV = Recvfrom (SK, RECVBUF, 1024, 0, RES->AI_ADDR, & (Len));
cout<< "Recv from Server:" <<recvBuf<< "Len:" <<lenRecv<<endl;

WSACleanup ();
Closesocket (SK);

return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.