📄 main.c
字号:
/* * Embedded NNTP Client * * ./software/ch11/emnntp/main.c * * mtj@cogitollc.com * */#include <stdio.h>#include <string.h>#include <unistd.h>#include "emnntp.h"#define SERVER "192.168.2.151"#define GROUP "companyx.devicey.control"#define FEEDBACK_GROUP "companyx.devicey.feedback"#define MYNAME "Device_#1"#define MAX_MSG 4096#define MAX_ROWS 10#define MAX_STRING 80typedef struct { int msgId; char msgSender[MAX_STRING+1]; char msgSubject[MAX_STRING+1]; int msgSize; char msgDate[MAX_STRING+1];} msgType;msgType messages[MAX_ROWS];/* * Test main to use the emnntp API. */int main(){ int result, count, index = 0; int lastMessage = -1; char contents[MAX_MSG+1]; news_t news; /* Load our news body into the mail structure and clear it out. */ news.msg = contents; bzero( contents, sizeof(contents) ); news.msgLen = MAX_MSG; while (1) { /* Connect to the NNTP Server */ count = nntpConnect( SERVER ); if (count == 0) { /* Set our incoming message group */ count = nntpSetGroup( GROUP, lastMessage ); index = 0; while (count-- > 0) { result = nntpRetrieve( &news, MAX_MSG ); if (result > 0) { result = nntpParse ( &news, FULL_PARSE ); if (result == 0) { /*---------------------*/ /* Process the Message */ /*---------------------*/ /* Save some data about the processed message */ strncpy( messages[index].msgSubject, news.subject, MAX_STRING ); strncpy( messages[index].msgSender, news.sender, MAX_STRING ); strncpy( messages[index].msgDate, news.msgDate, MAX_STRING ); messages[index].msgId = news.msgId; messages[index].msgSize = strlen(news.bodyStart); index++; lastMessage = news.msgId; } } } /* Send back feedback */ result = nntpSetGroup( FEEDBACK_GROUP, -1 ); if (result >= 0) { strcpy(news.sender, MYNAME); strcpy(news.subject, "Messages Processed"); bzero(news.msg, MAX_MSG); sprintf( news.msg, "<HTML><BODY><H2>Messages Processed by %s</H2><BR>" "<CENTER>" "<TABLE BORDER><CAPTION>Message List</CAPTION>", MYNAME ); sprintf( &news.msg[strlen(news.msg)], "<TR><bold><TH>Msg ID</TH><TH>Sender</TH><TH>Subject</TH>" "<TH>Msg Size</TH><TH>Transmitted</TH>" "<TH>Status</TH></bold></TR>"); for ( count = 0 ; count < index ; count++ ) { sprintf( &news.msg[strlen(news.msg)], "<TR><TD><CENTER>%d</CENTER></TD><TD>%s</TD><TD>%s</TD>" "<TD><CENTER>%d</CENTER></TD><TD>%s</TD><TD>%s</TD></TR>", messages[count].msgId, messages[count].msgSender, messages[count].msgSubject, messages[count].msgSize, messages[count].msgDate, "Processed" ); } sprintf( &news.msg[strlen(news.msg)], "</TABLE></CENTER></BODY></HTML>" ); news.bodyStart = news.msg; result = nntpPost( &news ); } } nntpDisconnect(); sleep(60); } return 0;}/* * Copyright (c) 2002 Charles River Media. All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, is hereby granted without fee provided * that the following conditions are met: * * 1. Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * 3. Neither the name of Charles River Media nor the names of * its contributors may be used to endorse or promote products * derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY CHARLES RIVER MEDIA AND CONTRIBUTERS * 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CHARLES * RIVER MEDIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARAY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -