perl网络编程中关于接受数据的疑惑
接受了一个套接字的连接以后,为什么传入的数据读不出来??代码如下:
use strict;
use IO::Socket;
use IO::Handle;
use constant MY_PORT => 12007;
#构建Socket套接字
my $port = MY_PORT;
my $sock = IO::Socket::INET-> new(Listen => 20,
LocalPort => $port,
Timeout => 60*60,
Reuse => 1)
or die "Cannot create Listening socket: $!\n ";
while(1)
{
next unless my $session = $sock-> accept;
my $peer = gethostbyaddr($session-> peeraddr, AF_INET) ||
$session-> peerhost;
my $port = $session-> peerport;
print $port, "\n ";
print $peer, "\n ";
print <$session> ;
close $session;
}
close $sock;
打印了一下$!诊断了一下,说是Bad file descriptor
客户端是用C 语言写的,代码也贴上来吧
#include "stdafx.h "
#include "winsock2.h "
#include <malloc.h>
#define USERPORT 12007
#define MAJOR_VERSION1
#define MINOR_VERSION2
#define WSA_MAKEWORD(x,y) ((y)*256+(x))
int main(int argc, char* argv[])
{
char buf[1024];
//struct sockaddr_in client;
struct sockaddr_in server;
struct hostent FAR * host;
int Status;
SOCKET s;
int ns;
int namelen;
WORDwMajorVersion, wMinorVersion;
LPWSADATAlpmyWSAData;
WORD VersionReqd;
wMajorVersion = MAJOR_VERSION;
wMinorVersion = MINOR_VERSION;
VersionReqd = WSA_MAKEWORD(wMajorVersion,wMinorVersion);
lpmyWSAData = (LPWSADATA)malloc(sizeof(WSADATA));
Status = WSAStartup(VersionReqd, lpmyWSAData);
if ((s=socket(AF_INET, SOCK_STREAM, 0)) <0)
{
printf( "Socket() ");
return -1;
}
host = gethostbyname( "Yinth ");
if (host == NULL)
{
printf( "No host machine ");
return -1;
}
ZeroMemory(&server, sizeof(server));
memset(&server, '\0 ', sizeof (struct sockaddr_in));
memcpy((char FAR *)&server.sin_addr,(char FAR *)host-> h_addr,host-> h_length);
server.sin_family = host-> h_addrtype;
server.sin_port = htons(USERPORT);
printf( "请输入: ");
char str[256];
gets(str);
connect(s, (struct sockaddr FAR *)&server, sizeof(server));
int i = send(s, str, sizeof(str),MSG_OOB);
closesocket(s);
WSACleanup();
return 0;
}
希望各位能够不吝指教.谢谢.
[解决办法]
呵呵,用数据包抓取软件试下。暂时不在windows下所以不能试C代码。