1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
{%TARGET=linux,freebsd,darwin}
program tb0524;
uses sockets,baseunix,sysutils;
const port=6667;
procedure do_server;
var s,t:string;
lsock,usock:longint;
saddr:Tinetsockaddr;
len:longInt;
sin,sout:text;
i:byte;
begin
lsock:=socket(af_inet,sock_stream,0);
if lsock=-1 then
begin
writeln(socketerror);
halt(1);
end;
with saddr do
begin
family:=af_inet;
port:=ntobe(word(6667));
addr:=0;
end;
if not bind(lsock,saddr,sizeof(saddr)) then
begin
writeln(socketerror);
halt(1);
end;
if not listen(lsock,1) then
begin
writeln(socketerror);
halt(1);
end;
len:=sizeof(saddr);
usock:=accept(lsock,saddr,len);
if usock=-1 then
begin
writeln(SocketError);
halt(1);
end;
sock2text(usock,sin,sout);
reset(sin);
rewrite(sout);
repeat
readln(sin,s);
t:='';
for i:=length(s) downto 1 do
t:=t+s[i];
writeln(sout,t);
until eof(sin);
close(sin);
close(sout);
shutdown(usock,2);
end;
procedure do_client;
var s:sizeint;
saddr:Tinetsockaddr;
sin,sout:text;
str:ansistring;
begin
s:=socket(af_inet,sock_stream,0);
saddr.sin_family:=af_inet;
saddr.sin_port:=htons(port);
saddr.sin_addr.s_addr:=hosttonet($7f000001); {127.0.0.1}
if connect(s,saddr,sin,sout)=false then
begin
writeln(socketerror);
halt(1);
end;
writeln(sout,'abcd');
readln(sin,str);
if str<>'dcba' then
halt(1);
writeln(sout,'1234');
readln(sin,str);
if str<>'4321' then
halt(1);
close(sin);
close(sout);
shutdown(s,2);
end;
begin
if fpfork=0 then
do_server
else
begin
{Give server some time to start.}
sleep(2000);
do_client;
end;
end.
|