Beispiel #1
0
/**
 *  erstellt und initialisiert das Socket.
 *
 *  @return @p true bei Erfolg, @p false bei Fehler
 *
 *  @author FloSoft
 */
bool Socket::Create(int family, bool asUDPBroadcast)
{
    // socket ggf. schließen
    Close();

    socket_ = asUDPBroadcast ? socket(family, SOCK_DGRAM, IPPROTO_UDP) : socket(family, SOCK_STREAM, 0);

    // Ist es gültig?
    status_ = (INVALID_SOCKET == socket_ ? INVALID : VALID);

    if (asUDPBroadcast)
    {
        int enable = 1;
        SetSockOpt(SO_BROADCAST, &enable, sizeof(enable), SOL_SOCKET);
    }
    else
    {
        // Nagle deaktivieren
        int disable = 1;
        SetSockOpt(TCP_NODELAY, &disable, sizeof(int), IPPROTO_TCP);

        int enable = 1;
        SetSockOpt(SO_REUSEADDR, &enable, sizeof(int), SOL_SOCKET);
    }

    refCount_ = new int32_t(1);
    isBroadcast = asUDPBroadcast;

    return (status_ != INVALID);
}
Beispiel #2
0
/**
@fn void set_frame_head_and_tail(m_oCommFrameStruct& frame)
@detail 填写数据包头尾信息
@param[in]	frame	数据包头
@return void
@note
*/
BOOL line_socket::Create(UINT nSocketPort, int nSocketType,LPCTSTR lpszSocketAddress)
{
    BOOL b_ret = TRUE;

    if(lpszSocketAddress != NULL)	ip_.Format(__TEXT("%s"), lpszSocketAddress);
    if(nSocketPort != 0) port_ = nSocketPort;

    b_ret = CAsyncSocket::Create(0x0,	nSocketType, 	FD_READ|FD_WRITE|FD_CONNECT|FD_CLOSE,  NULL);

    //设置接受缓冲
    int iOptionValue = recv_buff_size_ != 0 ?recv_buff_size_ : CURRENT_RECVBUF_SIZE;
    int iOptionLen = sizeof(int);
    SetSockOpt(SO_RCVBUF, (void*)&iOptionValue, iOptionLen, SOL_SOCKET);

    //设置发送缓冲为0
    iOptionValue = 0;
    iOptionLen = sizeof(int);
    SetSockOpt(SO_SNDBUF, (void*)&iOptionValue, iOptionLen, SOL_SOCKET);

    if(b_ret) {
        recv_proc_thread_ = AfxBeginThread(recv_processing_thread_proc, this);
        check_timeout_thread_ = AfxBeginThread(check_timeout_thread_proc, this);
        send_proc_thread_ = AfxBeginThread(send_processing_thread_proc, this);
        if(recv_proc_thread_ == NULL || check_timeout_thread_ == NULL || send_proc_thread_ == NULL) {
            Close();
            b_ret = FALSE;
        }
    }

    return b_ret;
}
/**
* 设置缓冲区可以收发的帧数量
* @param int iFrameCount IP帧数量
* @return BOOL TRUE:成功;FALSE:失败
*/
BOOL CSocketTailTimeFrame::SetBufferSize(int iFrameCount)
{
	BOOL bReturn = false;

	int iOptionValue = iFrameCount * m_iFrameSize;
	int iOptionLen = sizeof(int);
	bReturn = SetSockOpt(SO_RCVBUF, (void*)&iOptionValue, iOptionLen, SOL_SOCKET);

	iOptionValue = iFrameCount * m_iFrameSize;
	iOptionLen = sizeof(int);
	bReturn = SetSockOpt(SO_SNDBUF, (void*)&iOptionValue, iOptionLen, SOL_SOCKET);
	return bReturn;
}
Beispiel #4
0
void
sys_telnet_init(void)
{
    signal(SIGINT, intr);
    signal(SIGQUIT, intr2);
    signal(SIGPIPE, deadpeer);
#ifdef	SIGWINCH
    signal(SIGWINCH, sendwin);
#endif
#ifdef	SIGTSTP
    signal(SIGTSTP, susp);
#endif
#ifdef	SIGINFO
    signal(SIGINFO, ayt);
#endif

    setconnmode(0);

    NetNonblockingIO(net, 1);


#if	defined(SO_OOBINLINE)
    if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1)
	perror("setsockopt (SO_OOBINLINE) (ignored)");
