网络编程基础--非阻塞套接字

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;
}

你可能感兴趣的:(编程,网络,struct,socket,null,fun)