server improved. -- noch

This commit is contained in:
norayr 2015-03-19 21:01:47 +04:00
parent 48041fee19
commit 3d7c49a6dc
3 changed files with 65 additions and 10 deletions

39
src/test/server/readme.md Normal file
View file

@ -0,0 +1,39 @@
forking server example in oberon
================================
to compile
>make
run
>./s
test
>telnet localhost 2055
write something
>հեյ
stdout of the client
> $ telnet localhost 2055
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
հեյ
Affirmative, DaveConnection closed by foreign host.
stdout of the server:
>$ ./s
socket created.
binding completed.
listen okay
accept okay
forked okay
received message is հեյ

View file

@ -17,7 +17,7 @@ BEGIN
IF n < 0 THEN IF n < 0 THEN
Out.String("error reading from socket"); Out.Ln; Out.String("error reading from socket"); Out.Ln;
END; END;
Out.String("message is "); Out.String(str); Out.Ln; Out.String("received message is "); Out.String(str); Out.Ln;
s := 17; s := 17;
n := Unix.Write(sock, S.ADR(aff), s); n := Unix.Write(sock, S.ADR(aff), s);
IF n < 2 THEN IF n < 2 THEN
@ -26,13 +26,27 @@ BEGIN
END DoSmth; END DoSmth;
PROCEDURE htons(in: Int16; VAR out : Int16);
BEGIN
out[0] := in[1];
out[1] := in[0];
END htons;
PROCEDURE ZeroByteArr(VAR a : ARRAY OF S.BYTE);
VAR i : LONGINT;
BEGIN
FOR i := 0 TO LEN(a)-1 DO
a[i] := 0
END;
END ZeroByteArr;
PROCEDURE serve; PROCEDURE serve;
VAR sockfd, newsockfd, portno, clilen, pid: sockets.Int32; VAR sockfd, newsockfd, portno, clilen, pid: sockets.Int32;
ServAddr, CliAddr: sockets.SockAddrIn; ServAddr, CliAddr: sockets.SockAddrIn;
Null : Int32; Null : Int32;
Port, maxQueue, res : Int32; Port, maxQueue, res : Int32;
afinet, port: Int16; afinet, port, port0: Int16;
BEGIN BEGIN
Port := 2055; Port := 2055;
maxQueue := 5; maxQueue := 5;
@ -47,12 +61,14 @@ BEGIN
types.IntegerToInt16(sockets.AfInet, afinet); types.IntegerToInt16(sockets.AfInet, afinet);
types.IntegerToInt16(Port, port); types.IntegerToInt16(Port, port);
htons(port, port0); (* only necessary on little endian computers *)
ServAddr.SinFamily := afinet; ServAddr.SinFamily := afinet;
ServAddr.SinPort := port; ServAddr.SinPort := port0;
Out.String("listening on port ");Out.Int(S.VAL(INTEGER, ServAddr.SinPort), 0); Out.Ln; ZeroByteArr(ServAddr.SinZero);
(*Out.String("listening on port ");Out.Int(S.VAL(INTEGER, ServAddr.SinPort), 0); Out.Ln;*)
ServAddr.SinAddr.SAddr := 0(*sockets.InAddrAny*); ServAddr.SinAddr.SAddr := 0(*sockets.InAddrAny*);
res := sockets.Bind(sockfd, S.VAL(sockets.SockAddr, ServAddr), (SIZE(sockets.SockAddrIn))); res := sockets.Bind(sockfd, S.VAL(sockets.SockAddr, ServAddr), (SIZE(sockets.SockAddr)));
IF res < 0 THEN IF res < 0 THEN
Out.String("error on binding") Out.String("error on binding")
ELSE ELSE

View file

@ -11,7 +11,7 @@ TYPE
Int64* = LONGINT; Int64* = LONGINT;
String* = ARRAY 256 OF CHAR; String* = ARRAY 256 OF CHAR;
PROCEDURE LongintToInt16*(int: LONGINT; VAR int16: Int16)(* : Int16*); PROCEDURE LongintToInt16*(int: LONGINT; VAR int16: Int16);
VAR longintarr : intarr64; VAR longintarr : intarr64;
BEGIN BEGIN
(*RETURN SYS.VAL(Int16, int)*) (*RETURN SYS.VAL(Int16, int)*)
@ -20,13 +20,13 @@ TYPE
int16[1] := longintarr[1]; (* this will work for little endian -- noch *) int16[1] := longintarr[1]; (* this will work for little endian -- noch *)
END LongintToInt16; END LongintToInt16;
PROCEDURE IntegerToInt16*(int: INTEGER; VAR int16: Int16)(* : Int16*); PROCEDURE IntegerToInt16*(int: INTEGER; VAR int16: Int16);
VAR intarr : intarr32; VAR intarr : intarr32;
BEGIN BEGIN
(*RETURN SYS.VAL(Int16, int)*) int16 := SYS.VAL(Int16, int)
intarr := SYS.VAL(intarr32, int); (*intarr := SYS.VAL(intarr32, int);
int16[0] := intarr[0]; int16[0] := intarr[0];
int16[1] := intarr[1]; (* this will work for little endian -- noch *) int16[1] := intarr[1];*) (* this will work for little endian -- noch *)
END IntegerToInt16; END IntegerToInt16;
END types. END types.