Download presentation
Presentation is loading. Please wait.
Published byCameron Hubbard Modified over 8 years ago
1
Using TCP sockets in Perl Created by M Bateman, A Ruddle & C Allison As part of the TCP View project
2
Overview TCP socket Client/Server Multithread server Thread pooling server Alternate thread pooling server
3
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
4
TCP Socket Operations
5
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
6
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”); }
7
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 = ) { print ($line); } Close (SOCK);
8
Issues Can only deal with one request at once Performance –Have to wait until the guy in front has finished with the server
9
Multithreading
10
Issues Performance –Thread creation at client connection –Creating threads takes time Possible deadlock –Due to multithreaded
11
Issues Possible deadlock Resource thrashing Concurrency errors Thread leakage Request overload Performance –Thread creation still has to be performed in the main loop
12
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
13
Summary
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.