網路編程基礎–非阻塞通訊端

來源:互聯網
上載者:User

1.非阻塞網路程式--伺服器端

由於通訊端是一種特殊的檔案,因此,可以使用變更檔阻塞狀態的方法修改通訊端的阻塞狀態。

當通訊端被設定為非阻塞狀態時,如果對資料暫停用通訊端進行讀寫操作,讀寫函數會返回-1,共置errno為EAGAIN,表示當前資料不可用。

下例同樣示範大小寫字母轉換的程式。

//server.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netient/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#define MAX_LINE 100

void my_fun(char * p)
{
if(p==NULL)
    return ;

for(;*p!='/0';p++)
  if(*p>='A'&&*p<='Z')
    *p=*p-'A'+'a';

}

int main()
{
struct sockaddr_in sin;
struct sockaddr_in cin;
int s_fd;
int port=8000;
socklen_t addr_len;
char buf[MAX_LINE];
char addr_p[INET_ADDRSTRLEN];//存放IP地址的緩衝區
int n;

bzero(&sin,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_port=htons(port);

s_fd=socket(AF_INET,SOCK_DGRAM,0);//使用UDP協議建立一個通訊端,並且得到發送本資料包的用戶端的地址
if(s_fd==-1){
perror("fail to creat socket");
exit(1);
}

if(bind(s_fd,(struct sockaddr * )&sin,sizeof(sin))==-1){
perror("call to bind");
exit(1);
}

flags=fcntl(sock_fd,F_GETFL);
flags |=O_NONBLOCK;//設定通訊端為非阻塞通訊端的標誌

if(fcntl(sock_fd,F_SETFL,flags)==-1){
perror("fail to fcntl");
exit(1);
}

while(1){
sleep(5);//休眠5秒鐘,等待資料可用
addr_len=sizeof(fin);

/*UDP協議時不需連線的,所以只有一個通訊端接受用戶端傳輸的資料包
 *而不需要建立一個額外的通訊端,用於建立串連
 *同樣的使用UDP協議的網路程式也不需要調用accept函數
 */
//接受用戶端傳送過來的字串
n=recvfrom(s_fd,buf,MAX_LINE,0,(struct sockaddr * )&sin,&addr_len);
if(n==-1&&errno!=EAGAIN){
perror("fail to receive/n");
exit(1);
}else if(errrno=EAGAIN)
  printf("socket are not ready now/n");

inet_ntop(AF_INET,&cin.sin_addr,addr_p,sizeof(addr_p));

printf("client IP is %s,port is %d/n",addr_p,ntohs(cin.sin_port));

printf("content is:%s/n",buf);

my_fun(buf);

n=sendto(s_fd,buf,n,0,(struct sockaddr *)&cin,addr_len);
if(n==-1&&errno!=EAGAIN){
perror("fail to send");
exit(1);
}else if(errno==EAGAIN)
  printf("socket are not ready now/n");
}

if(close(s_fd)==-1){
perror("fail to close");
exit(1);
}

return 0;
}

 

2.非阻塞網路程式--用戶端

//client.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netient/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>

#define MAX_LINE 100

int main()
{
struct sockaddr_in sin;
struct sockaddr_in cin;
int s_fd;
int port=8000;
socklen_t addr_len;
char buf[MAX_LINE];
char addr_p[INET_ADDRSTRLEN];//存放IP地址的緩衝區
int n;

bzero(&sin,sizeof(sin));
sin.sin_family=AF_INET;
inet_pton(AF_INET,"127.0.0.1",&sin.sin_addr);
sin.sin_port=htons(port);

s_fd=socket(AF_INET,SOCK_DGRAM,0);//使用UDP協議建立一個通訊端
if(s_fd==-1){
perror("fail to creat socket");
exit(1);
}

if(fgets(buf,MAXLINE,stdin)==NULL){
perror("fail to get a line");
exit(1);
}

n==sendto(s_fd,buf,strlen(str)+1,0,(struct sockaddr *) &sin,sizeof(sin));
if(n==-1){
perror("fail to send");
exit(1);
}

addr_len=sizeof(cin);

n=recvfrom(s_fd,buf,MAAX_LINE,0,(struct sockaddr *)&cin,&addr_len);
if(n==-1){
perror("fail to receive/n");
exit(1);
}else
  printf("receive from server:%s/n",buf);

if(close(s_fd)==-1){
perror("fail to close");
exit(1);
}

return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.