コード例 #1
0
/* *************************************** 
*  recv msg from local host
*  use the msg to control dial routine
* ****************************************/
void *thread_msg_receiver(void *args)
{
	int listenfd, nrecv;
	uint8_t *recv_buf;

	struct sockaddr_in servaddr;
	struct sockaddr_in clntaddr;
	struct ctrl_msg *pmsg;
	socklen_t addrlen = sizeof(clntaddr);


	recv_buf = malloc(ETH_DATA_LEN);
	if( recv_buf == NULL)
	{
		printf("Malloc for the recv_buf failed, in thread_msg_receiver function\n");
		exit(-1);
	}

	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(LISTEN_PORT);
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

	listenfd = socket(AF_INET, SOCK_DGRAM, 0);
	if( -1 == listenfd)
	{
		perror("Create listen socket failed");
		exit(-1);
	}

	if( 0 != bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)))
	{
		perror("Bind listenfd failed");
		exit(-1);
	}
	

	while(1)
	{
		memset(recv_buf, 0, ETH_DATA_LEN);
		nrecv = recvfrom(listenfd, recv_buf, ETH_DATA_LEN, 0,
				(struct sockaddr*) &clntaddr, &addrlen);
		if( -1 == nrecv && errno == EINTR)
			continue;
		if( ((struct ctrl_msg *) recv_buf)->msg_magic == MAGIC)
		{
			pmsg = (struct ctrl_msg *) recv_buf;
		       	if( 0 == strncmp(pmsg->msg, "off", strlen(pmsg->msg) ) )
		       	{
			       printf("Logoff!\n");
			       logoff();
			       status = OFFLINE;
		       }
		       if(0 == strncmp(pmsg->msg, "on", strlen(pmsg->msg) ))
		       {
			       printf("Logon!\n");
			       logon();
		       }
		       if(0 == strncmp(pmsg->msg, "over", strlen(pmsg->msg) ))
		       {
			      printf("Exiting!\n");
			      if( status == ONLINE )
			      	      logoff();
			      exit(0);
		       }
		}
	}
}
コード例 #2
0
ファイル: main.c プロジェクト: marktrue/eapolclient-marktrue
static void onExit(int sig)
{
    printf("Bye Bye Honey~(*^_^*)\n");
    logoff();
    exit(0);
}