Accept a connection from a socket (deprecated).
Source position: socketsh.inc line 179
function Accept( |
Sock: LongInt; |
var addr: TInetSockAddr; |
var SockIn: file; |
var SockOut: file |
):Boolean; |
Sock: LongInt; |
var addr: TInetSockAddr; |
var SockIn: text; |
var SockOut: text |
):Boolean; |
Sock: LongInt; |
var addr: ansistring; |
var SockIn: text; |
var SockOut: text |
):Boolean; |
Sock: LongInt; |
var addr: ansistring; |
var SockIn: file; |
var SockOut: file |
):Boolean; |
Accept accepts a connection from a socket Sock, which was listening for a connection. If a connection is accepted, a file descriptor is returned. On error -1 is returned. The returned socket may NOT be used to accept more connections. The original socket remains open.
The Accept call fills the address of the connecting entity in Addr, and sets its length in Addrlen. Addr should be pointing to enough space, and Addrlen should be set to the amount of space available, prior to the call.
The alternate forms of the Accept command, with the Text or File parameters are equivalent to subsequently calling the regular Accept function and the Sock2Text or Sock2File functions. These functions return True if successful, False otherwise.
On error, -1 is returned, and errors are reported in SocketError, and include the following:
|
Listen for connections on a socket. |
|
|
Open a connection to a server socket (deprecated). |
|
|
Open a connection to a server socket. |
|
|
Bind a socket to an address. |
Program server; {$mode objfpc} {$h+} { Program to test Sockets unit by Michael van Canneyt and Peter Vreman Server Version, First Run sock_svr to let it create a socket and then sock_cli to connect to that socket } uses Sockets; Var FromName : string; Buffer : string; S,fd : Longint; len : longword; SAddr,SRemote : TInetSockAddr; procedure perror (const Msg:string); begin writeln (Msg,': ',SocketError); halt(100); end; procedure endnst msg : string); begin fpSend(fd,@msg[1],length(msg),0); end; function Receive(var S : string) : boolean; var nRead : integer; begin SetLength(S,255); nRead:=fpRecv(fd,@S[1],255,0); Result:=nRead>0; if Result then SetLength(S,nRead) else SetLength(S,0); end; begin S:=fpSocket (AF_INET,SOCK_STREAM,0); if SocketError<>0 then Perror ('Server : Socket : '); SAddr.inamily:=AF_INET; { port 5000 in network order } SAddr.inort:=htons(5000); SAddr.inddr.s_addr:=0; if fpBind(S,@SAddr,sizeof(saddr))=-1 then PError ('Server : Bind : '); if fpListen (S,1)=-1 then PError ('Server : Listen : '); Writeln('Waiting for Connect from Client, run now sock_cli in an other tty'); len:=sizeof(SRemote); fd:=fpAccept(S, @SRemote,@len); if fd=-1 then PError ('Server : Accepting connection') else begin endessage From Server'); while Receive(Buffer) do begin Writeln('Server : read : ',Buffer); end; fpShutdown(fd,2); end; CloseSocket(s); end.