Recent

Author Topic: Winsock and OS/2 Strange Issue  (Read 1693 times)

NeddieSeagoon

  • New member
  • *
  • Posts: 8
Winsock and OS/2 Strange Issue
« on: February 12, 2024, 04:42:25 pm »
Hi,

I've messed with this for days, googled it, asked bing, searched this forum etc.

I'm writing a Internet BBS in freepascal under OS/2.  Why?  Because it's there and I'm bored with Ruby (already wrote a multithreaded BBS for Ruby and one in TP6 back in the day)... so, I'm working on a multi-threaded server, which is coming along (so far)... but... I can't get this thing to give me the correct address of a connecting client.  Here is sample code (not the server but my test spike) that gives me weird addresses under OS/2, but works FINE under Windows 11.

Under OS/2 I get:

Server is listening on port 1234
Accepted a connection from 132.153.7.0
(etc...)

On Windows:
Server is listening on port 1234
Accepted a connection from 127.0.0.1

I don't get it. 

 
Code: Pascal  [Select][+][-]
  1.  
  2. {This is a TCP server using PMWsock}
  3. program TCPServer;
  4.  
  5. uses
  6.   Winsock, SysUtils;
  7.  
  8. const
  9.   SERVER_PORT = 1234; {Arbitrary port number}
  10.   MAX_CLIENTS = 10; {Maximum number of concurrent clients}
  11.  
  12. var
  13.   WSAData: TWSAData;
  14.   ServerSocket, ClientSocket: TSocket;
  15.   ServerAddr, ClientAddr: TSockAddr;
  16.   CommAddr : SockAddr;
  17.   Buffer: array[0..255] of Char;
  18.   BytesSent, BytesRecv: Integer;
  19.   Message: String;
  20.   ClientAddrSize: Integer;
  21.   CommAddrSize : LongInt;
  22.   CommLen: LONGINT;
  23.   i : integer;
  24.   output :string;
  25. begin
  26.   {Initialize Winsock}
  27.   WSAStartup($0101, WSAData);
  28.   {Create a socket}
  29.   ServerSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  30.   {Bind the socket to a port}
  31.   ServerAddr.sin_family := AF_INET;
  32.   ServerAddr.sin_port := htons(SERVER_PORT);
  33.   ServerAddr.sin_addr.S_addr := INADDR_ANY;
  34.   bind(ServerSocket, ServerAddr, SizeOf(ServerAddr));
  35.   {Listen for incoming connections}
  36.   listen(ServerSocket, MAX_CLIENTS);
  37.   WriteLn('Server is listening on port ', SERVER_PORT);
  38.   {Accept a connection from a client}
  39.   ClientAddrSize := SizeOf(ClientAddr);
  40.   ClientSocket := accept(ServerSocket, @ClientAddr, @ClientAddrSize);
  41.   WriteLn('Accepted a connection from ', inet_ntoa(ClientAddr.sin_addr));
  42.   CommLen := Sizeof(CommAddr);
  43.   Writeln('ERROR: ', getpeername(ClientSocket, CommAddr,CommLen));
  44.   Writeln('Server Socket: ',ServerSocket);
  45.   Writeln('Client Socket: ',ClientSocket);
  46.   writeln('Last Error: ', WSAGetLastError);
  47.   Writeln('Connected IP: ');
  48.   For i := 0 to 13 do
  49.     BEGIN
  50.      write(commaddr.sa_data[i]); write(' ');
  51.     END;
  52.     Writeln;
  53.     Writeln('sa_family: ',commaddr.sa_family);
  54.     Writeln(strpas(inet_ntoa(PSockAddrIn(@CommAddr.sa_data)^.sin_addr)));
  55.   {Receive a message from the client}
  56.   BytesRecv := recv(ClientSocket, Buffer, SizeOf(Buffer), 0);
  57.   Buffer[BytesRecv] := #0;
  58.   WriteLn('Received: ', Buffer);
  59.   {Send a message to the client}
  60.   Message := 'Hello, this is a TCP server';
  61.   {BytesSent := send(ClientSocket, Message[1], Length(Message), 0);}
  62.   {Close the client socket}
  63.   closesocket(ClientSocket);
  64.   {Close the server socket}
  65.   closesocket(ServerSocket);
  66.   {Clean up Winsock}
  67.   WSACleanup;
  68. end.
  69.  
