BSD套接字(BSD Sockets)
BSD Sockets 使用的最广泛的网络程序编程方法,主要用于应用程序的编写,用于网络上主机与主机之间的相互通信.UNIX,Linux,VxWorks均支持BSD Sockets,Windows的Winsock基本上是来自BSD SocketsSocket分为Stream Sockets和Data Sockets.Stream Sockets是可靠性的双向数据传输用TCP
BSD Sockets 使用的最广泛的网络程序编程方法,主要用于应用程序的编写,用于网络上主机与主机之间的相互通信.
UNIX,Linux,VxWorks均支持BSD Sockets,Windows的Winsock基本上是来自BSD Sockets
Socket分为Stream Sockets和Data Sockets.
Stream Sockets是可靠性的双向数据传输用TCP,如HTTP,FTP等
Data Sockets是不可靠连接用UDP,如多点广播,DHCP,SNMP,RIP等.
- TCP服务器端和一个TCP客户端通信的程序过程
| | 客户机 |
| 两个浅蓝色的函数是两个Sockets的握手函数,即在这里建立连接 |
|
- UDP服务器端和一个UDP客户端通信的程序过程
| | 客户机 |
|
这里不建立连接,只是数据收发 <----------
|
|
UDP广播包程序示例
发送UDP广播包,查找本地网络中所有运行SNMP服务设备的IP信息。
winsock, win32控制台程序。
#define SNMP_PORT 161
void SearchIP_In_LAN()
{
WSADATA wsaData;
SOCKET sockListener;
SOCKADDR_IN saUdpCli,saUdpServ;
BOOL fBroadcast = TRUE;
char sendBuff[40]= //SNMP查询包内容
{
'\x30','\x26','\x02','\x01','\x00',
'\x04','\x06','\x70','\x75','\x62',
'\x6c','\x69','\x63','\xa0','\x19',
'\x02','\x01','\x03','\x02','\x01',
'\x00','\x02','\x01','\x00','\x30',
'\x0e','\x30','\x0c','\x06','\x08',
'\x2b','\x06','\x01','\x02','\x01',
'\x01','\x01','\x00','\x05','\x00'
};
char recvBuff[600];
int nSize,err,timeoutnum = 1;;
ULONG HostIpAddr;
int rv;
int ncount=0;
if(WSAStartup(MAKEWORD( 2, 2),&wsaData)!=0)
{
printf("Winsocket start error!\n");
return;
}
sockListener=socket(AF_INET,SOCK_DGRAM,0);
err = setsockopt ( sockListener,
SOL_SOCKET,
SO_BROADCAST,
(CHAR *)&fBroadcast,
sizeof ( BOOL ));
if ( SOCKET_ERROR == err ) return ;
int TimeOut=10000;
err = setsockopt ( sockListener,
SOL_SOCKET,
SO_RCVTIMEO,
(CHAR *) &TimeOut,
sizeof (TimeOut)
);
if ( SOCKET_ERROR == err ) return ;
saUdpCli.sin_family = AF_INET;
saUdpCli.sin_port = htons(0);
saUdpCli.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind( sockListener, (SOCKADDR *)&saUdpCli, sizeof(SOCKADDR_IN))!=0)
{
printf("Can't bind socket to local port!Program stop.\n");
return;
}
err = gethostname(recvBuff,sizeof(recvBuff));
if ( SOCKET_ERROR == err )
return ;
struct hostent *hp = gethostbyname(recvBuff);
if (!hp) return ;
saUdpServ.sin_family = AF_INET;
memcpy(&(saUdpServ.sin_addr),hp->h_addr,hp->h_length);
HostIpAddr=ntohl(saUdpServ.sin_addr.s_addr);
saUdpServ.sin_addr.s_addr = htonl ( INADDR_BROADCAST );
saUdpServ.sin_port = htons (SNMP_PORT);
nSize = sizeof ( SOCKADDR_IN );
if((rv = sendto ( sockListener,
sendBuff,
sizeof(sendBuff),0,
(SOCKADDR *) &saUdpServ,
sizeof ( SOCKADDR_IN )))==SOCKET_ERROR)
{
printf("Send error!\n");
closesocket(*((SOCKET*)sockListener));
return;
}
printf("Send successful!\n");
do // waiting to receive
{
if((rv=recvfrom ( sockListener,
recvBuff,sizeof(recvBuff),0,
(SOCKADDR *) &saUdpCli,&nSize))==SOCKET_ERROR)
{
if(WSAGetLastError()==WSAETIMEDOUT)
{
if (timeoutnum > 6 ) //Wait for 1 minute.
break;
else
timeoutnum++;
}
}
else
{
CString Ip(inet_ntoa ( saUdpCli.sin_addr ));
printf("\t IP Adress-> %s ",Ip);
}
}while(TRUE);
closesocket(*((SOCKET*)sockListener));
return ;
}
网络软件设计框图(个人理解,仅供参考)
- 设备管理服务软件架构
|
- 信息家电(IA)的架构
|
- 无线移动通信的架构
|
更多推荐
所有评论(0)