请教大家,Socket的accept为什么不能使用?(200分)

  • 主题发起人 主题发起人 eastnet
  • 开始时间 开始时间
E

eastnet

Unregistered / Unconfirmed
GUEST, unregistred user!
ServerSocket := Socket(AF_INET, SOCK_STREAM, 0);//建立服务器的socket
。。。。
sockfd := accept(ServerSockets, addrc, addrclen);//等待接收
。。。。

在A程序中正常,ServerSocket 返回1852,sockfd 返回0
在B程序中不正常,ServerSocket 返回1796,sockfd 返回-1
(AF_INET, SOCK_STREAM参数值两程序是相同的,ServerSockets两个程序不一样)
有谁知道是什么原因引起的啊??
 
描述过于简单,原因不好说,是不是端口的问题?
 
贴出你的代码!只是这些无法判断!
 
//启动
post:=StrtoInt(Listen_Port);
TimeServerSocket.Enabled :=True;

//Main.pas
procedure TMain.TimeServerSocketTimer(Sender: TObject);
var
ServerSocket, sockfd: TSocket;
wd: WSAData;
addr: TSockAddr;
wsstatus, rb, rl: integer;
SSocket: TSocket;
nsize: integer ;
logIp: string;
LocalRead, LocalWrite, Localexcept: TFDSet;
VTime: TTimeVal;
//RecvLen, SendLen: integer;
Hanlt,i:integer;
// Str:String;
Main: ListerSockets;
begin
TimeServerSocket.Enabled:=false;
wsstatus := WSAStartup(2, wd);//初始化winsock;
if wsstatus <> 0 then
begin
closesocket(ServerSocket);
TimeServerSocket.Enabled:=True;
exit;
end;
ServerSocket := Socket(AF_INET, SOCK_STREAM, 0);//建立服务器的socket
try
if not (ServerSocket > 0) then
begin
closesocket(ServerSocket);
TimeServerSocket.Enabled:=True;
// ShowMessage('服务器建立失败');
exit;
end;
addr.sin_family := AF_INET;
addr.sin_port := htons(post);
addr.sin_addr.S_addr := htonl(INADDR_ANY);
nsize := sizeof(addr );
rb := bind(ServerSocket, addr, nsize);//绑定,0表示成功
if rb <> 0 then
begin
closesocket(ServerSocket);
TimeServerSocket.Enabled:=True;
// ShowMessage('服务器绑定失败');
exit;
end;
rl := listen(ServerSocket, 100);//监听,0表示成功
if rl=0 then
begin
Main:=ListerSockets.Create(ServerSocket,DBConn);
end;
except
on E:exception do
begin
CloseSocket(ServerSocket);
TimeServerSocket.Enabled:=True;
end;
end;
WSACleanup;

end;


//ListerSocket.pas

procedure ListerSockets.Main;
var
addrc: PSockAddr;
addrclen: PInteger;
sockfd: TSocket;
LocalRead, LocalWrite, Localexcept: TFDSet;
Main:AcceptSocket;
begin
Try
begin
new(addrc);
new(addrclen);
addrclen^ := sizeof(addrc^);
while (true) do
begin
sockfd := accept(ServerSockets, addrc, addrclen);//等待接收
//logIp := inet_ntoa(addrc^.sin_addr); //Ip Addr Transport
if sockfd = INVALID_SOCKET then
begin
closesocket(sockfd);
exit;
end
else
begin
Main:=AcceptSocket.Create(sockfd,thedbcon);
end;
end;
end
except
on E:Exception do
begin
closesocket(sockfd);
exit;
end;
end;
end;
 
你是不是在一台机子运行啊,监听端口必须不一样
 
后退
顶部