Esempio n. 1
0
SOCKET passiveUDP(const char *service)																//建立被动UDP套接字
{
	return passiveSock(service, "udp", 0);
}
Esempio n. 2
0
// Main function
int main(int argc,char* argv[])
{
    int iListenFd, iClientFd;        
	int iPort;
	status_t status;

	pthread_t recvThreadId;
	pthread_t sendThreadId;
	pthread_t commandThreadId;
	pthread_t ipcHandlerId;
/*
	pthread_t posionerThId;
	pthread_t trackerThId;
	pthread_t gpsThId;
	pthread_t modemThId;
*/
	QSOCKINFO *SqsockInfo;
    // Server socket addr
    struct sockaddr_in SsockaddrServer;        
    // Client socket addr
	struct sockaddr_in SsockaddrClient;        
    socklen_t socklenSize;

	if(argc != 2)
        errexit("Usage : %s [PORT]:\n", argv[0]);

	iPort = atoi(argv[1]);
	if(iPort<0 || iPort >65536)
        errexit("PORT RANGE (1 ~ 65535)\n");
system("echo 0 > /tmp/trackerSw");
    socklenSize = sizeof(struct sockaddr_in);

    printLog("[SERVER] creating passive socket..\n");
    iListenFd = passiveSock(&SsockaddrServer, iPort);
    printLog("[SERVER] passive socket %d created.\n", iListenFd);

    while(1)
    {
        // connect to first client, create temporary socket 
        printLog("[SERVER] waiting for client..\n");
        if ((iClientFd = accept(iListenFd, (struct sockaddr*)&SsockaddrClient, &socklenSize)) < 0)
            errexit("accept failed: %s\n", strerror(errno));
        
		printLog("[SERVER] servant socket %d created.\n", iClientFd);

		// Initial client information struct
		SqsockInfo = (QSOCKINFO*)malloc(sizeof(QSOCKINFO));
		SqsockInfo->iSock = iClientFd;
		fprintf(stderr,"### %s():%d ###\n",__func__,__LINE__);
		SqsockInfo->writeQueue=NULL;
		SqsockInfo->readQueue=NULL;

		//소켓정보와 큐 정보생성 및 관리 변수에 할당
  		SqsockInfo->writeQueue = qCreate(&status, "WtcpIp", 0xFF);
  		if (SqsockInfo->writeQueue == NULL){
			close(iClientFd);
    		close(iListenFd);
        	errexit("create TCP IP Write Queue fail.\n");
		}
	
		SqsockInfo->readQueue = qCreate(&status, "RtcpIp", 0xFF);
  		if (SqsockInfo->readQueue == NULL){
			close(iClientFd);
    		close(iListenFd);
        	errexit("create TCP IP Read Queue fail.\n");
		}

		//NOTE: threads sharing the memory, client variable cannot reuse
        memcpy(&SqsockInfo->SsockaddrClient, &SsockaddrClient, sizeof(SsockaddrClient));  

		// commandGroup 쓰레드 생성
		if(pthread_create(&recvThreadId, NULL, recvCommand, (void*)SqsockInfo))
        	errexit("create recv thread failed: %s\n", strerror(errno));
		//쓰레드와 메인 분리
		pthread_detach(recvThreadId);

		// commandGroup 쓰레드 생성
		if(pthread_create(&commandThreadId, NULL, commandGroup, (void*)SqsockInfo))
        	errexit("create command thread failed: %s\n", strerror(errno));
		//쓰레드와 메인 분리
		pthread_detach(commandThreadId);

		// commandGroup 쓰레드 생성
		if(pthread_create(&sendThreadId, NULL, sendCommandResult, (void*)SqsockInfo))
        	errexit("create send thread failed: %s\n", strerror(errno));
		//쓰레드와 메인 분리
		pthread_detach(sendThreadId);

		// Posioner 쓰레드 생성
		if(pthread_create(&ipcHandlerId, NULL, ipcHandlerFunction, (void*)SqsockInfo))
        	errexit("create send thread failed: %s\n", strerror(errno));
		//쓰레드와 메인 분리
		pthread_detach(ipcHandlerId);
    }
    close(iListenFd);
    return 0;
}
Esempio n. 3
0
SOCKET passiveTCP(const char *service ,int qlen)														//建立被动TCP套接字并监听
{
	return passiveSock(service,"tcp",qlen);
}
int passiveTCP(const char *service) {
	return passiveSock(service, "tcp", 0);
}