The simplest Winsock program written with Pascal

zhaozj2021-02-08  284

I have rewritten the first example of the Socket program tutorial, and then reverse it. It is actually directly calling the Win API to compile DCC32 -CC Server1.PAS with Windows Telnet Test Server1 with the command line. .pas: program server1; uses windows, winsock; const myport = 1414; // predefined port {type sockaddr_in = record case Integer of 0: (sin_family: u_short; sin_port: u_short; sin_addr: TInAddr; sin_zero: array [0. .7] of char); 1: (sa_family: u_short; sa_data: array [0..13] of char) end; tsockaddrin = sockaddr_in; psockaddr = ^ TsockAddr; tsockaddr = sockaddr_in; wsadata = record /// !!! Also WSDATA wVersion: Word; wHighVersion: Word; szDescription: array [0..WSADESCRIPTION_LEN] of Char; szSystemStatus: array [0..WSASYS_STATUS_LEN] of Char; iMaxSockets: Word; iMaxUdpDg: Word; lpVendorInfo: PChar; end; TWSAData = WSAData End} var str: string; s, ns: tsocket; // type: u_int | integer; ver: word; recE: twsadata; // type: wsadata; wsstatus: integer; // winsocket_status; i, pktlen: integer; SZD: array [0..wsadescription_len] of char; szs: array [0..wsasys_status_len] of char; server: tsockaddr; // TPYE: SOCKADDR_IN Client: PSockAddr; // Type: ^ SockAddr_in Namelen: Pinteger; // Type: ^ Integer; BUF: Array [0..1023] of char; becom: = 514; Write ('wsastartup =') TrywsStatus: = WSAStartup (Ver, Rece); // Initializing Winsock; Under Windows is a must-have WriteLn (WSSTATUS); // Winsock State, 0 represents Success IF WSSTATUS = 0 ThenbeginWriteln; WriteLn ('***** *********************************************************** ************************* '); // The following shows the Winsock Status Write (' wversion = '); WriteLn (Rece.WVersion); Write ( 'wHighVersion ='); writeln (rece.wHighVersion); write ( 'iMaxSockets ='); writeln (rece.iMaxSockets); write ( 'iMaxUdpDg ='); writeln (rece.iMaxUdpDg); writeln ( 'szDescription is' ); for i: =

0 to WSADESCRIPTION_LEN DO WRITE (SZD [I]); Writeln; Writeln ('szsystemStatus is'); for i: = 0 to WSASYS_STATUS_LEN DO WRITE (SZS [I]); Writeln; WriteLn (' ******* *********************************************************** *********************** '); End; Write (' socket = '); s: = socket (AF_INET, SOCK_STREAM, 0); // The first step is to establish a server's socket, 0 means successful Writeln (s); if s <0 THEN EXIT; WRITE ('bind ='); server.sin_family: = af_inet; server.sin_port: = htons (myport); server .SIN_ADDR.S_ADDR: = INADDR_AD (WSSTATUS: = BIND (S, Server, SIZEOF (Server)); // Step 2, Binding, 0 means successful WriteLn (WSSTATUS); if WSSTATUS <> 0 THEN EXIT; WRITE 'Listen ='); WSSTATUS: = Listen (S, 5); // Step 3, listening to WriteLn (WSSTATUS); if WSSTATUS <> 0 THEN EXIT; WRITE ('accept ='); new (client); New (Namelen); Namelen ^: = SIZEOF (Client ^); ns: = accept (s, client, namelen); // fourth step, waiting to receive // ​​Note: Bind is the data of the SockAddr_in structure, accept It is two pointers !!! if ns = -1 then // below can be made into multi-threaded procedure begin writeln (-1); End else begin Writeln (ns); pktlen: = 0; Writeln ('RECV') Repeat pktlen: = RECV (NS, BUF, 1024, 0); // Receive data // pktlen = to receive length, 0 means broken joint, <0 means failing SLEEP (1); WRI TE (pktlen: 5); if pktlen> 0 The begin i: = send (ns, buf, pktlen, 0); // Return to data END; Until Pktlen <1; // Loop until the client is disconnected END; FINALLIWRITELN; Writeln ('***************************************************** ************************); Writeln ('Pressto EXIT WINSOCKET');

READLN (STR);

WSSTATUS: = wsacleanup (); // Exit Winsocket;

Write ('wsacleanup =');

Writeln (WSSTATUS);

END;

Writeln;

Writeln ('Press

TO EXIT ');

READLN (STR);

End.

Supplement date: 2000-07-22 10:37:10

转载请注明原文地址:https://www.9cbs.com/read-1400.html

New Post(0)