« Last Edit: February 12, 2024, 08:56:57 pm by NeddieSeagoon »

Thaddy

  • Hero Member
  • *****
  • Posts: 14615
  • Sensorship about opinions does not belong here.
Re: Winsock and OS/2 Strange Issue
« Reply #1 on: February 12, 2024, 07:05:41 pm »
132.153.7.0 is USA so maybe your external IP address?
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

NeddieSeagoon

  • New member
  • *
  • Posts: 8
Re: Winsock and OS/2 Strange Issue
« Reply #2 on: February 12, 2024, 07:32:18 pm »
That's a reasonable theory but I live in the UK.

I've checked... Mine starts with 86...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5519
  • Compiler Developer
Re: Winsock and OS/2 Strange Issue
« Reply #3 on: February 12, 2024, 08:45:53 pm »
{This is a TCP server using PMWsock}

Please use [code=pascal][/code]-tags to avoid the forum software interpreting your code.

NeddieSeagoon

  • New member
  • *
  • Posts: 8
Re: Winsock and OS/2 Strange Issue
« Reply #4 on: February 12, 2024, 08:57:32 pm »
Sorry about that.  Fixed.

Thaddy

  • Hero Member
  • *****
  • Posts: 14615
  • Sensorship about opinions does not belong here.
Re: Winsock and OS/2 Strange Issue
« Reply #5 on: February 12, 2024, 09:02:16 pm »
That's a reasonable theory but I live in the UK.

I've checked... Mine starts with 86...
Please figure out how that can happen. Are you using a cloud service? It worries me a bit. OS/2 should return a local IP.
If have little or no current knowledge about OS/2 although I used  it in the past.
« Last Edit: February 12, 2024, 09:05:32 pm by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

NeddieSeagoon

  • New member
  • *
  • Posts: 8
Re: Winsock and OS/2 Strange Issue
« Reply #6 on: February 13, 2024, 12:48:34 pm »
Heh.  I will try to figure it out.  It would be great if someone else who was running OS/2 could give it a try.. I'm running it in Virtualbox.  It's actually this AcraOS ... but it's basically OS/2 4.52.

I used to support it back in the day, when I worked at ASU... but back in the say was 25 years ago, and in Arizona. 

I wonder if there is some sort of ,dll problem... I'll report back.

NeddieSeagoon

  • New member
  • *
  • Posts: 8
Re: Winsock and OS/2 Strange Issue
« Reply #7 on: February 15, 2024, 11:30:57 am »
Okay.. I've been playing with this... and I conclude that it is either a bug in PMWsock.dll (unlikely) or more likely, a bug in the FreePascal unit.

Turns out the returned client address is available in Integer format via:

addr.sin_addr.S_addr

And that, in my case, is equal to 16777343...

so, I wrote this procedure (and I hate math):

Code: Pascal  [Select][+][-]
  1.  
  2. uses
  3.   math,sysutils;
  4.  
  5. FUNCTION Ntoa(addr:LONGINT):String;
  6.  
  7.   VAR
  8.   IP     : STRING;
  9.   I,
  10.   octet  : INTEGER;
  11. BEGIN
  12.   IP := '';
  13.  
  14.  {Loop through the four octets from left to right}
  15.   FOR I := 3 downto 0 DO
  16.     BEGIN
  17.       {shift the address to the right by 8*i bits
  18.        and mask the last 8 bits with 255}
  19.       octet := (addr shr (8*i)) and 255;
  20.  
  21.       IF I <> 3 THEN
  22.         ip := inttostr(octet) + '.' + ip
  23.       ELSE
  24.         ip := inttostr(octet) + ip;
  25.     END;
  26.   Ntoa := IP;
  27.  END;
  28.  
  29. BEGIN
  30.   Writeln(Ntoa(16777343));
  31. end.
  32.  

Which gives me, 127.0.0.1, which is the RIGHT ANSWER.

