#include "stdafx.h"
#include <assert.h>
#include "winsock.h"
#pragma comment(lib,"ws2_32.lib")

void CreatUDP();
void CloseUDP();
void senddo(char* dat,int ln);
WSADATA wsd;
SOCKET sHost;
SOCKET sClient;
SOCKADDR_IN servAddr;

extern"C"
{
	char ipadd[15] = "127.0.0.1";
}


void CreatUDP()
{

	if(WSAStartup(MAKEWORD(2,2), &wsd) != 0)
    {
      //  cout<<"WSAStartup failed"<<endl;
        printf("WSAStartup failed");

    }

    //创建套接字
    sHost = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    if(INVALID_SOCKET == sHost)
    {
     //   cout<<"socket failed"<<endl;
		printf("socket failed");      
		WSACleanup();

    }

    //创建客户端套接字
    sClient = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    if(INVALID_SOCKET == sClient)
    {
     //   cout<<"Client socket failed"<<endl;
		printf("Client socket failed"); 
        WSACleanup();

    }

    //设置服务器地址
    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr  = inet_addr(ipadd);
    servAddr.sin_port = htons((short)10888);
//	printf("creat udp OK\n");

}

void senddo(char* dat,int ln)
{
	sendto(sHost,dat,ln,0,(SOCKADDR*)&servAddr,sizeof(SOCKADDR));
/*	char* buf;
	buf = dat;
	while(ln-32>0)
	{
		sendto(sHost,buf,32,0,(SOCKADDR*)&servAddr,sizeof(SOCKADDR));
		buf = buf+32;
		ln = ln -32;
	}
	if(ln!=0)
	{
		sendto(sHost,buf,ln,0,(SOCKADDR*)&servAddr,sizeof(SOCKADDR));
	}*/
}

void CloseUDP()
{
	closesocket(sHost);
    WSACleanup();
}