我在运行的时候只要一单击button1就会死机,请各位帮忙看看是怎么回事,哪里出错了?该怎么解决?procedure TForm1.FormCreate(Sender: TObject);
var
WSAData:TWSAData;
begin
//init the version of the windows socket
if(WSAStartup(MAKEWORD(2,0),WSAData)<>0)then
showmessage('Error at WSAStartup().')
else
showmessage('WSAStartup() is success.');
//-------------------------------------------------------------
//create a socket--gameServerSocket
gameServerSocket:=socket(AF_INET,SOcK_STREAM,IPPROTO_TCP);
if gameServerSocket=INVALID_SOCKET then
begin
showmessage('Error at socket()'+IntToStr(WSAGetLastError()));
end; //-------------------------------------------------------------
//bind the socket
serverAddr.sin_family:=AF_INET;
serverAddr.sin_addr.S_addr:=INADDR_ANY;//inet_addr('127.0.0.1');
serverAddr.sin_port:=4040;
bind(gameServerSocket,serverAddr,sizeof(serverAddr));
end;procedure TForm1.Button1Click(Sender: TObject);
begin
//----------------------------------------------
//listen for the connection requests
listen(gameServerSocket,1);
//----------------------------------------------
//create a sockt for accept
StatusBar1.SimpleText:='Waiting for client to connect......'; //----------------------------------------------
//accept then connection
while true do
begin
acceptSocket:=SOCKET_ERROR; while acceptSocket=SOCKET_ERROR do
begin
acceptSocket:=accept(gameServerSocket,nil,nil);
end; StatusBar1.SimpleText:='Client connected.';
gameServerSocket:=acceptSocket;
break;
end;
end;我在运行的时候只要一单击button1就会死机,请各位帮忙看看是怎么回事,哪里出错了?该怎么解决?
var
WSAData:TWSAData;
begin
//init the version of the windows socket
if(WSAStartup(MAKEWORD(2,0),WSAData)<>0)then
showmessage('Error at WSAStartup().')
else
showmessage('WSAStartup() is success.');
//-------------------------------------------------------------
//create a socket--gameServerSocket
gameServerSocket:=socket(AF_INET,SOcK_STREAM,IPPROTO_TCP);
if gameServerSocket=INVALID_SOCKET then
begin
showmessage('Error at socket()'+IntToStr(WSAGetLastError()));
end; //-------------------------------------------------------------
//bind the socket
serverAddr.sin_family:=AF_INET;
serverAddr.sin_addr.S_addr:=INADDR_ANY;//inet_addr('127.0.0.1');
serverAddr.sin_port:=4040;
bind(gameServerSocket,serverAddr,sizeof(serverAddr));
end;procedure TForm1.Button1Click(Sender: TObject);
begin
//----------------------------------------------
//listen for the connection requests
listen(gameServerSocket,1);
//----------------------------------------------
//create a sockt for accept
StatusBar1.SimpleText:='Waiting for client to connect......'; //----------------------------------------------
//accept then connection
while true do
begin
acceptSocket:=SOCKET_ERROR; while acceptSocket=SOCKET_ERROR do
begin
acceptSocket:=accept(gameServerSocket,nil,nil);
end; StatusBar1.SimpleText:='Client connected.';
gameServerSocket:=acceptSocket;
break;
end;
end;我在运行的时候只要一单击button1就会死机,请各位帮忙看看是怎么回事,哪里出错了?该怎么解决?
这里有问题
acceptSocket:=accept(gameServerSocket,nil,nil);
就不能进行下去了,但是还是不知道怎么回事?
SOCKET s,
struct sockaddr* addr,
int* addrlen
);s
[in] Descriptor that identifies a socket that has been placed in a listening state with the listen function. The connection is actually made with the socket that is returned by accept.
addr
[out] Optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family that was established when the socket from the sockaddr structure was created.
addrlen
[in, out] Optional pointer to an integer that contains the length of addr.
这是MSDN上的。不是很清楚!
SOCKET s,
struct sockaddr* addr,
int* addrlen
);struct sockaddr* addr,---连接服务端客户端的地址吗?
acceptSocket:=accept(gameServerSocket,0,0);
[Warning] UnitSocketServer.pas(139): Constant 0 converted to NIL
while true do
这里有问题
struct sockaddr FAR *addr,
int FAR *addrlen ); 参 数: s Socket的识别码
addr 存放来连接的彼端的位址
addrlen addr的长度 传回值: 成功 - 新的Socket识别码
失败 - INVALID_SOCKET (呼叫 WSAGetLastError() 可得知原因) 说明: Server 端之应用程式呼叫此一函式来接受 Client 端要求之
Socket 连接动作;如果Server 端之 Socket 是为 Blocking 模式,且没有人
要求连接动作,那麽此一函式会Block 函式马上回覆错误。accept() 函式的答
覆值为一新的 Socket,此 Socket 不可再用来接受其它的连接要求;但是原
先之 Socket 仍可接受其他人的连接要求。