当前位置: 技术问答>linux和unix
网络编程的小问题!
来源: 互联网 发布时间:2016-03-13
本文导语: 我的客户端程序向网络写数据函数是这样的: 我想在红颜色语句处将stdin(标准输入流)改为一个文件指针,使文件指针指向的文件内容能被发送出去,这样能行吗? 该如何修改? 赐教啊! #include "common.h" /* Read from the...
我的客户端程序向网络写数据函数是这样的:
我想在红颜色语句处将stdin(标准输入流)改为一个文件指针,使文件指针指向的文件内容能被发送出去,这样能行吗?
该如何修改? 赐教啊!
#include "common.h"
/* Read from the keyboard and write to the server
Read from the server and write to the keyboard
we use select() to multiplex
*/
void read_write(ssl,sock)
SSL *ssl;
{
int width;
int r,c2sl=0,c2s_offset=0;
fd_set readfds,writefds;
int shutdown_wait=0;
char c2s[BUFSIZZ],s2c[BUFSIZZ];
int ofcmode;
/*First we make the socket nonblocking*/
ofcmode=fcntl(sock,F_GETFL,0);
ofcmode|=O_NDELAY;
if(fcntl(sock,F_SETFL,ofcmode))
err_exit("Couldn't make socket nonblocking");
width=sock+1;
while(1){
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_SET(sock,&readfds);
/*If we've still got data to write then don't try to read*/
if(c2sl)
FD_SET(sock,&writefds);
else
FD_SET(fileno(stdin),&readfds);
r=select(width,&readfds,&writefds,0,0);
if(r==0)
continue;
/* Now check if there's data to read */
if(FD_ISSET(sock,&readfds)){
do {
r=SSL_read(ssl,s2c,BUFSIZZ);
switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
fwrite(s2c,1,r,stdout);
break;
case SSL_ERROR_ZERO_RETURN:
/* End of data */
if(!shutdown_wait)
SSL_shutdown(ssl);
goto end;
break;
case SSL_ERROR_WANT_READ:
break;
default:
berr_exit("SSL read problem");
}
} while (SSL_pending(ssl));
}
/* Check for input on the console*/
if(FD_ISSET(fileno(stdin),&readfds)){
c2sl=read(fileno(stdin),c2s,BUFSIZZ);
if(c2sl==0){
shutdown_wait=1;
if(SSL_shutdown(ssl))
return;
}
c2s_offset=0;
}
/* If we've got data to write then try to write it*/
if(c2sl && FD_ISSET(sock,&writefds)){
r=SSL_write(ssl,c2s+c2s_offset,c2sl);
switch(SSL_get_error(ssl,r)){
/* We wrote something*/
case SSL_ERROR_NONE:
c2sl-=r;
c2s_offset+=r;
break;
/* We would have blocked */
case SSL_ERROR_WANT_WRITE:
break;
/* Some other error */
default:
berr_exit("SSL write problem");
}
}
}
end:
SSL_free(ssl);
close(sock);
return;
}
我想在红颜色语句处将stdin(标准输入流)改为一个文件指针,使文件指针指向的文件内容能被发送出去,这样能行吗?
该如何修改? 赐教啊!
#include "common.h"
/* Read from the keyboard and write to the server
Read from the server and write to the keyboard
we use select() to multiplex
*/
void read_write(ssl,sock)
SSL *ssl;
{
int width;
int r,c2sl=0,c2s_offset=0;
fd_set readfds,writefds;
int shutdown_wait=0;
char c2s[BUFSIZZ],s2c[BUFSIZZ];
int ofcmode;
/*First we make the socket nonblocking*/
ofcmode=fcntl(sock,F_GETFL,0);
ofcmode|=O_NDELAY;
if(fcntl(sock,F_SETFL,ofcmode))
err_exit("Couldn't make socket nonblocking");
width=sock+1;
while(1){
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_SET(sock,&readfds);
/*If we've still got data to write then don't try to read*/
if(c2sl)
FD_SET(sock,&writefds);
else
FD_SET(fileno(stdin),&readfds);
r=select(width,&readfds,&writefds,0,0);
if(r==0)
continue;
/* Now check if there's data to read */
if(FD_ISSET(sock,&readfds)){
do {
r=SSL_read(ssl,s2c,BUFSIZZ);
switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
fwrite(s2c,1,r,stdout);
break;
case SSL_ERROR_ZERO_RETURN:
/* End of data */
if(!shutdown_wait)
SSL_shutdown(ssl);
goto end;
break;
case SSL_ERROR_WANT_READ:
break;
default:
berr_exit("SSL read problem");
}
} while (SSL_pending(ssl));
}
/* Check for input on the console*/
if(FD_ISSET(fileno(stdin),&readfds)){
c2sl=read(fileno(stdin),c2s,BUFSIZZ);
if(c2sl==0){
shutdown_wait=1;
if(SSL_shutdown(ssl))
return;
}
c2s_offset=0;
}
/* If we've got data to write then try to write it*/
if(c2sl && FD_ISSET(sock,&writefds)){
r=SSL_write(ssl,c2s+c2s_offset,c2sl);
switch(SSL_get_error(ssl,r)){
/* We wrote something*/
case SSL_ERROR_NONE:
c2sl-=r;
c2s_offset+=r;
break;
/* We would have blocked */
case SSL_ERROR_WANT_WRITE:
break;
/* Some other error */
default:
berr_exit("SSL write problem");
}
}
}
end:
SSL_free(ssl);
close(sock);
return;
}
|
直接要open,fd得了,用不着FILE *了...
|
ssl的不熟,但应该不对吧,只是指向文件,没有去读,是不会发送的
|
SendStream并不是传送一个流对象,而是把流中的数据读出来传送过去。你在接收时应根据实际情况决定使用ReceiveBuf(一般情况)或ReceiveText(如果是文本)接收。
事实上SendStream方法相当于:
char * Buf;
int BufLen;
BufLen = AStream->Size();
Buf = new char [BufLen];
AStream -> Position = 0;
AStream -> Read(Buf,BufLen);
ClientSocket1->Socket->SendBuf(Buf,BufLen);
delete []Buf;
delete AStream;//注意这句,你的流将被SendBuf释放,你不要再去释放它。
事实上SendStream方法相当于:
char * Buf;
int BufLen;
BufLen = AStream->Size();
Buf = new char [BufLen];
AStream -> Position = 0;
AStream -> Read(Buf,BufLen);
ClientSocket1->Socket->SendBuf(Buf,BufLen);
delete []Buf;
delete AStream;//注意这句,你的流将被SendBuf释放,你不要再去释放它。
|
stdin和文件流是没有区别的。
|
你可以先读到一个临时buffer再发送,我猜文件指针只是指向文件资源的一个句柄而不是指向它的内容,它内部应该有全局变量保存了文件偏移指针.所以,你这样都能发送数据出去,那还要fread之类的函数干什么.具体可以在网上找找文件流的实现原理.
|
lz要干嘛?