forked from nchowning/multinotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multinotify-server.pl
126 lines (110 loc) · 3.5 KB
/
multinotify-server.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use warnings;
use IO::Socket::INET;
use Thread;
# Flush after every write
$| = 1;
my ($socket,$client_socket);
my ($peeraddress,$peerport);
our $message_check = 0;
our @message_list;
# Socket creation
$socket = new IO::Socket::INET (
LocalHost => 'IP ADDRESS IN HERE',
LocalPort => 'PORT IN HERE',
Proto => 'tcp',
Listen => 5,
Reuse => 1
) or die "Error in Socket Creation : $!\n";
print "Listening for connections\n\n";
while(1) # To infinity and beyond!
{
$client_socket = $socket->accept();
$buf = <$client_socket>;
if ($buf)
{
# Variables to hold the client's IP address and port
$peeraddress = $client_socket->peerhost();
$peerport = $client_socket->peerport();
print "Connection to: $peeraddress:$peerport established.\n\n";
# Check to see what the client is sending.
# If it's sending "send", call the &sendmessage subroutine.
# Otherwise call the &recievemessage subroutine.
if ($buf eq "send\n")
{
&sendmessage;
# If $receiver has a defined value, a receiving client
# is connected in which case we need to call &receivemessage
# to send the new message onto the receiving client.
if(defined($receiver))
{
$message_check = 1;
&receivemessage;
}
else
{
# Send to Prowl... eventually
}
}
else
{
# If the client did not send "send" to the socket, it is a
# receiving client. Set the $receiver variable and call the
# &receivemessage subroutine.
$receiver = $client_socket;
new Thread \&receivemessage;
}
}
# The client has closed the socket.
else
{
# If $receiver is set, the receiving client is connected to the server.
if(defined($receiver))
{
# Check to see if the disconnecting client is the receiving client.
# If it is, undefine the $receiver variable.
if($client_socket eq $receiver)
{
undef $receiver
}
}
# Remove the client from $read_set and close it.
print "Client disconnected.\n\n";
close($client_socket);
}
}
sub sendmessage
{
print "Send request from client $peeraddress:$peerport\n";
# Send an approval message to the sending client
$data = "approved";
print $client_socket "$data\n";
# Receive the username from the client and store it in $username
$username = <$client_socket>;
print "Username: $username";
push (@message_list, $username);
# Send the value "un" to the client to verify that the username was received
$data = "un\n";
print $client_socket "$data\n";
# Receive the message from the client and store it in $message
$message = <$client_socket>;
print "Message: $message\n";
push (@message_list, $message);
}
sub receivemessage
{
# If the variable $username is defined, a message has been sent
if ($message_check == 1)
{
# Send the username to the client
print $receiver "$message_list[0]";
# If the server received "thanks" form the receiving client proceed to
# send the message to the client
if (<$receiver> eq "thanks\n")
{
print $receiver "$message_list[1]";
}
@message_list = ();
# Undefine the $username variable
$message_check = 0;
}
}