I don't know who maintains this or how to report a problem?

BTW:  The regular Sockets unit doesn't seem to work right either... but that's another story.


Thaddy

  • Hero Member
  • *****
  • Posts: 14615
  • Sensorship about opinions does not belong here.
Re: Winsock and OS/2 Strange Issue
« Reply #8 on: February 15, 2024, 11:36:11 am »
You should handle the pointer-to-structure, not the structure itself:
This is a pointer type: addr.sin_addr.S_addr
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

NeddieSeagoon

  • New member
  • *
  • Posts: 8
Re: Winsock and OS/2 Strange Issue
« Reply #9 on: February 15, 2024, 02:43:31 pm »
Like so?

Code: Pascal  [Select][+][-]
  1.   WriteLn('Accepted a connection from ', inet_ntoa(@ClientAddr.sin_addr));
  2.   IP := PInteger(@ClientAddr.sin_addr.s_addr)^;
  3.   Writeln('NTOA: ',Ntoa(IP));
  4.  

That works (my most recent Pascal programming is from Turbo Pascal 6.  I'm afraid Ruby has damanged my brain.)...

I still get the wrong answer from inet_ntoa and the right answer from my function.  Hmmm...


TRon

  • Hero Member
  • *****
  • Posts: 2675
Re: Winsock and OS/2 Strange Issue
« Reply #10 on: February 15, 2024, 02:48:44 pm »
Are you aware of the existence of StrToHostAddr and friends  ?

NeddieSeagoon

  • New member
  • *
  • Posts: 8
Re: Winsock and OS/2 Strange Issue
« Reply #11 on: February 15, 2024, 03:23:25 pm »
Yes sir, but that (StrToHostAddr) doesn't exist in the PMWsock unit (which is the Winsock unit for OS/2).

It's the only one I've been able to get to work.  I'd much rather use the Sockets unit, but I'm constantly getting 10014 errors trying to us fpaccept.

The PWMsock.dll which this is using only does Winsock 1.1  Probably good enough for my purposes, but not ideal for the future.

TRon

  • Hero Member
  • *****
  • Posts: 2675
Re: Winsock and OS/2 Strange Issue
« Reply #12 on: February 15, 2024, 03:38:06 pm »
Yes sir, but that (StrToHostAddr) doesn't exist in the PMWsock unit (which is the Winsock unit for OS/2).
If you are bound to using PMWsock (instead of using the FPC cross-platform sockets units, mind I dunno anything about OS2 so don't know if it is supported by FPC) then you can have a look at the source-code here and make the functions available for you. These functions do not depend on external 3th party libraries, so copy-paste (perhaps with a little adjustment to make them work with your PWM library used types ) ? ).
« Last Edit: February 15, 2024, 03:39:56 pm by TRon »

Thaddy

  • Hero Member
  • *****
  • Posts: 14615
  • Sensorship about opinions does not belong here.
Re: Winsock and OS/2 Strange Issue
« Reply #13 on: February 15, 2024, 03:59:28 pm »
Cross platform:
Code: Pascal  [Select][+][-]
  1. program ntoatest;
  2. uses sockets;
  3.  
  4. Var
  5.  addr:in_addr;
  6. begin
  7.   addr.s_addr :=  16777343;
  8.   Writeln(NetAddrToStr(addr));
  9. end.
You don't need winsock for that.... (nor math  :) )
I would rewrite your example anyway in fcl-net. No need for winsock.
« Last Edit: February 15, 2024, 04:25:38 pm by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

NeddieSeagoon

  • New member
  • *
  • Posts: 8
Re: Winsock and OS/2 Strange Issue
« Reply #14 on: February 15, 2024, 04:10:57 pm »
Yeah.  Like I said, I can't get the regular sockets unit to work on OS/2.  I've tried the same code on Windows 11 and it runs fine.

PMWsock only has inet_ntoa (I've looked though the source to the unit... which mainly is a passthough for PMWsock.dll.

I'll have a look at pulling those Functions out of Socket.

It would be really nice if I could make the Sockets unit work. 

 

TinyPortal © 2005-2018