一个VC简单的UDP广播示例

接收端:
#include<stdlib.h>
//#include<iostream.h>
#include <conio.h>
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#pragma   comment   ( lib ,   "ws2_32.lib" )
/*

HANDLE hr,hl,hbusy;

  struct  thrpar
  {
                SOCKET s;
  };

  struct recpar
  {
                  SOCKET srec;
                  struct sockaddr_in addr;
  };


 UINT recthread(LPVOID pp)
  {
                  SOCKET srec,s2;
                  struct recpar *prec=(struct recpar *)pp;
      sockaddr_in recaddr;

                  srec=prec->srec;
                  recaddr=prec->addr;
                  char buf[256];
                  DWORD ret;

      printf("recv thread start!");
                  ret=recv(srec,buf,256,0);
                  if(ret==0)
                  {
                                  printf("connect close!");
                                  closesocket(srec);
                                  return 0;
                  }
                  else if(ret==SOCKET_ERROR)
                  {
                                  printf("connect error!");
                                  closesocket(srec);
                                  return 0;
                  }
   printf("recv data:%s",buf);
     return 0;
  }

                 
   DWORD WINAPI listhread(LPVOID pp)
  {
                   ::MessageBox(NULL,"enter the listhread","KK",MB_OK);
                  struct thrpar *pthr=(struct thrpar*)pp;
                  SOCKET sl,saccept;
                  sockaddr_in here, there;
                  char name[256];
                  struct hostent *phost;
                  int ret;
      
                  gethostname(name,256);
                  phost=gethostbyname(name);

                  here.sin_family=AF_INET;
                  here.sin_port=htons(1024);
                  here.sin_addr.S_un.S_un_b.s_b1=phost->h_addr_list[0][0];
                  here.sin_addr.S_un.S_un_b.s_b2=phost->h_addr_list[0][1];
                  here.sin_addr.S_un.S_un_b.s_b3=phost->h_addr_list[0][2];
                  here.sin_addr.S_un.S_un_b.s_b4=phost->h_addr_list[0][3];
     
      sl=pthr->s;
                 
                  ret=bind(sl,(struct sockaddr *)&here,sizeof(here));
                  if(ret!=0)
                  {
                                  printf("bind error");
                                  return 0;
                  }
                  ret=listen(sl,5);
                  if(ret!=0)
                  {
                                   printf("listen error");
                                  return 0;
                  }
                  while(true)
                  {
                                  int nlen=sizeof(there);
                                  saccept=accept(sl,(struct sockaddr *)&there,&nlen);
                                  if(saccept==INVALID_SOCKET)
                                  {
                                                  printf("accept socket invalid");
                                  }
                                  DWORD wret=WaitForSingleObject(hbusy,500);
                                  if(wret==WAIT_TIMEOUT)
                                  {
                                                 send(saccept,"\0",1,0);
                                                closesocket(saccept);
                                                continue ;
                                 }
                                 DWORD recid;
                                  struct recpar *prec;
                                  prec->addr=there;
                                  prec->srec=saccept;
                                  
                                  hr=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&listhread,prec,0,&recid);
                                 return 1;
                  }
                   return 1;
  }

void main(void)
{
                SOCKET s;
                sockaddr_in addrin;
                WSADATA wsdata;
                BOOL bsocket;
                char *smsg="this is a test";
                
                  WSAStartup(0x0202,&wsdata);
                 
                                s=WSASocket(AF_INET,SOCK_STREAM,0,NULL,0,WSA_FLAG_OVERLAPPED);
                                hbusy=CreateSemaphore(NULL,0,2,NULL);
                                struct  thrpar *psc;
                                psc=new struct thrpar;
                                DWORD lisid;
                                psc->s=s;
                                
                                  hr=CreateThread(NULL,0,listhread,psc,0,&lisid);
                                  DWORD ret=WaitForSingleObject(hr,10000);
                                  if(ret==WAIT_OBJECT_0)printf("thread time out%d\r\n",lisid);
                                 
}*/
void   main( void   )
{
                SOCKET s;
                sockaddr_in from,a;
                WSADATA wsdata;
                BOOL optval;
                   //启?动??SOCKET库a,??版??本??为a2.0
                WSAStartup(0x0202,&wsdata);
                optval=TRUE;
                   //然??后??赋3值??给?两??个?地??址??,??一??个?用??来???绑???定??套???接??字??,??一??个?用??来???从???网??络?上??的??广?播??地??址??接??收??消?息??;??
                a.sin_family=AF_INET;
                a.sin_addr.s_addr=0;
                a.sin_port=htons(5050);

                from.sin_family=AF_INET;
                from.sin_addr.s_addr=INADDR_BROADCAST;
                from.sin_port=htons(5050);
                
                   int   fromlength= sizeof   (SOCKADDR);
                   //用??UDP初?始??化??套???接??字??
                s=socket(AF_INET,SOCK_DGRAM,0);
                    //设???置?该?套???接??字??为a广?播??类???型??,??
                setsockopt(s,SOL_SOCKET,SO_BROADCAST,(   char   FAR *)&optval, sizeof   (optval));
                bind(s,(sockaddr *)&a,   sizeof (sockaddr_in));
                   char   buf[256];
                   while (1)
                {   //从???广?播??地??址??接??收??消?息??,??注???意?a用??来???绑???定??的??地??址??和??接??收??消?息??的??地??址??是??不?一??样??的??
                                recvfrom(s,buf,256,0,(   struct   sockaddr FAR *)&from,( int   FAR *)&fromlength);
                                Sleep(2000);
                                printf(   "%s\n" ,buf);
                                ZeroMemory(buf,256);
                }
}

发送端:
#include <stdlib.h>
//#include<iostream.h>
#include <stdio.h>
#include <conio.h>
#include <winsock2.h>
#include <windows.h>

#pragma   comment   ( lib ,   "ws2_32.lib" )
int   main()
{
                SOCKET s;
                sockaddr_in addrto;
                WSADATA wsdata;
                BOOL bsocket;
                   char   *smsg= "this is a test wyj"   ;
                   //启?动??SOCKET库a,??版??本??为a2.0
                WSAStartup(0x0202,&wsdata);
                
                s=WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0,WSA_FLAG_OVERLAPPED);
                bsocket=TRUE;
                   //然??后??赋3值??给?地??址??,??用??来???从???网??络?上??的??广?播??地??址??接??收??消?息??;??
                addrto.sin_family=AF_INET;
    addrto.sin_addr.s_addr=INADDR_BROADCAST;
                addrto.sin_port=htons(5050);
                   bool   opt= true   ;
                   //设???置?该?套???接??字??为a广?播??类???型??,??
                setsockopt(s,SOL_SOCKET,SO_BROADCAST,(   char   FAR *)&opt, sizeof   (opt));
                   int   nlen= sizeof   (addrto);
                   while (1)
                {
                                Sleep(1000);
                                   //从???广?播??地??址??发???送??消?息??
                                   int   ret=sendto(s,smsg,strlen(smsg),0,(sockaddr*)&addrto,nlen);
                                   if (ret==SOCKET_ERROR)
                                                printf(   "Error:%s\n" , WSAGetLastError());
                                   else
                                {                              
                                                printf(   "ok" );          
                                }
                }

                   return   -1;
}

你可能感兴趣的:(一个VC简单的UDP广播示例)