这两天又看了几眼Socket编程,我老是没长性,总是东看看西看看。还老爱挑毛病,钻牛角尖,真是不可救药了。这不,又开始看Socket不顺眼了。当时是看了一个朋友给我的一段socket raw编程的代码,有一段看的我很恶心。如下:
if(( ErrorCode = WSAStartup ( MAKEWORD ( 2 , 1 ),& wsaData ))!= 0 ){
printf ( "WSAStartup failed: %d/n" , ErrorCode );
return 2 ;
}
sockMain = WSASocket ( AF_INET , SOCK_RAW , IPPROTO_RAW , NULL , 0 , WSA_FLAG_OVERLAPPED );
if( sockMain == INVALID_SOCKET )
{
printf ( "Socket failed: %d/n" , WSAGetLastError ());
return 3 ;
}
ErrorCode = setsockopt ( sockMain , IPPROTO_IP , IP_HDRINCL ,( char *)& flag , sizeof (int));
if( ErrorCode == SOCKET_ERROR )
{
printf ( "Set sockopt failed: %d/n" , WSAGetLastError ());
return 4 ;
}
ErrorCode = setsockopt ( sockMain , SOL_SOCKET , SO_SNDTIMEO ,( char *)& TimeOut , sizeof ( TimeOut ));
if( ErrorCode == SOCKET_ERROR )
{
printf ( "Set sockopt time out failed: %d/n" , WSAGetLastError ());
return 5 ;
}
也 许很多人都觉得我神经,这个不是每个socket程序的必经之路么?要是觉得这个很难受破坏结构,那还怎么写大型的socket程序?也许吧,我说了我就 是一个死脑筋,总喜欢看起来好一些东西。总觉得那种每一个函数调用后,就跟着一堆针对返回值得
if(( ErrorCode = WSAStartup ( MAKEWORD ( 2 , 1 ),& wsaData ))!= 0 ){
printf ( "WSAStartup failed: %d/n" , ErrorCode );
return 2 ;
}
sockMain = WSASocket ( AF_INET , SOCK_RAW , IPPROTO_RAW , NULL , 0 , WSA_FLAG_OVERLAPPED );
if( sockMain == INVALID_SOCKET )
{
printf ( "Socket failed: %d/n" , WSAGetLastError ());
return 3 ;
}
ErrorCode = setsockopt ( sockMain , IPPROTO_IP , IP_HDRINCL ,( char *)& flag , sizeof (int));
if( ErrorCode == SOCKET_ERROR )
{
printf ( "Set sockopt failed: %d/n" , WSAGetLastError ());
return 4 ;
}
ErrorCode = setsockopt ( sockMain , SOL_SOCKET , SO_SNDTIMEO ,( char *)& TimeOut , sizeof ( TimeOut ));
if( ErrorCode == SOCKET_ERROR )
{
printf ( "Set sockopt time out failed: %d/n" , WSAGetLastError ());
return 5 ;
}
也 许很多人都觉得我神经,这个不是每个socket程序的必经之路么?要是觉得这个很难受破坏结构,那还怎么写大型的socket程序?也许吧,我说了我就 是一个死脑筋,总喜欢看起来好一些东西。总觉得那种每一个函数调用后,就跟着一堆针对返回值得