130 likes | 248 Views
U sing TCP sockets in Perl. Created by M Bateman, A Ruddle & C Allison As part of the TCP View project. O verview. TCP socket Client/Server Multithread server Thread pooling server Alternate thread pooling server. T CP Provides. Process to process communication
E N D
Using TCP sockets in Perl Created by M Bateman, A Ruddle & C Allison As part of the TCP View project
Overview • TCP socket • Client/Server • Multithread server • Thread pooling server • Alternate thread pooling server
TCP Provides • Process to process communication • Use tuple of IP address & port • Reliable • In order • Socket is one end-point of a two way connection link
TCP in Perl • Implemented in Socket package • Provides abstractions over • Socket and connect operations • Bind, listen & accept • Remember to close the socket at the end
Daytime server use Socket; $port = 13; $proto = getprotobyname (‘tcp’); socket (SERVER, PF_INET, SOCK_STREAM, $proto); setsockopt (Server, SOL_SOCKET, SO_REUSEADDR, pack (“1”, 1)); bind (SEVER, socket_in ($port, INADDR_ANY)) listen (SERVER, SOMAXCONN); for (; $paddr = accept (CLIENT, SERVER); close CLIENT) { print (CLIENT scalar localtime . “\n”); }
Daytime client use Socket; $port = 13; $remote = $ARGV[0]; $iaddr = inet_aton ($remote); $paddr = sockaddr_in ($port, $iaddr); $proto = getprotobyname (‘tcp’); socket (SOCK, PF_INET, SOCK_STREAM, $proto); connect (SOCK, $paddr); while ($line = <SOCK>) { print ($line); } Close (SOCK);
Issues • Can only deal with one request at once • Performance • Have to wait until the guy in front has finished with the server
Issues • Performance • Thread creation at client connection • Creating threads takes time • Possible deadlock • Due to multithreaded
Issues • Possible deadlock • Resource thrashing • Concurrency errors • Thread leakage • Request overload • Performance • Thread creation still has to be performed in the main loop
Pool Alternative Implementation • Create pool as before • Create thread which makes sure the pool has n thread waiting • On connection take thread from the pool • The above thread makes sure there are always threads in the pool waiting to be used