Example #1
0
main ()
{
SOCKET LSock, CSock;
unsigned short usPort=80;
int Rc;
char szVer [128];

  Tcp4uVer (szVer, sizeof szVer);
  printf ("Using %s\n", szVer);

  /* --- prepare a listening connection */
  Tcp4uInit ();
  Rc = TcpGetListenSocket (& LSock, "http", & usPort, 1);
  if (Rc!=TCP4U_SUCCESS)
  {
     printf ("%s\n", Tcp4uErrorString(Rc));
     return 0;
  }
  /* --- now listen at port */
  Rc = TcpAccept (&CSock, LSock, 0);
  while (Rc>=TCP4U_SUCCESS)
  {
    /* now the application code. Note that you can fork or creat */
    /* a new thread in order to create a multi-user server       */
    /* --- in this app, we listen at the opened socket */
    Rc = TnReadLine (CSock, szBuf, sizeof szBuf, 60, HFILE_ERROR);
    if (Rc>=TCP4U_SUCCESS)  printf ("%s\n", szBuf);
  }
  TcpClose (& CSock);
  Tcp4uCleanup ();
return 0;
}  /* main */   
bool TestTcpSendBlocking( )
{
	Socket hListen = TcpListen( LISTEN_PORT, 255, "127.0.0.1" );
	if( hListen == INVALID_SOCKET )
	{
		CLog::Print( LOG_ERROR, "TcpListen() error" );
		return false;
	}

	CTcpClientThreadArg * pclsArg = new CTcpClientThreadArg();
	pclsArg->m_bRun = true;

	StartThread( "TcpClientThread", TcpClientThread, pclsArg );

	char szIp[52];
	int iPort;

	Socket hConn = TcpAccept( hListen, szIp, sizeof(szIp), &iPort );

	while( pclsArg->m_bRun )
	{
		sleep(1);
	}

	closesocket( hConn );

	delete pclsArg;

	return true;
}
Example #3
0
/**
 * @ingroup ServerPlatform
 * @brief SipLoadBalancer 관리를 위한 네트워크 서버 쓰레드 함수
 * @param lpParameter 의미없음
 * @returns 0 을 리턴한다.
 */
THREAD_API ServerThread( LPVOID lpParameter )
{
	IMonitorCallBack * pclsCallBack = (IMonitorCallBack *)lpParameter;
	Socket hMonitorSocket;
	pollfd sttPoll[1];
	char	szIp[INET6_ADDRSTRLEN];
	int		iPort, n;
	std::string	strIp;

	hMonitorSocket = TcpListen( pclsCallBack->m_iMonitorPort, 255, NULL, pclsCallBack->m_bIpv6 );
	if( hMonitorSocket == INVALID_SOCKET )
	{
		CLog::Print( LOG_ERROR, "Monitor Tcp Server Port(%d) open error(%d)", pclsCallBack->m_iMonitorPort, GetError() );
	}
	else
	{
		sttPoll[0].fd = hMonitorSocket;
		sttPoll[0].events = POLLIN;
		sttPoll[0].revents = 0;
	}

	CLog::Print( LOG_INFO, "MonitorServerThread is started" );

	while( gbStop == false )
	{
		n = poll( sttPoll, 1, 1000 );
		if( n <= 0 )
		{
			continue;
		}

		Socket hClientSocket = TcpAccept( hMonitorSocket, szIp, sizeof(szIp), &iPort );
		if( hClientSocket == INVALID_SOCKET )
		{

		}
		else if( pclsCallBack->IsMonitorIp( szIp ) )
		{
			StartMonitorThread( hClientSocket, szIp, iPort, pclsCallBack );
		}
		else
		{
			if( strcmp( strIp.c_str(), szIp ) )
			{
				CLog::Print( LOG_ERROR, "monitor client(%s:%d) is disconnected because it's ip is not monitor ip", szIp, iPort );
				strIp = szIp;
			}

			closesocket( hClientSocket );
		}
	}

	closesocket( hMonitorSocket );

	CLog::Print( LOG_INFO, "MonitorServerThread is terminated" );

	return 0;
}
/**
 * @ingroup SnmpStack
 * @brief SNMP 클라이언트의 TCP 연결 처리 쓰레드
 * @param lpParameter CSnmpTcpAgent 객체
 * @returns 0 을 리턴한다.
 */
