SOCKET socket(int family,int type,int PRotocol)
SOCKRET s;
s=socket(AF_INET,SOCK_DGRAM,0);
或s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)
int bind(SOCKET s,strUCt sockaddr_in*name,int namelen)
//设置有关的全局变量
SOCKET sr,ss;
HPSTR sockBufferS,sockBufferR;
HANDLE hSendData,hReceiveData;
DWROD dwDataSize=1024*4;
struct sockaddr_in therel.there2;
#DEFINE LOCAL_HOST_ADDR 200.200.200.201
#DEFINE REMOTE_HOST-ADDR 200.200.200.202
#DEFINE LOCAL_HOST_PORT 2000
#DEFINE LOCAL_HOST_PORT 2001
//套接字建立函数
BOOL make_skt(HWND hwnd)
{
struct sockaddr_in here,here1;
ss=socket(AF_INET,SOCK_DGRAM,0);
sr=socket(AF_INET,SOCK_DGRAM,0);
if((ss==INVALID_SOCKET)(sr==INVALID_SOCKET))
{
MessageBox(hwnd,“套接字建立失败!”,“”,MB_OK);
return(FALSE);
}
here.sin_family=AF_INET;
here.sin_addr.s_addr=inet_addr(LOCAL_HOST_ADDR);
here.sin_port=htons(LICAL_HOST_PORT);
//another socket
herel.sin_family=AF_INET;
herel.sin_addr.s_addr(LOCAL_HOST_ADDR);
herel.sin_port=htons(LOCAL_HOST_PORT1);
SocketBuffer();//套接字缓冲区的锁定设置
setsockopt(ss,SOL_SOCKET,SO_SNDBUF,(char FAR*)sockBufferS,dwDataSize);
if(bind(ss,(LPSOCKADDR)&here,sizeof(here)))
{
MessageBox(hwnd,“发送套接字绑定失败!”,“”,MB_OK);
return(FALSE);
}
setsockopt(sr SQL_SOCKET,SO_RCVBUfso_REUSEADDR,(char FAR*)
sockBufferR,dwDataSize);
if(bind(sr,(LPSOCKADDR)&here1,sizeof(here1)))
{
MessageBox(hwnd,“接收套接字绑定失败!”,“”,MB_OK);
return(FALSE);
}
return(TRUE);
}
//套接字缓冲区设置
void sockBuffer(void)
{
hSendData=GlobalAlloc(GMEM_MOVEABLEGMEM_SHARE,dwDataSize);
if(!hSendData)
{
MessageBox(hwnd,“发送套接字缓冲区定位失败!”,NULL,
MB_OKMB_ICONEXCLAMATION);
return;
}
if((sockBufferS=GlobalLock(hSendData)==NULL)
{
MessageBox(hwnd,“发送套接字缓冲区锁定失败!”,NULL,
MB_OKMB_ICONEXCLAMATION);
GlobalFree(hRecordData[0];
return;
}
hReceiveData=globalAlloc(GMEM_MOVEABLEGMEM_SHARE,dwDataSize);
if(!hReceiveData)
{
MessageBox(hwnd,"“接收套接字缓冲区定位败!”,NULL
MB_OKMB_ICONEXCLAMATION);
return;
}
if((sockBufferT=Globallock(hReceiveData))=NULL)
MessageBox(hwnd,"发送套接字缓冲区锁定失败!”,NULL,
MB_OKMB_ICONEXCLAMATION);
GlobalFree(hRecordData[0]);
return;
}
{
int sendto(SOCKET s.char*buf,int len,int flags,struct sockaddr_in to,int
tolen);
int recvfrom(SOCKET s.char*buf,int len,int flags,struct sockaddr_in
fron,int*fromlen)
void CloseSocket(void)
{
GlobalUnlock(hSendData);
GlobalFree(hSenddata);
GlobalUnlock(hReceiveData);
GlobalFree(hReceiveDava);
if(WSAAysncSelect(ss,hwnd,0,0)=SOCKET_ERROR)
{
MessageBos(hwnd,“发送套接字关闭失败!”,“”,MB_OK);
return;
}
if(WSAAysncSelect(sr,hwnd,0,0)==SOCKET_ERROR)
{
MessageBox(hwnd,“接收套接字关闭失败!”,“”,MB_OK);
return;
}
WSACleanup();
closesockent(ss);
closesockent(sr);
return;
}
int ok=sizeof(SOCKADDR);
case wMsg;
switch(1Param)
{
case FD_READ:
//套接字上读数据
if(recvfrom(sr.lpPlayData[j],dwDataSize,0,(struct sockaddr FAR*)&there1,
(int FAR*)&ok)==SOCKET_ERROR0
{
MessageBox)hwnd,“数据接收失败!”,“”,MB_OK);
return(FALSE);
}
case FD_WRITE:
//套接字上写数据
}
break;
新闻热点
疑难解答