unit Winsock_TCP_S_C;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, buffer_record , WinSock;
type net_structure =
record
S : tSocket;
S_server_accepting : tSocket;
s_port : u_short;
c_port : u_short;
s_addr : sockaddr_in;
c_addr : sockaddr_in;
buffer_rcv : buffer;
buffer_snd : buffer;
server_address_string : PChar;
client_address_string : PChar;
Error : DWORD;
data_send: integer;
data_recived : integer;
end;
type TNet =
class(TObject)
private
//<---------------------------------------------------------------------------->
net_s_client : net_structure;
net_s_server : net_structure;
//<---------------------------------------------------------------------------->
function WinSock_bind_server_socket_TCP :boolean;
function Winsock_setopt_TCP(is_server: boolean) :boolean;
function WinSock_startup(is_server: boolean) :boolean;
function Winsock_openTCP(is_server: boolean) :boolean;
function Winsock_accept_server_socket_TCP :boolean;
function Winsock_listen_server_socket_TCP :boolean;
function Winsock_ioctlsocket(sock:tSocket;argp:u_long;is_server:boolean) : boolean;
function Winsock_client_connectTCP : boolean;
function Winsock_reciveTCP(is_server: boolean) :integer;
function Winsock_sendTCP(is_server: boolean) : integer;
function Winsock_closeTCP(is_server: boolean) :boolean;
function Winsock_cleanup(is_server: boolean) : boolean;
function transfer_length(Data_to_compute:buffer):integer;
public
//<---------------------------------------------------------------------------->
//<---------------------------------------------------------------------------->
constructor create;
function StartNetwork(is_server: boolean) : boolean;
function StopNetwork(is_server: boolean) : boolean;
function Winsock_load(is_server: boolean) : boolean;
function Winsock_unload(is_server: boolean) : boolean;
function Server_Change_Port(new_port: u_short) :boolean;
function Server_Listen : boolean;
function Server_Accepting : boolean;
function Server_NonBlocking: Boolean;
function Client_NonBlocking: Boolean;
function Client_Fill_ServerAddrPort(server_addr:PChar; server_port: u_short) :boolean;
function Client_ConnectToServer : boolean;
function FillBufferToSend(is_server : boolean ;BufferToSend: buffer): boolean;
function GetRecivedBuffer(is_server : boolean):buffer;
function Recive(is_server : boolean) : integer;
function Snd(is_server : boolean) : integer;
function GetSocketError(is_server : boolean):DWORD;
function GetServerPort(is_server : boolean) : u_short;
function GetClientPort(is_server : boolean) : u_short;
function GetServerIP (is_server : boolean) : PChar;
function GetClientIP (is_server : boolean) : PChar;
protected
//<---------------------------------------------------------------------------->
//<---------------------------------------------------------------------------->
end;
implementation
//<---------------------------------------------------------------------------->
// TNet: private
//<---------------------------------------------------------------------------->
function TNet.WinSock_startup(is_server:boolean):boolean;
var wsa: TWSAData;
c : integer;
begin
if is_server = true
then
begin
c:= WSAStartup(MAKEWORD(2, 2), wsa);
if c <> 0
then
begin
net_s_server.Error:= WSAGetLastError;
result :=false;
end
else result:=true;
end
else
begin
c:= WSAStartup(MAKEWORD(2, 2), wsa);
if c <> 0
then
begin
net_s_client.Error:= WSAGetLastError;
result:= false;
end
else result:= true;
end
end;
function TNet.Winsock_openTCP(is_server: boolean):boolean;
begin
if is_server = true
then
begin
net_s_server.S:= socket (AF_INET
{TCP/UDP INTERNETWORK Protokolle}, SOCK_STREAM
{SOCK_STREM: Voraussetzung für TCP}, 0
{TCP});
if net_s_server.S = INVALID_SOCKET
then
begin
net_s_server.Error:= WSAGetLastError;
result :=false;
end
else result := true;
end
else
begin
net_s_client.S := socket (AF_INET
{TCP/UDP INTERNETWORK Protokolle},
SOCK_STREAM
{SOCK_STREM: Voraussetzung für TCP}, 0
{TCP});
if net_s_client.S = INVALID_SOCKET
then
begin
net_s_client.Error:= WSAGetLastError;
result :=false;
end
else result := true;
end
end;
function TNet.Winsock_setopt_TCP(is_server: boolean):boolean;
begin
if is_server = true
then
begin
result := true;
try
net_s_server.s_addr.sin_family := AF_INET;
net_s_server.s_addr.sin_port := htons(net_s_server.s_port);
net_s_server.s_addr.sin_addr.S_addr := htonl(INADDR_ANY);
net_s_server.s_addr.sin_zero:='
0';
except
begin
result:= false;
end
end
end
else
begin
result:= true;
try
net_s_client.c_addr.sin_family := AF_INET;
net_s_client.c_addr.sin_addr.S_addr:= htonl(INADDR_ANY);
net_s_client.c_addr.sin_zero:='
0';
except
begin
result:=false;
end
end
end
end;
function TNet.WinSock_bind_server_socket_TCP:boolean;
var c : integer;
begin
c:= bind (net_s_server.S , net_s_server.s_addr , sizeof(net_s_server.s_addr));
if c <> 0
then
begin
net_s_server.Error:=WSAGetLastError;
result := false;
end
else result := true;
end;
function TNet.Winsock_listen_server_socket_TCP:boolean;
var c : integer;
begin
c:= listen(net_s_server.S , SOMAXCONN);
if c <> 0
then
begin
net_s_server.Error:=WSAGetLastError;
result:= false;
end
else result := true;
end;
function TNet.Winsock_client_connectTCP:boolean;
var c: integer;
begin
net_s_client.s_addr.sin_family := AF_INET;
net_s_client.s_addr.sin_port := htons(net_s_client.s_port);
net_s_client.s_addr.sin_addr.S_addr:= inet_addr(net_s_client.server_address_string);
c:= connect(net_s_client.S, net_s_client.s_addr, sizeof(net_s_client.s_addr));
if c <> 0
then
begin
net_s_client.Error:=WSAGetLastError;
result:= false;
end
else result := true;
end;
function TNet.Winsock_accept_server_socket_TCP:boolean;
begin
net_s_server.S_server_accepting:= accept(net_s_server.S,
nil ,
nil);
if net_s_server.S_server_accepting = INVALID_SOCKET
then
begin
net_s_server.Error:=WSAGetLastError;
result:= false;
end
else result:= true;
end;
function TNet.Winsock_reciveTCP(is_server: boolean) :integer;
begin
if is_server = true
then
begin
net_s_server.data_recived:= recv( net_s_server.S_server_accepting ,
net_s_server.buffer_rcv , transfer_length(net_s_server.buffer_rcv) , 0);
if net_s_server.data_recived > 0
then
begin
result:=net_s_server.data_recived;
end
else if net_s_server.data_recived <= 0
then
begin
net_s_server.Error:=WSAGetLastError;
result:= -1 ;
end
end
else
begin
net_s_client.data_recived:= recv( net_s_client.s ,
net_s_client.buffer_rcv , transfer_length(net_s_client.buffer_rcv) , 0);
if net_s_client.data_recived > 0
then
begin
result:=net_s_client.data_recived;
end
else if net_s_client.data_recived <= 0
then
begin
net_s_client.Error:=WSAGetLastError;
result:= -1 ;
end
end
end;
function TNet.Winsock_sendTCP(is_server: boolean) : integer;
begin
if is_server = true
then
begin
net_s_server.data_send:= send(net_s_server.S_server_accepting,
net_s_server.buffer_snd, transfer_length(net_s_server.buffer_snd) , 0 );
if net_s_server.data_send > 0
then
begin
result:=net_s_server.data_send;
end
else if net_s_server.data_send <= 0
then
begin
net_s_server.Error:= WSAGetLastError;
result:= -1 ;
end
end
else
begin
net_s_client.data_send:= send( net_s_client.S ,
net_s_client.buffer_snd , transfer_length(net_s_client.buffer_snd) , 0 );
if net_s_client.data_send > 0
then
begin
result:= net_s_client.data_send;
end
else if net_s_server.data_send <= 0
then
begin
net_s_server.Error:= WSAGetLastError;
result:= -1 ;
end
end
end;
function TNet.Winsock_closeTCP(is_server: boolean):boolean;
var c,d : integer;
begin
if is_server = true
then
begin
c := closesocket( net_s_server.S_server_accepting );
d := closesocket( net_s_server.S );
if c <> -1
then
begin
net_s_server.Error:=WSAGetLastError;
result := false;
end
else if d <> 0
then
begin
net_s_server.Error:=WSAGetLastError;
result := false;
end
else result := true;
end
else
c := closesocket ( net_s_client.S );
if c <> -1
then
begin
net_s_client.Error:=WSAGetLastError;
result:= false;
end
else result := true;
end;
function TNet.Winsock_cleanup(is_server: boolean) : boolean;
var c : integer;
begin
if is_server = true
then
begin
c:=WSACleanup;
if c <> 0
then
begin
net_s_server.Error:= WSAGetLastError;
result := false;
end
else
begin
c:=WSACleanup;
if c <> 0
then
begin
net_s_client.Error:= WSAGetLastError;
result:= false;
end
else result := true;
end
end
end;
function TNet.Winsock_ioctlsocket(sock:tSocket;argp:u_long;is_server:boolean) : boolean;
var c : integer;
begin
if is_server = true
then
begin
c:=ioctlsocket(sock,FIONBIO,argp);
if c <> 0
then
begin
net_s_server.Error:= WSAGetLastError;
result := false;
end
else
begin
c:=ioctlsocket(sock,FIONBIO,argp);
if c <> 0
then
begin
net_s_client.Error:= WSAGetLastError;
result:= false;
end
else result := true;
end
end
end;
function Tnet.transfer_length(Data_to_compute:buffer):integer;
var length_off:integer;
begin
length_off:=sizeof(data_to_compute.proto_cmd);
length_off:=length_off+length(data_to_compute.str);
result:=length_off;
end;
//<---------------------------------------------------------------------------->
//<---------------------------------------------------------------------------->
// TNet: public
//<---------------------------------------------------------------------------->
constructor TNet.create;
begin
inherited create;
net_s_client.s_port:=10000;
net_s_client.c_port:=10000;
net_s_client.server_address_string:='
127.0.0.1';
net_s_client.client_address_string:='
127.0.0.1';
net_s_server.s_port:=10000;
net_s_client.c_port:=10000;
net_s_server.server_address_string:='
127.0.0.1';
net_s_server.client_address_string:='
127.0.0.1';
end;
function TNet.Winsock_load(is_server : boolean): boolean;
begin
result := Winsock_startup(is_server);
end;
function TNet.Winsock_unload(is_server : boolean): boolean;
begin
result := Winsock_cleanup(is_server);
end;
function TNet.StartNetwork(is_server: boolean): boolean;
begin
if Winsock_openTCP(is_server) = true
then
if Winsock_setopt_TCP(is_server) = true
then
begin
if is_server = true
then
if WinSock_bind_server_socket_TCP = true
then result := true;
end
else result:= true;
end;
function TNet.StopNetwork(is_server:boolean):boolean;
begin
result := Winsock_closeTCP(is_server);
end;
function TNet.Server_Change_Port(new_port: u_short):boolean;
var t : boolean;
begin
t:= false;
if StopNetwork(true) = true
then
begin
t:= true;
try
net_s_server.s_port := new_port;
except t:= false;
end
end;
if t = true
then
begin
result:= StartNetwork(true)
end
else result := false;
end;
function TNet.Server_Listen: boolean;
begin
Winsock_listen_server_socket_TCP;
result := Winsock_listen_server_socket_TCP;
end;
function TNet.Server_Accepting: boolean;
begin
result:= Winsock_accept_server_socket_TCP;
end;
function TNet.Client_Fill_ServerAddrPort(server_addr : PChar; server_port: u_short):boolean;
begin
result:= true;
try
net_s_client.server_address_string := server_addr;
net_s_client.s_port := server_port;
except result := false;
end;
end;
function TNet.Client_ConnectToServer:boolean;
begin
result := Winsock_client_connectTCP;
end;
function TNet.FillBufferToSend(is_server : boolean ; BufferToSend: buffer): boolean;
begin
if is_server = true
then
begin
result:= true;
try
net_s_server.buffer_snd := BufferToSend;
except result := false;
end
end
else
begin
result:=true;
try
net_s_client.buffer_snd := BufferToSend;
except result := false;
end
end
end;
function TNet.GetRecivedBuffer(is_server : boolean):buffer;
begin
if is_server = true
then
begin
result := net_s_server.buffer_rcv;
end
else
begin
result := net_s_client.buffer_rcv;
end
end;
function TNet.Recive(is_server : boolean) : integer;
begin
result:=Winsock_reciveTCP(is_server);
end;
function TNet.Snd(is_server : boolean) : integer;
begin
result:=Winsock_sendTCP(is_server);
end;
function TNet.GetSocketError(is_server : boolean):DWORD;
begin
if is_server = true
then
begin
result:=net_s_server.Error;
end
else
begin
result:=net_s_client.Error;
end
end;
function TNet.GetServerPort(is_server : boolean): u_short;
begin
if is_server = true
then
begin
result := ntohs(net_s_server.s_addr.sin_port);
end
else
begin
result := ntohs(net_s_client.s_addr.sin_port);
end
end;
function TNet.GetClientPort(is_server : boolean): u_short;
begin
if is_server = true
then
begin
result := ntohs(net_s_server.c_addr.sin_port);
end
else
begin
result := ntohs(net_s_client.c_addr.sin_port);
end
end;
function TNet.GetServerIP(is_server : boolean) : PChar;
begin
if is_server = true
then
begin
result:=net_s_server.server_address_string;
end
else
begin
result:=net_s_client.server_address_string;
end
end;
function TNet.GetClientIP(is_server: boolean): PChar;
begin
if is_server = true
then
begin
result:=net_s_server.server_address_string;
end
else
begin
result:=net_s_client.client_address_string;
end
end;
function TNet.Server_NonBlocking: Boolean;
var argp: u_long;
begin
argp:=1;
result:=Winsock_ioctlsocket(net_s_server.S_server_accepting,argp,true);
end;
function TNet.Client_NonBlocking: Boolean;
var argp: u_long;
begin
argp:=1;
result:=Winsock_ioctlsocket(net_s_client.S,argp,true);
end;
//<---------------------------------------------------------------------------->
end.