客戶端程序新鮮出爐了........主要采用了OTIPIA提供的通信類完成的設(shè)計,我基本上沒有參與,幾個同學(xué)寫出來的,很不錯,鼓個掌。。!
Qtopia提供了四個與套接字相關(guān)的類,分別說明如下:
a)QServerSocket類。它是基于TCP的服務(wù)器類,可以讓它在指定端口上進行監(jiān)聽。它的API使用十分方便,調(diào)用構(gòu)造函數(shù),實現(xiàn)newConnection()成員函數(shù)來建立新連接即可。
b)QSocket類。它是具有緩沖的TCP連接類。
C)QSocketDevice類。它是獨立于平臺的低級別Socket—API類。
d)QSocketNotifier類。它是socket回調(diào)支持類,利用它可以在Qtopia中編寫異步socket通信程序。一旦打開一個非阻塞式socket(如TCP、UDP等)或其他操作系統(tǒng)支持的協(xié)議族,就可以創(chuàng)建一QSoeketNotifier對象來監(jiān)測套接字。當(dāng)發(fā)生套接字事件時,將QSocketNotifier發(fā)出的activated()信號與希望被調(diào)用的槽連接。
客戶端主要的代碼如下:
(1)窗體函數(shù)
#include "win.h"
#include <qvariant.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
char m_str[256];
/*
* Constructs a Form1 as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*
* The dialog will by default be modeless, unless you set 'modal' to
* TRUE to construct a modal dialog.
*/
Form1::Form1( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
if ( !name )
setName( "Form1" );
textLabel1 = new QLabel( this, "textLabel1" );
textLabel1->setGeometry( QRect( 60, 60, 380, 50 ) );
languageChange();
resize( QSize(497, 314).expandedTo(minimumSizeHint()) );
timer_thread=new QTimer(this);
connect(timer_thread,SIGNAL(timeout()),SLOT(initthread()));
timer_thread->start(500,true);
timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),SLOT(ontimer()));
timer->start(500,false);
}
/*
* Destroys the object and frees any allocated resources
*/
Form1::~Form1()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void Form1::languageChange()
{
setCaption( tr( "Form1" ) );
textLabel1->setText( tr( "textLabel1" ) );
}
void Form1::ontimer()
{
// while(1)
// {
printf("\n Now in function ontimer.\n");
textLabel1->setText( tr(m_str) );
// }
}
void * thread1(void * para)
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
printf("\n Now in function thread1.\n");
if ((he=gethostbyname("192.168.1.196")) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
while (1)
{ memset(buf,0,sizeof(buf));
if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)
{
perror("recv");
exit(1);
}
if (numbytes>0) {
buf[numbytes] = '\0';
printf("Received: %s\n",buf);
sprintf(m_str,"%s",buf);
}
else
break;
}
close(sockfd);
}
void Form1::initthread()
{
pthread_t th_a;
pthread_create(&th_a,NULL,thread1,NULL);
}
基本完成了,晚上把主函數(shù)寫出來就算是大功告成了,發(fā)現(xiàn)了幾個做工程好苗子,能吃苦,能鉆研,是老師們最喜歡的!!