home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
sibdemo3.zip
/
SAMPLES.DAT
/
SAMPLES
/
TCPIP
/
TCPSERV.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-07-14
|
2KB
|
87 lines
Program Tcpserv;
Uses Tcpip, ScString;
Const Portst = '19000';
var Port: Word;
srbuf: array[0..255] of char;
csocket: integer;
nsocket: integer;
cname_length: integer;
Client: sockaddr_in;
server: sockaddr_in;
rc : integer;
outbuf: Pchar;
Clitemp: Sockaddr;
Tmpmsg: Cstring;
tmplen: integer;
x:integer;
Begin
clitemp.sa_family := AF_INET;
clitemp.sa_data := #0#0#0#0#0#0#0#0#0#0#0#0#0#0;
Client := Sockaddr_in(clitemp);
Writeln('Checking for Sockets');
If Sock_init <> 0 then
begin
Writeln('TCP Not Loaded.');
readln;
Halt(1);
end;
port := String2Word(Portst);
server.sin_port := htons(port);
Writeln('Creating server socket');
csocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if Csocket < 0 then
begin
writeln('Socket Error');
readln;
Halt(2);
end;
server.sin_family := AF_INET;
server.sin_Addr := INADDR_ANY;
Writeln('Binding Socket');
Rc := bind(csocket, sockAddr(server), sizeof(serveR));
If RC < 0 then
begin
Psock_errno('Bind()');
halt(3);
end;
Writeln('Waiting for connection');
rc := Listen(csocket,2);
If RC <> 0 then
begin
Psock_errno('Listen()');
halt(4);
end;
cname_length := sizeof(client);
Writeln('Accepting Connection');
nsocket := accept(csocket, PSockaddr(@client), @cname_length);
If nsocket < 0 then
begin
Psock_errno('Accept()');
Writeln(Sock_errno);
halt(5);
end;
rc := recv(nsocket, srbuf, sizeof(srbuf),0);
if rc < 0 then
begin
Psock_errno('Recv()');
halt(6);
end;
Write('Server Connect: ');
Write(srbuf);
Writeln(' just contacted us');
TmpMsg := 'Hi, We read you loud and clear';
rc := send(nsocket, @tmpmsg,length(tmpmsg), 0);
If Rc<0 then
begin
Psock_errno('Send()');
halt(7);
end;
soclose(csocket);
soclose(nsocket);
End.