logo

Navigation:First_Community->Software->Linux development Goto:New TopicSettingSearch
Linux网络编程--4. 完整的读写函数
Posted by: imac Date: March 29, 2008 02:06AM

CopyRight owned by the original author.--(www.MegaEntry.com)

一旦我们建立了连接,我们的下一步就是进行通信了.在Linux下面把我们前面建立的通道 看成是文件描述符,这样服务器端和客户端进行通信时候,只要往文件描述符里面读写东西了. 就象我们往文件读写一样.  4.1 写函数write   ssize_t write(int fd,const void *buf,size_t nbytes)

CopyRight owned by the original author.--(www.MegaEntry.com)

write函数将buf中的nbytes字节内容写入文件描述符fd.成功时返回写的字节数.失败时返回-1. 并设置errno变量. 在网络程序中,当我们向套接字文件描述符写时有俩种可能.  1)write的返回值大于0,表示写了部分或者是全部的数据.  2)返回的值小于0,此时出现了错误.我们要根据错误类型来处理. 

CopyRight owned by the original author.--(www.MegaEntry.com)

如果错误为EINTR表示在写的时候出现了中断错误.  如果为EPIPE表示网络连接出现了问题(对方已经关闭了连接).  为了处理以上的情况,我们自己编写一个写函数来处理这几种情况. 

CopyRight owned by the original author.--(www.MegaEntry.com)

int my_write(int fd,void *buffer,int length) {  int bytes_left;  int written_bytes;

CopyRight owned by the original author.--(www.MegaEntry.com)

 char *ptr;  ptr=buffer;  bytes_left=length;  while(bytes_left>0)  {

CopyRight owned by the original author.--(www.MegaEntry.com)

        /* 开始写*/         written_bytes=write(fd,ptr,bytes_left);         if(written_bytes<=0) /* 出错了*/         {                        if(errno==EINTR) /* 中断错误 我们继续写*/                         written_bytes=0;

MegaEntry - Social networking and discussion site!

                else             /* 其他错误 没有办法,只好撤退了*/                         return(-1);         }         bytes_left-=written_bytes;         ptr+=written_bytes;     /* 从剩下的地方继续写  */  }

CopyRight owned by the original author.--(www.MegaEntry.com)

 return(0); } 4.2 读函数read  ssize_t read(int fd,void *buf,size_t nbyte) read函数是负责从fd中读取内容.当读成功时,read返回实际所读的字节数,如果返回的值是0 表示已经读到文件的结束了,小于0表示出现了错误.如果错误为EINTR说明读是由中断引起的, 如果是ECONNREST表示网络连接出了问题. 和上面一样,我们也写一个自己的读函数. 

MegaEntry - Social networking and discussion site!

int my_read(int fd,void *buffer,int length) {  int bytes_left;  int bytes_read;  char *ptr;   

MegaEntry - Social networking and discussion site!

 bytes_left=length;  while(bytes_left>0)  {    bytes_read=read(fd,ptr,bytes_read);    if(bytes_read<0)    {

CopyRight owned by the original author.--(www.MegaEntry.com)

     if(errno==EINTR)         bytes_read=0;      else         return(-1);    }    else if(bytes_read==0)

MegaEntry - Social networking and discussion site!

       break;     bytes_left-=bytes_read;     ptr+=bytes_read;  }  return(length-bytes_left); }

MegaEntry - Social networking and discussion site!

4.3 数据的传递  有了上面的两个函数,我们就可以向客户端或者是服务端传递数据了.比如我们要传递一个结构.可以使用如下方式   /*  客户端向服务端写 */

CopyRight owned by the original author.--(www.MegaEntry.com)

 struct my_struct my_struct_client;  write(fd,(void *)&my_struct_client,sizeof(struct my_struct);  /* 服务端的读*/   char buffer[sizeof(struct my_struct)];  struct *my_struct_server;

MegaEntry - Social networking and discussion site!

 read(fd,(void *)buffer,sizeof(struct my_struct));   my_struct_server=(struct my_struct *)buffer;     在网络上传递数据时我们一般都是把数据转化为char类型的数据传递.接收的时候也是一样的 注意的是我们没有必要在网络上传递指针(因为传递指针是没有任何意义的,我们必须传递指针所指向的内容) 


Reply To This Message
Subject: 

Copyright 2005-2006 megaentry,All Rights Reserved
IE 6.0 or above is perfect