11.附加:每每經過setsockopt()設置了緩衝區大小,但還不能知足數據的傳輸需求,
個人習慣是本身寫個處理網絡緩衝的類,動態分配內存; 通常的習慣是本身寫個處理網絡緩衝的類,動態分配內存;下面我將這個類寫出,但願對你們有所幫助:
//仿照String 改寫而成
//==============================================================================
// 二進制數據,主要用於收發網絡緩衝區的數據
// CNetIOBuffer 以 MFC 類 CString 的源代碼做爲藍本改寫而成,用法與 CString 相似,
// 可是 CNetIOBuffer 中存放的是純粹的二進制數據,'\0' 並不做爲它的結束標誌。
// 其數據長度能夠經過 GetLength() 得到,緩衝區地址能夠經過運算符 LPBYTE 得到。
//==============================================================================
// Copyright (c) All-Vision Corporation. All rights reserved.
// Module: NetObject
// File: SimpleIOBuffer.h
// Author: gdy119
// Email :
8751webmaster@126.com
// Date: 2004.11.26
//==============================================================================
// NetIOBuffer.h
#ifndef _NETIOBUFFER_H
#define _NETIOBUFFER_H
//=============================================================================
#define MAX_BUFFER_LENGTH 1024*1024
//=============================================================================
//主要用來處理網絡緩衝的數據
class CNetIOBuffer
{
protected:
LPBYTE m_pbinData;
int m_nLength;
int m_nTotalLength;
CRITICAL_SECTION m_cs;
void Initvalibers();
public:
CNetIOBuffer();
CNetIOBuffer(const LPBYTE lbbyte, int nLength);
CNetIOBuffer(const CNetIOBuffer&binarySrc);
virtual ~CNetIOBuffer();
//=============================================================================
BOOL CopyData(const LPBYTE lbbyte, int nLe
---------------------------------------------------------------
其實我以爲第5條很應該值得注意
int nZero=0;
setsockopt(socket,SOL_S0CKET,SO_SNDBUF,(char *)&nZero,sizeof(nZero));
記得之前有些朋友討論過,socket雖然send成功了,可是其實只是發送到數據緩衝區裏面了,而並無真正的在物理設備上發送出去;而經過這條語句,將發送緩衝區設置爲0,即屏蔽掉髮送緩衝之後,一旦send返回(固然是就阻塞套結字來講),就能夠確定數據已經在發送的途中了^_^,可是這樣作也許會影響系統的性能
---------------------------------------------------------------
setoptsock()這個函數 設置成端口複用的時候,很容易對一些沒有進行單獨bind模式的程序形成危害。
好比old的 ping icmp door,簡單的sniffer後,收到包,而後設置setoptsock bind web服務,而後創建個cmd進程 bind再80端口。
Example Code
The following example demonstrates the
setsockopt function.
#include <stdio.h>
#include "winsock2.h"
void main() {
//---------------------------------------
// Declare variables
WSADATA wsaData;
SOCKET ListenSocket;
sockaddr_in service;
//---------------------------------------
// Initialize Winsock
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if( iResult != NO_ERROR )
printf("Error at WSAStartup\n");
//---------------------------------------
// Create a listening socket
ListenSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if (ListenSocket == INVALID_SOCKET) {
printf("Error at socket()\n");
WSACleanup();
return;
}
//---------------------------------------
// Bind the socket to the local IP address
// and port 27015
h
ostent* thisH
ost;
char* ip;
u_short port;
port = 27015;
thisH
ost = geth
ostbyname("");
ip = inet_ntoa (*(struct in_addr *)*thisH
ost->h_addr_list);
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(ip);
service.sin_port = htons(port);
if ( bind( ListenSocket,(SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
printf("bind failed\n");
cl
osesocket(ListenSocket);
return;
}
//---------------------------------------
// Initialize variables and call
setsockopt.
// The SO_KEEPALIVE parameter is a socket option
// that makes the socket send keepalive messages
// on the session. The SO_KEEPALIVE socket option
// requires a boolean value to be passed to the
//
setsockopt function. If TRUE, the socket is
// configured to send keepalive messages, if FALSE
// the socket configured to NOT send keepalive messages.
// This section of code tests the
setsockopt function
// by checking the status of SO_KEEPALIVE on the socket
// using the getsockopt function.
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
int iOptVal;
int iOptLen = sizeof(int);
if (getsockopt(ListenSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR) {
printf("SO_KEEPALIVE Value: %ld\n", iOptVal);
}
if (
setsockopt(ListenSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&bOptVal, bOptLen) != SOCKET_ERROR) {
printf("Set SO_KEEPALIVE: ON\n");
}
if (getsockopt(ListenSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR) {
printf("SO_KEEPALIVE Value: %ld\n", iOptVal);
}
WSACleanup();
return;
}
Notes for IrDA Sockets
Keep in mind the following:
The Af_irda.h header file must be explicitly included.
IrDA provides the following settable socket option:
Value Type Meaning
IRLMP_IAS_SET *IAS_SET Sets IAS attributes
The IRLMP_IAS_SET socket option enables the application to set a single attribute of a single class in the local IAS. The application specifies the class to set, the attribute, and attribute type. The application is expected to allocate a buffer of the necessary size for the passed parameters.
IrDA provides an IAS
Database that stores IrDA-based information. Limited
access to the IAS
Database is available through the
windowsSockets 2 interface, but such
access is not normally used by applications, and exists primarily to support connections to non-
windows devices that are not compliant with the
windows Sockets 2 IrDA conventions.
The following structure, IAS_SET, is used with the IRLMP_IAS_SET
setsockopt option to manage the local IAS
Database:
typedef struct _IAS_SET {
char irdaClassName[IAS_MAX_CLASSNAME];
char irdaAttribName[IAS_MAX_ATTRIBNAME];
u_long irdaAttribType;
union
{
LONG irdaAttribInt;
struct
{
u_short Len;
u_char OctetSeq[IAS_MAX_OCTET_STRING];
} irdaAttribOctetSeq;
struct
{
u_char Len;
u_char CharSet;
u_char UsrStr[IAS_MAX_USER_STRING];
} irdaAttribUsrStr;
} irdaAttribute;
} IAS_SET, *PIAS_SET, FAR *LPIAS_SET;
The following structure, IAS_QUERY, is used with the IRLMP_IAS_QUERY
setsockopt option to query a peer's IAS
Database:
typedef struct _
windows_IAS_QUERY {
u_char irdaDeviceID[4];
char irdaClassName[IAS_MAX_CLASSNAME];
char irdaAttribName[IAS_MAX_ATTRIBNAME];
u_long irdaAttribType;
union
{
LONG irdaAttribInt;
struct
{
u_long Len;
u_char OctetSeq[IAS_MAX_OCTET_STRING];
} irdaAttribOctetSeq;
struct
{
u_long Len;
u_long CharSet;
u_char UsrStr[IAS_MAX_USER_STRING];
} irdaAttribUsrStr;
} irdaAttribute;
} IAS_QUERY, *PIAS_QUERY, FAR *LPIAS_QUERY;
Many SO_ level socket options are not meaningful to IrDA. Only SO_LINGER is specifically supported.
Note
setsockopt must be called before bind on
windows NT 4.0,
windows 95, and
windows 98 platforms.
Requirements
Client Requires
windows XP,
windows 2000 Professional,
windows NT Workstation,
windows Me,
windows 98, or
windows 95.
Server Requires
windows Server 2003,
windows 2000 Server, or
windows NT Server.
Header Declared in Winsock2.h.
Library Link to Ws2_32.lib.
DLL Requires Ws2_32.dll.