socket例子及高階用法

2021-08-28 20:38:57 字數 3184 閱讀 1875

使用sigalrm訊號為阻塞操作設定超時

可以處理connect和read等超時

#include#include#include#include#include#include#include#include #include #include #define maxline 4096

typedef void (*sighandler_t)(int);

#define timeout_set

static void connect_alarm(int sig_num)

static void now_alarm(int sig_num)

int main(int argc, char** argv)

; sighandler_t p;

unsigned int remain_sec;

struct sigaction sa_alarm;

if( argc != 2) else

if( (sockfd = socket(af_inet, sock_stream, 0)) < 0)

memset(&servaddr, 0, sizeof(servaddr));

//結構體清零

servaddr.sin_family = af_inet;

servaddr.sin_port = htons(6666);//埠號

if( inet_pton(af_inet, server_ip, &servaddr.sin_addr) <= 0)

printf("connect to server here~~~maybe wait here\n");

#ifdef timeout_set

#if 0

p = signal(sigalrm, connect_alarm);

if(p == sig_err)

printf("p:%p connect_alarm:%p\n", p, connect_alarm);

#else

sa_alarm.sa_flags = sa_resethand;

sa_alarm.sa_handler = connect_alarm;

sigaction(sigalrm, &sa_alarm, null);

#endif

remain_sec = alarm(5);

printf("remain sec:%d\n", remain_sec);

if(remain_sec != 0)

//while(1);

#if 0

//set to null

p = signal(sigalrm, p);

if(p == sig_err)

printf("p:%p connect_alarm:%p\n", p, connect_alarm);

#endif

#endif

if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)

printf("send msg to server: \n");

fgets(sendline, 4096, stdin);

if( send(sockfd, sendline, strlen(sendline), 0) < 0)

close(sockfd);

exit(0);

}

常規例子

server.c

#include#include#include#include#include#include#include#define maxline 4096

int main(int argc, char** argv)

memset(&servaddr, 0, sizeof(servaddr));

//協議位址

servaddr.sin_family = af_inet;

servaddr.sin_addr.s_addr = htonl(inaddr_any);

servaddr.sin_port = htons(6666);//

//bind把乙個位址族中的特定位址賦給socket

if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)

if( listen(listenfd, 10) == -1)

printf("*****=waiting for client's request*****=\n");

while(1)

n = recv(connfd, buff, maxline, 0);

buff[n] = '\0';

printf("recv msg from client: %s\n", buff);

close(connfd);

} close(listenfd);

}

client.c

#include#include#include#include#include#include#include#define maxline 4096

int main(int argc, char** argv)

if( (sockfd = socket(af_inet, sock_stream, 0)) < 0)

memset(&servaddr, 0, sizeof(servaddr));

//結構體清零

servaddr.sin_family = af_inet;

servaddr.sin_port = htons(6666);//埠號

if( inet_pton(af_inet, argv[1], &servaddr.sin_addr) <= 0)

if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)

printf("send msg to server: \n");

fgets(sendline, 4096, stdin);

if( send(sockfd, sendline, strlen(sendline), 0) < 0)

close(sockfd);

exit(0);

}

String的用法及例子

string是c 標準庫的乙個重要的部分,主要用於字串處理。可以使用輸入輸出流方式直接進行操作,也可以通過檔案等手段進行操作。其中使用的 多數都是來自cpp官網。宣告和初始化方法 想使用string首先要在標頭檔案當中加入 string 宣告 string s 宣告乙個string 物件 strin...

監聽socket例子

tcpserver.c include include include include include include include include include include includestatic bool stop false static void handle term int ...

C 中rapidxml用法及例子

c 中rapidxml用法及例子 rapidxml是乙個快速的xml庫,比tinyxml快了50 100倍。本文給出建立 讀取 寫入xml的原始碼。下面三個檔案是本段 需要的庫檔案 include rapidxml rapidxml.hpp include rapidxml rapidxml uti...