当前位置: 技术问答>linux和unix
linux下c/s回射程序读写问题?
来源: 互联网 发布时间:2016-05-30
本文导语: 参考书籍写了个c /s回射程序,为了读写数据流调用了readn 和writen函数. 如果c和s端采用writen和read 函数,那么程序能正常运行。运行如下 客户端 Send: asdf Receive: asdf Send: asdf Receive: asdf Send: 阿三等分 Receive: 阿三等...
参考书籍写了个c /s回射程序,为了读写数据流调用了readn 和writen函数. 如果c和s端采用writen和read
函数,那么程序能正常运行。运行如下
客户端
Send:
asdf
Receive:
asdf
Send:
asdf
Receive:
asdf
Send:
阿三等分
Receive:
阿三等分
Send:
服务器端
Receive:asdf
length:5
Send length:5
Receive:asdf
length:5
Send length:5
Receive:阿三等分
length:13
Send length:13
。
但是若将read用readn代替,那么客户端总是阻塞于输入,按回车检也不管用,除非ctl+c
客户端
Send:
a
sssfss
^C
kun@kun-desktop:~$
小弟初学,望各位dx多多指点,Thank you in advance!
附:
服务器端程序:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096
#define BUFF_SIZE 8192
#define SERV_PORT 1986
#define LISTENQ 10
#define bzero(ptr,n) memset(ptr,0,n)
ssize_t readn(int fd,void *vptr,size_t n);
ssize_t writen(int fd,const void *vptr,size_t n);
//ssize_t readline(int fd,void *vptr,size_t maxlen);
void str_echo(int);
void sig_chld(int);
int main(int argc,char **argv)
{ int listenfd,connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(listenfd0)
{ nwrite=write(fd,ptr,nleft);
if(nwrite0)
{ nread=read(fd,ptr,nleft);
if(nread
函数,那么程序能正常运行。运行如下
客户端
Send:
asdf
Receive:
asdf
Send:
asdf
Receive:
asdf
Send:
阿三等分
Receive:
阿三等分
Send:
服务器端
Receive:asdf
length:5
Send length:5
Receive:asdf
length:5
Send length:5
Receive:阿三等分
length:13
Send length:13
。
但是若将read用readn代替,那么客户端总是阻塞于输入,按回车检也不管用,除非ctl+c
客户端
Send:
a
sssfss
^C
kun@kun-desktop:~$
小弟初学,望各位dx多多指点,Thank you in advance!
附:
服务器端程序:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096
#define BUFF_SIZE 8192
#define SERV_PORT 1986
#define LISTENQ 10
#define bzero(ptr,n) memset(ptr,0,n)
ssize_t readn(int fd,void *vptr,size_t n);
ssize_t writen(int fd,const void *vptr,size_t n);
//ssize_t readline(int fd,void *vptr,size_t maxlen);
void str_echo(int);
void sig_chld(int);
int main(int argc,char **argv)
{ int listenfd,connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(listenfd0)
{ nwrite=write(fd,ptr,nleft);
if(nwrite0)
{ nread=read(fd,ptr,nleft);
if(nread