[cpp]view plain
copy
/* * file: udpechoclient-timeout.c
* author: 雲守護
*/#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "diewithmessage.h"
//超時設定
static
const
unsigned
inttimeout_sec=3;
//重連次數
static
const
unsigned
intmaxtries=5;
//已經嘗試次數
unsigned int
tries=0;
//鬧鈴到來處理函式,超時時間到
void
catchalarm(
intignored);
intmain(
intargc,
char
** argv)
//然後接收資料,都是迴圈接收的
struct
sockaddr_storage fromaddr;
socklen_t fromaddrlen=sizeof
(fromaddr);
//設定定時器,超時設定
alarm(timeout_sec);
char
buffer[bufsiz+1];
//接收資料
while
((numbytes=recvfrom(sock,buffer,bufsiz,0,(
struct
sockaddr*)&fromaddr,&fromaddrlen))<0)
}else
}else
} //鬧鈴歸零
alarm(0);
buffer[echostringlen]='\0'
; printf("received: %s \n"
,buffer);
close(sock);
return
(exit_success);
} //鬧鈴響了,執行該函式
void
catchalarm(
intignored)
[cpp]view plain
copy
/* * file: udpechoserver-sigio.c
* author: 雲守護
*/#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "diewithmessage.h"
//使用者空閒時任務
void
useidletime();
//非同步io處理者
void
sigiohandler(
intsignaltype);
intserver_sock;
#define maxstringlength 1024*8
//列印客戶端位址訊息
void
printsocketaddress(
const
struct
sockaddr *address,
file
*stream)
// convert binary to printable address
if(inet_ntop(address->sa_family, numericaddress, addrbuffer,
sizeof
(addrbuffer)) == null)
fputs("[invalid address]"
, stream);
// unable to convert
else
} intmain(
intargc,
char
** argv)
freeaddrinfo(server_addr);
printf("server success %s\n"
,service);
//操作訊號,設定非同步io
struct
sigaction handler;
//設定非同步到來時的處理函式
handler.sa_handler=sigiohandler;
//填充訊號掩碼
if(sigfillset(&handler.sa_mask)<0)
diewithsystemmessage("sigfillset() failed!"
);
//標誌設定0,即無標誌
handler.sa_flags=0;
//註冊sigio訊號
if(sigaction(sigio,&handler,0)<0)
diewithsystemmessage("sigaction() failed for sigio"
);
//設定server_sock接收sigio訊號資訊
if(fcntl(server_sock,f_setown,getpid())<0)
diewithsystemmessage("fcntl failed!"
);
//使用非阻塞io和sigio訊號傳送
if(fcntl(server_sock,f_setfl,o_nonblock|fasync)<0)
diewithsystemmessage("unable to put client sock into non-blocking/async mode"
);
for(;;)
return
(exit_success);
} void
useidletime()
//非同步io處理函式
void
sigiohandler(
intsignaltype)
}else
} //迴圈接收資料
}while
(numbytesrcvd>=0);
}
[cpp]view plain
copy
/* * file: diewithmessage.h
* author: root
** created on 2023年11月13日, 下午3:52
*/#ifndef diewithmessage_h
#define diewithmessage_h
#ifdef __cplusplus
extern
"c"
void
diewithsystemmessage(
const
char
* msg)
#ifdef __cplusplus
} #endif
#endif /* diewithmessage_h */
from;
linux c socket程式設計
這裡主要是 網上有原理 socket 有同步,非同步之分,我這是個同步的,同步又有阻塞和非阻塞之分,這的非阻塞是用select 函式實現的 客戶端 include include include include include include include include include incl...
linux c socket程式設計
ntohl 與 htonl 相反的過程 ntohs 與htons 相反的過程 inet addr char 點分十進位制轉化為整型 inet nota int 整型轉化為點分十進位制 主機位元組序 和 網路位元組序 當主機位元組序埠號位為 0x1234 那麼 網路位元組序就為 0x12 0x34 如...
linux C socket程式設計
這是乙個關於socket的題 要求你自己寫乙個socket程式,然後解析客戶端從瀏覽器位址列提交的資料。要求 在瀏覽器中輸入http localhost 8500 後,你的socket程式向瀏覽器返回乙個網頁,以後就用這個網頁向你寫得這個socket程式提交資料,你的socket程式負責解析這些資料...