do
{
tv.tv_sec=2;
tv.tv_usec=0;
FD_ZERO(&fds);
FD_SET(nsock,&fds);
select(32,&fds,NULL,NULL,&tv);
} while(FD_ISSET(nsock,&fds)==0);
rec=recv(nsock,buf,sizeof(buf),0);
if(rec<=0)
{
closesocket(nsock);
nsock=-1;
strcpy(buf,"Disconnect");
}
これを単純に繰り返すようにすると、
do
{
do
{
tv.tv_sec=2;
tv.tv_usec=0;
FD_ZERO(&fds);
FD_SET(nsock,&fds);
select(32,&fds,NULL,NULL,&tv);
} while(FD_ISSET(nsock,&fds)==0);
} while((rec=recv(nsock,buf,sizeof(buf),0))<=0);
closesocket(nsock);
nsock=-1;
とすることができます。
//---------------------------------------------------------------------
int32 test_thread(void *Owner)
{
uint32 oldValue,newValue;
oldValue=modifiers();
while(true)
{
if((newValue=modifiers())!=oldValue)
{
((BWindow *)Owner)->PostMessage(new BMessage(MSG_SEND));
oldValue=newValue;
}
}
}
//---------------------------------------------------------------------
/**** ファイル名 : MainWindow.h ****/
#ifndef MAINWINDOW #define MAINWINDOW //--------------------------------------------------------------------- #include <Be.h> //--------------------------------------------------------------------- #define MAINWINDOW_TITLE "Thread" #define MAINWINDOW_POSITION_LEFT 100 #define MAINWINDOW_POSITION_TOP 100 #define MAINWINDOW_POSITION_WIDTH 400 #define MAINWINDOW_POSITION_HEIGHT 128 #define MAINWINDOW_WINDOWSTYLE (B_TITLED_WINDOW) //--------------------------------------------------------------------- #define MSG_SEND 'sndb' //--------------------------------------------------------------------- class BAppMainView : public BView { public: BStringView *strview; BAppMainView(BRect frame); }; //--------------------------------------------------------------------- class BAppMainWindow : public BWindow { private: int cnt; thread_id tstthr; public: BAppMainView *mainview; //----------------------------------------------------------------- BAppMainWindow(BRect frame,const char *title); virtual void MessageReceived(BMessage *msg); //----------------------------------------------------------------- bool QuitRequested(); }; //--------------------------------------------------------------------- #endif
/**** ファイル名 : MainWindow.cpp ****/
//--------------------------------------------------------------------- #include "MainWindow.h" //--------------------------------------------------------------------- int32 test_thread(void *Owner) { uint32 oldValue,newValue; oldValue=modifiers(); while(true) { if((newValue=modifiers())!=oldValue) { ((BWindow *)Owner)->PostMessage(new BMessage(MSG_SEND)); oldValue=newValue; } } } //--------------------------------------------------------------------- BAppMainWindow::BAppMainWindow(BRect frame,const char *title) :BWindow(frame,title,MAINWINDOW_WINDOWSTYLE,0) { mainview=new BAppMainView(Bounds()); AddChild(mainview); cnt=0; tstthr=spawn_thread(test_thread,"TestThread", B_NORMAL_PRIORITY,(void *)this); resume_thread(tstthr); } //--------------------------------------------------------------------- void BAppMainWindow::MessageReceived(BMessage *msg) { switch(msg->what) { case MSG_SEND: { char buf[100]; cnt++; sprintf(buf,"cnt=%d",cnt); mainview->strview->SetText(buf); } break; default: BWindow::MessageReceived(msg); } } //--------------------------------------------------------------------- BAppMainView::BAppMainView(BRect frame) :BView(frame,"bappmainview",B_FOLLOW_ALL,B_WILL_DRAW) { BRect viewrect(Bounds()); strview=new BStringView(BRect(8,68,viewrect.right-8,88),"strview", "NoData", B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP); AddChild(strview); } //--------------------------------------------------------------------- bool BAppMainWindow::QuitRequested() { kill_thread(tstthr); be_app->PostMessage(B_QUIT_REQUESTED); return true; }; //---------------------------------------------------------------------
| 圧縮ファイル R5 Intel環境で確認 |
Be7thApp20001009.zip |
| ソースファイル | BaseApp.h |
| main.cpp | |
| MainWindow.cpp | |
| MainWindow.h |