THREAD_API SnmpTcpAgentListenThread( LPVOID lpParameter )
{
	CSnmpTcpAgent * pclsAgent = (CSnmpTcpAgent *)lpParameter;
	pollfd sttPoll[1];
	char	szIp[52];
	int		iPort, n;

	TcpSetPollIn( sttPoll[0], pclsAgent->m_hSocket );

	CLog::Print( LOG_INFO, "SnmpTcpAgentListenThread is started" );

	while( pclsAgent->m_bStop == false )
	{
		n = poll( sttPoll, 1, 1000 );
		if( n <= 0 ) continue;
		if( pclsAgent->m_bStop ) break;

		Socket hSocket = TcpAccept( pclsAgent->m_hSocket, szIp, sizeof(szIp), &iPort );
		if( hSocket != INVALID_SOCKET )
		{
			CSnmpTcpAgentClientArg * pclsArg = new CSnmpTcpAgentClientArg();
			if( pclsArg == NULL )
			{
				CLog::Print( LOG_ERROR, "%s new error(%d)", __FUNCTION__, GetError() );
				closesocket( hSocket );
			}
			else
			{
				pclsArg->m_pclsAgent = pclsAgent;
				pclsArg->m_hSocket = hSocket;
				pclsArg->m_strIp = szIp;
				pclsArg->m_iPort = iPort;

				if( StartThread( "SnmpTcpAgentClientThread", SnmpTcpAgentClientThread, pclsArg ) == false )
				{
					closesocket( hSocket );
					delete pclsArg;

					CLog::Print( LOG_ERROR, "%s SnmpTcpAgentClientThread thread start error(%d)", __FUNCTION__, GetError() );
				}
			}
		}
	}

	closesocket( pclsAgent->m_hSocket );

	CLog::Print( LOG_INFO, "SnmpTcpAgentListenThread is terminated" );

	return 0;
}
Example #5
0
int main()
{
  int sockfd, rc;
  int port = 9997;
  sockfd = TcpListenX("127.0.0.1", port);
  printf("sockfd:%d\n", sockfd);
  int sport;
  char buf[1025]; 
  char sip[100]; 
  while(1) {
    rc = TcpAccept(sockfd, sip, &sport);
    printf("rc : %d\n", rc);
    printf("sip:%s, sport:%d\n", sip, sport);
    int sx = 0;
    rc = TcpRecv(rc, buf, 1024, sx);
  } 
  return 0;
}
Example #6
0
void *tcp_rcvTask(void *argc)
{
    int  ret, i;
    int  numEvents;
    unsigned char buf[MAX_DATA_LENGTH*1000]; /* NEXT_CHECK by icecom 110823 */

    ThreadAlive[PTASK0] = TRUE;

    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    pthread_cleanup_push(ThreadCleanUp, (void *)NULL);

    memset(buf, 0x0, sizeof(buf));

    FD_ZERO(&allset);

    tlink.ListenFD = TcpOpen(&tlink);

    if (tlink.ListenFD <= 0) {
        if (tlink.mode == LINK_MODE_SERVER) {
            xprint(FMS_HST | FMS_ERROR, "%s : S-TCP TcpOpen failed\n", __func__);
        } else if (tlink.mode == LINK_MODE_CLIENT) {
            xprint(FMS_HST | FMS_ERROR, "%s : client mode\n", __func__);
        } else {
            xprint(FMS_HST | FMS_ERROR, "%s : unknown socket mode\n", __func__);
        }
        /* NEXT_CHECK error processing, by icecom */
    } else {
        FD_SET(tlink.ListenFD, &allset);
        compareMaxfd(tlink.ListenFD);
        if (tlink.mode == LINK_MODE_CLIENT) {
        } else {
            TcpInfo();
        }
    }

    for (;;) {
        if (ThreadAlive[PTASK0] != TRUE) {
            break;
        }

        rset = allset;
        if (0 > (numEvents = recv_extendedPoll(200, &rset))) {
            xprint(FMS_HST | FMS_ERROR, "%s(%d) : recv_extendedPoll (reason=%s)\n",
                    __func__, __LINE__, strerror(errno));
            if (errno == EINTR) {
                continue;
            } else if (errno == EBADF || errno == EPIPE || errno == EIO) {
                xprint(FMS_HST | FMS_ERROR, "select at ProcessEvent(%s) EXIT !!\n", strerror(errno));
            } else {
                xprint(FMS_HST | FMS_ERROR, "select at ProcessEvent(%s) EXIT !!\n", strerror(errno));
            }
        } else if (numEvents == 0) {
            /* Time out 인 경우 정상처리 한다. */
            continue;
        } else {
            if (tlink.mode == LINK_MODE_SERVER) {
                if (tlink.ListenFD > 0) {
                    if (FD_ISSET(tlink.ListenFD, &rset)) {
                        xprint(FMS_HST | FMS_INFO1, "%s(%d) :: TcpAccept Start\n", __func__, __LINE__);

                        pthread_mutex_lock(&TcpMutex);
                        i = TcpAccept(tlink.ListenFD);
                        pthread_mutex_unlock(&TcpMutex);

                        if (i == MAX_LISTENQ) {
                            xprint(FMS_HST | FMS_ERROR, "%s(%d) :: TcpAccept error\n", __func__, __LINE__);
                            continue;
                        } else {
                            pthread_mutex_lock(&TcpMutex);
                            if (alist[i].AcceptFD > 0) {
                                FD_SET(alist[i].AcceptFD, &allset);
                                compareMaxfd(alist[i].AcceptFD);
#if 0
                                /* 20140723, by jeon */
                                TcpInfo();
#endif
                            } else {
                                xprint(FMS_HST | FMS_ERROR, "TcpAccept fail(fd=%d)\n", alist[i].AcceptFD);
                                pthread_mutex_unlock(&TcpMutex);
                                continue;
                            }
                            pthread_mutex_unlock(&TcpMutex);

                        }
                    }
                }

                pthread_mutex_lock(&TcpMutex);
                for (i = 0; i < MAX_LISTENQ; i++) {
                    if (alist[i].AcceptFD > 0) {
                        if (FD_ISSET(alist[i].AcceptFD, &rset)) {
                            if ((ret = TcpRecv(i, alist[i].AcceptFD, buf)) > 0) {

                                /* 20140723, by jeon */
                                if (hostConf.vmc_system[i].systemport == 0) {
                                    vm_proc_state_msg *hb  = (vm_proc_state_msg *)&buf[DEFAULT_TCP_COMHDR_LENGTH];

                                    alist[i].sysinfo.systemaddr = NBO_NTOHL(hb->vm_ipaddress);
                                    sprintf(alist[i].sysinfo.systemname, "VM-%s", addr2ntoa(alist[i].sysinfo.systemaddr));

                                    hostConf.vmc_system[i].systemaddr = alist[i].sysinfo.systemaddr;
                                    sprintf(hostConf.vmc_system[i].systemname, "%s", alist[i].sysinfo.systemname);

                                    hostConf.vmc_system[i].systemport = alist[i].sysinfo.port;

                                    TcpInfo();
                                }

                                TcpdSock_Event(i, buf);
                            }
                        }
                    }
                }
                pthread_mutex_unlock(&TcpMutex);
            } else if (tlink.mode == LINK_MODE_CLIENT) {
            }
        }
    }

    ThreadAlive[PTASK0] = FALSE;
    ThreadFlags[PTASK0] = FALSE;

    /* pthread mgnt */
    pthread_cleanup_pop(1);

    return(NULL);
}