#endif	/* defined(SO_OOBINLINE) */
}
/**
* 初始化
* @param CString strIPForInstrument 信源IP地址
* @param UINT uiIPForInstrument 信源IP地址
* @param UINT uiPortForTailTime	信源端口号
* @param CString strIPLCI 信宿IP地址
* @param UINT uiPortLCI	信宿端口号
* @return BOOL TRUE:成功;FALSE:失败
*/
BOOL CSocketTailTimeFrame::OnInit(CString strIPForInstrument, UINT uiIPForInstrument, UINT uiPortForTailTime, CString strIPLCI, UINT uiPortLCI)
{
	BOOL bReturn = false;

	// LCI设备IP地址
	m_strIPLCI = strIPLCI;
	//  LCI设备IP地址端口号
	m_uiPortLCI = uiPortLCI;

	// 和现场仪器通讯的本机IP地址
	m_strIPForInstrument = strIPForInstrument;
	// 和现场仪器通讯的本机IP地址
	m_uiIPForInstrument = uiIPForInstrument;
	// 用于仪器尾包时刻查询的端口号
	m_uiPortForTailTime = uiPortForTailTime;
	// 生成网络端口
	bReturn = Create(m_uiPortForTailTime, SOCK_DGRAM, m_strIPForInstrument);
	Bind(m_uiPortForTailTime,m_strIPForInstrument);
	//设置广播模式
	int	iOptval, iOptlen;
	iOptlen = sizeof(int);
	iOptval = 1;
	SetSockOpt(SO_BROADCAST, &iOptval, iOptlen, SOL_SOCKET);

	return bReturn;
}
Beispiel #6
0
int ServerThread::InitServer(int max_connections)
// Initialize the server. Returns a non-zero value if any
// errors ocurr.
{
  if(InitSocketLibrary() == 0) {
    if(InitSocket(SOCK_STREAM, servercfg->port) < 0) return 1;
  }
  else {
    return 1;
  }

  int val = 1;
  if(SetSockOpt(gxsocket, SOL_SOCKET, SO_REUSEADDR,
		&val, sizeof(val)) < 0) {
    CheckSocketError((gxSocket *)this, "Error initializing server"); 
    Close();
    return 1;
  }

  // Bind the name to the socket
  if(Bind() < 0) {
    CheckSocketError((gxSocket *)this, "Error initializing server"); 
    Close();
    return 1;
  }
  
  // Listen for connections with a specified backlog
  if(Listen(max_connections) < 0) {
    CheckSocketError((gxSocket *)this, "Error initializing server");
    Close();
    return 1;
  }

  return 0;
}
Beispiel #7
0
int MySocket::SetSockOpt(int level, int optName, const void *optVal, 
			 unsigned optLen)
