client:
#include "Winsock2.h"
#include "stdafx.h"
#pragma comment(lib,"Ws2.lib")
// TODO: 在 STDAFX.H 中
// 引用任何所需的附加头文件,而不是在此文件中引用
void SOCKETRACE(char *buf,int len)
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2,0),&wsadata);
struct sockaddr_in ipaddr;
ipaddr.sin_family=AF_INET;
ipaddr.sin_port=htons(11000);
ipaddr.sin_addr.s_addr=inet_addr("169.254.2.2");
int sk=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
int c=connect(sk,(sockaddr*)&ipaddr,sizeof(ipaddr));
send(sk,buf,len,0);
closesocket(sk);
WSACleanup();
}
void OEMTRACEW(BOOL cond, LPCWSTR fmt, ...)
{
if(cond)
{
int n, size = 100;
wchar_t* p ;
va_list ap ;
p = (wchar_t*)malloc(size * sizeof(wchar_t)) ;
while( 1 )
{
/* Try to print in the allocated space. */
va_start( ap, fmt ) ;
n = _vsnwprintf( p, size, fmt, ap ) ;
va_end( ap ) ;
/* If that worked, return the string. */
if( n > -1 && n < size )
break ;
/* Else try again with more space. */
if( n > -1 ) /* C99 conform vsnprintf() */
size = n+1 ; /* precisely what is needed */
else /* glibc 2.0 */
size *= 2 ; /* twice the old size */
p = (wchar_t*)realloc( p, size * sizeof(wchar_t) ) ;
}
char nstring[200]={0};
wcstombs( nstring,p,200);
free( p ) ;
SOCKETRACE(nstring,strlen(nstring));
}
}
void OEMTRACE(BOOL cond, const char * fmt, ...)
{
if(cond)
{
int n, size = 100;
char* p ;
va_list ap ;
p = (char*)malloc(size) ;
while( 1 )
{
/* Try to print in the allocated space. */
va_start( ap, fmt ) ;
n = _vsnprintf( p, size, fmt, ap ) ;
va_end( ap ) ;
/* If that worked, return the string. */
if( n > -1 && n < size )
break ;
/* Else try again with more space. */
if( n > -1 ) /* C99 conform vsnprintf() */
size = n+1 ; /* precisely what is needed */
else /* glibc 2.0 */
size *= 2 ; /* twice the old size */
p = (char*)realloc( p, size ) ;
}
SOCKETRACE(p,strlen(p));
free( p ) ;
}
}
client c#:
udpClient = new UdpClient();
Byte[] bytes = Encoding.Unicode.GetBytes("aa");
udpClient.Send(bytes, bytes.Length,new IPEndPoint(IPAddress.Parse("169.254.2.2"), 11000));
udpserver c#:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace ScocketRec
{
class Program
{
static void Main(string[] args)
{
UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("169.254.2.2"), 11000));
try
{
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse("169.254.2.1"), 11000);
while(true)
{
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());
}
udpClient.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}