// Sets the current socket option for the specified option level or name.
// Returns -1 if an error occurs.
{
  return SetSockOpt(Mysocket, level, optName, optVal, optLen);
}
Beispiel #8
0
void
sys_telnet_init (void)
{
  (void) signal (SIGINT, intr);
  (void) signal (SIGQUIT, intr2);
  (void) signal (SIGPIPE, deadpeer);
#ifdef	SIGWINCH
  (void) signal (SIGWINCH, sendwin);
#endif
#ifdef	SIGTSTP
  (void) signal (SIGTSTP, susp);
#endif
#ifdef	SIGINFO
  (void) signal (SIGINFO, ayt);
#endif

  setconnmode (0);

  NetNonblockingIO (net, 1);

#if	defined(TN3270)
  if (noasynchnet == 0)
    {				/* DBX can't handle! */
      NetSigIO (net, 1);
      NetSetPgrp (net);
    }
#endif /* defined(TN3270) */

#if	defined(SO_OOBINLINE)
  if (SetSockOpt (net, SOL_SOCKET, SO_OOBINLINE, 1) == -1)
    {
      perror ("SetSockOpt");
    }
#endif /* defined(SO_OOBINLINE) */
}
Beispiel #9
0
bool ListenSocket::Listen(int backlog) {
    const auto sockId = CreateSockId(TRANSPORT_TCP);
    if (sockId == INVALID_SOCKID) {
        return false;
    }

    int optValue = 1;
    auto optLength = static_cast<SockLen_t>(sizeof(optValue));
    auto ret = SetSockOpt(sockId, SOL_SOCKET, SO_REUSEADDR, &optValue, optLength);
    if (ret == SOCKET_ERROR) {
        DestroySockId(sockId);
        return false;
    }

    auto sockAddr = m_localHost.SockAddr();
    auto sockAddrLen = m_localHost.SockAddrLen();
    ret = bind(sockId, sockAddr, sockAddrLen);
    if (ret == SOCKET_ERROR) {
        DestroySockId(sockId);
        return false;
    }

    ret = listen(sockId, backlog);
    if (ret == SOCKET_ERROR) {
        DestroySockId(sockId);
        return false;
    }

    AttachSockId(sockId);
    return true;
}
Beispiel #10
0
HRESULT HvSetSendTimeOut(const HV_SOCKET_HANDLE& hSocket, int iMS)
{
    struct timeval uTime;
    uTime.tv_sec = iMS / 1000;
    uTime.tv_usec = (iMS % 1000) * 1000;
    int iRet = SetSockOpt(hSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&uTime, sizeof(timeval));
    return (0 == iRet) ? S_OK : E_FAIL;
}
void DFClientSock::OnConnect(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	int iZero = 2048;
	SetSockOpt(SO_RCVBUF, &iZero, sizeof(iZero));

	((DFHideMainFrame*)(((CDFOpenMindApp*)AfxGetApp())->m_pMainWnd))->LogonProcess();
	CAsyncSocket::OnConnect(nErrorCode);
}
Beispiel #12
0
int DzSetSockOpt( int fd, int level, int name, const void* option, socklen_t len )
{
    DzHost* host = GetHost();
    assert( host );
    assert( fd >= 0 );
    assert( ( fd & HANDLE_HOST_ID_MASK ) == host->hostId );
    assert( !( level == SOL_SOCKET && name == SO_LINGER ) );

    return SetSockOpt( host, fd, level, name, option, len );
}
TCPServerSocket::TCPServerSocket(IPAddress &cLocalAddr, int iPortNo, int iBackLog)
        : TCPSocket((cLocalAddr.GetAddressFamily() == AF_INET6))
{
    const int   iReUseAddrFlag = 1;

    SetSockOpt(SOL_SOCKET, SO_REUSEADDR, &iReUseAddrFlag, sizeof(iReUseAddrFlag));
    Bind(cLocalAddr, iPortNo);
    SetLocal();
    Listen(iBackLog);
}
// 设置接收缓冲区大小
BOOL CSocketInterfaceDataRev::SetBufferSize(int iBufferSize)
{
	BOOL bReturn = false;

	int iOptionValue = iBufferSize;
	int iOptionLen = sizeof(int);
	bReturn = SetSockOpt(SO_RCVBUF, (void*)&iOptionValue, iOptionLen, SOL_SOCKET);

	return bReturn;
}
TCPServerSocket::TCPServerSocket(int iPortNo, int iBackLog, bool bUseIPv6)
        : TCPSocket(bUseIPv6)
{
    const int   iReUseAddrFlag = 1;

    SetSockOpt(SOL_SOCKET, SO_REUSEADDR, &iReUseAddrFlag, sizeof(iReUseAddrFlag));
    Bind(iPortNo);
    SetLocal();
    Listen(iBackLog);
}
Beispiel #16
0
void CGnuLocal::Init()
{
	if(!Create(UDP_PORT, SOCK_DGRAM))
	{
		return;
	}

	if(!SetSockOpt(SO_BROADCAST, &m_Broadcasted, sizeof(int)))
	{
		return;
	}
}
// 初始化
BOOL CSocketInterfaceDataRev::OnInit(CString strIPForInterface, UINT uiPortForDataReceive)
{
	BOOL bReturn = false;

	// 和内部通讯的本机IP地址
	m_strIPForInterface = strIPForInterface;
	// 用于命令帧的端口号
	m_uiPortForDataReceive = uiPortForDataReceive;
	// 生成网络端口
	bReturn = Create(m_uiPortForDataReceive, SOCK_DGRAM, m_strIPForInterface);

	//设置广播模式
	int	iOptval, iOptlen;
	iOptlen = sizeof(int);
	iOptval = 1;
	SetSockOpt(SO_BROADCAST, &iOptval, iOptlen, SOL_SOCKET);

	return bReturn;	
}
Beispiel #18
0
// CSvrSocket member functions
void CSvrSocket::OnConnect(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class
	CLoginResultMsg* pMsg = new CLoginResultMsg();
	pMsg->Header.type = ID_CUSTOM_SOCK_CONN_RESULT;
	pMsg->Error = nErrorCode;
	SendMsg(pMsg,"");

	if ( nErrorCode == 0)
	{
		BOOL bDontLinger = FALSE;
		BOOL bRet = SetSockOpt(SO_DONTLINGER, (const char*)&bDontLinger, sizeof(BOOL));
	}

	bConnected = nErrorCode == 0;
		
	
	CAsyncSocket::OnConnect(nErrorCode);
}
/**
* 初始化
* @param CString strIPForInstrument 信宿IP地址
* @param UINT uiPortForTailFrame	信宿端口号
* @param UINT uiPortForTailFrameSend	用于尾包转发的端口号
* @return BOOL TRUE:成功;FALSE:失败
*/
BOOL CSocketTailFrame::OnInit(CString strIPForInstrument, UINT uiPortForTailFrame, UINT uiPortForTailFrameSend)
{
	BOOL bReturn = false;

	// 和现场仪器通讯的本机IP地址
	m_strIPForInstrument = strIPForInstrument;
	// 用于尾包接收的端口号
	m_uiPortForTailFrame = uiPortForTailFrame;
	// 用于尾包转发的端口号
	m_uiPortForTailFrameSend = uiPortForTailFrameSend;
	// 生成网络端口
	bReturn = Create(m_uiPortForTailFrame, SOCK_DGRAM, m_strIPForInstrument);
	//设置广播模式
	int	iOptval, iOptlen;
	iOptlen = sizeof(int);
	iOptval = 1;
	SetSockOpt(SO_BROADCAST, &iOptval, iOptlen, SOL_SOCKET);
	Bind(m_uiPortForTailFrame,m_strIPForInstrument);

	return bReturn;
}
/**
* 初始化
* @param CString strIPForInterface 信源IP地址
* @param UINT uiPortForCommandSend 信源端口号
* @param UINT uiPortAimToLine 信宿端口号,测线
* @param UINT uiPortAimToOperation 信宿端口号,施工
* @return BOOL TRUE:成功;FALSE:失败
*/
BOOL CSocketInterfaceCmdSend::OnInit(CString strIPForInterface, UINT uiPortForCommandSend, UINT uiPortAimToLine, UINT uiPortAimToOperation)
{
	BOOL bReturn = false;

	// 和内部通讯的本机IP地址
	m_strIPForInterface = strIPForInterface;
	// 用于命令帧的端口号
	m_uiPortForCommandSend = uiPortForCommandSend;
	// 客户端目标IP端口号,测线
	m_uiPortAimToLine = uiPortAimToLine;
	// 客户端目标IP端口号,施工
	m_uiPortAimToOperation = uiPortAimToOperation;
	// 生成网络端口
	bReturn = Create(m_uiPortForCommandSend, SOCK_DGRAM, m_strIPForInterface);

	//设置广播模式
	int	iOptval, iOptlen;
	iOptlen = sizeof(int);
	iOptval = 1;
	SetSockOpt(SO_BROADCAST, &iOptval, iOptlen, SOL_SOCKET);

	return bReturn;	
}
BOOL CMulticastSocket::CreateReceivingSocket(LPCTSTR strGroupIP, UINT nGroupPort)
{
	/* Create socket for receiving packets from multicast group */
	if(!Create(nGroupPort, SOCK_DGRAM, FD_READ))
		return FALSE;

	BOOL bMultipleApps = TRUE;		/* allow reuse of local port if needed */
	SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET);

	/* Fill m_saHostGroup_in for sending datagrams */
	memset(&m_saHostGroup, 0, sizeof(m_saHostGroup));
	m_saHostGroup.sin_family = AF_INET;
	m_saHostGroup.sin_addr.s_addr = inet_addr(strGroupIP);
	m_saHostGroup.sin_port = htons((USHORT)nGroupPort);

	/* Join the multicast group */
	m_mrMReq.imr_multiaddr.s_addr = inet_addr(strGroupIP);	/* group addr */ 
	m_mrMReq.imr_interface.s_addr = htons(INADDR_ANY);		/* use default */ 
	if(setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&m_mrMReq, sizeof(m_mrMReq)) < 0)
		return FALSE;

	return TRUE;
}
Beispiel #22
0
// increases the send buffer to a bigger size
bool CEMSocket::UseBigSendBuffer()
{
#define BIGSIZE 128 * 1024
	if (m_bUsesBigSendBuffers)
		return true;
	m_bUsesBigSendBuffers = true;
    int val = BIGSIZE;
    int vallen = sizeof(int);
	int oldval = 0;
	GetSockOpt(SO_SNDBUF, &oldval, &vallen);
	if (val > oldval)
		SetSockOpt(SO_SNDBUF, &val, sizeof(int));
	val = 0;
	vallen = sizeof(int);
	GetSockOpt(SO_SNDBUF, &val, &vallen);
#if defined(_DEBUG) || defined(_BETA)
	if (val == BIGSIZE)
		theApp.QueueDebugLogLine(false, _T("Increased Sendbuffer for uploading socket from %uKB to %uKB"), oldval/1024, val/1024);
	else
		theApp.QueueDebugLogLine(false, _T("Failed to increase Sendbuffer for uploading socket, stays at %uKB"), oldval/1024);
#endif
	return val == BIGSIZE;
}
main ( ) 
{
	int sfd, cfd ;
	struct sockaddr_in serveraddr, clientaddr ;
	struct protoent *proto ;
	long int optval =  1;
	time_t ticks ;
	char buf[MAXLINE] ;
	u_int16_t port = 54321 ;
	socklen_t length ;
	
	proto = getprotobyname ( "tcp" ) ;
	sfd = Socket( AF_INET, SOCK_STREAM, proto->p_proto ) ;
	SetSockOpt ( sfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof ( optval ) ) ;

	bzero ( &serveraddr, sizeof ( serveraddr ) ) ;
	serveraddr.sin_family = AF_INET ;
	serveraddr.sin_addr.s_addr = htonl ( INADDR_ANY ) ;
	serveraddr.sin_port = htons ( port ) ;

	Bind ( sfd, ( struct sockaddr *) &serveraddr, sizeof ( serveraddr ) ) ;
	Listen ( sfd, LISTENQ ) ;
	
	length = sizeof ( clientaddr ) ;
	for ( ; ; )  { 
		cfd = Accept ( sfd, (struct sockaddr *) &clientaddr, &length ) ;
		printf ( "Get Client" ) ;
		ticks = time ( NULL ) ;
		snprintf ( buf, sizeof ( buf ), "<html><body bgcolor=gray><h3>%.24s<//h3><//body><//html>\r\n", \
				ctime(&ticks) ) ;
		//snprintf ( buf, sizeof ( buf ), "%.24s\r\n", ctime(&ticks) ) ;
		writen ( cfd, buf, strlen(buf) ) ;
		close ( cfd ) ;

		printf ( "Connection Close\n" ) ;
	}
}
CCRLSocket::CCRLSocket() {
	int buffer_size = 1024 * 1024 * 16;
	SetSockOpt(SO_RCVBUF, &buffer_size, sizeof(int));
}