Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 10a5d27

Browse files
authored
Add files via upload
1 parent d5246f2 commit 10a5d27

File tree

68 files changed

+3060
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+3060
-0
lines changed

‎Perl/Client Server/Chat Client.pl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use strict;
2+
use warnings;
3+
use IO::Socket::INET;
4+
5+
# Server configuration
6+
my $host = 'localhost';
7+
my $port = 7171;
8+
9+
# Connect to server
10+
my $client_socket = IO::Socket::INET->new(
11+
PeerAddr => $host,
12+
PeerPort => $port,
13+
Proto => 'tcp'
14+
) or die "Cannot connect to server: $!";
15+
16+
print "Connected to server.\n";
17+
print "Enter 'exit' to quit.\n";
18+
19+
# Send and receive messages
20+
while (1) {
21+
print "Enter message: ";
22+
my $message = <STDIN>;
23+
chomp($message);
24+
last if $message eq 'exit'; # Exit loop if user enters 'exit'
25+
26+
# Send message to server
27+
$client_socket->send($message);
28+
29+
# Receive response from server
30+
my $response = "";
31+
$client_socket->recv($response, 1024);
32+
print "Server response: $response\n";
33+
}
34+
35+
# Close socket
36+
$client_socket->close();

‎Perl/Client Server/Chat Server.pl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
use strict;
3+
use warnings;
4+
use IO::Socket::INET;
5+
use threads;
6+
use Thread::Queue;
7+
8+
# Server configuration
9+
my $host = 'localhost';
10+
my $port = 7171;
11+
12+
# Shared queue for inter-thread communication
13+
my $queue = Thread::Queue->new();
14+
15+
# Subroutine to handle client connections
16+
sub handle_client {
17+
my ($client_socket) = @_;
18+
my $client_address = $client_socket->peerhost;
19+
my $client_port = $client_socket->peerport;
20+
print "Connection established with client $client_address:$client_port\n";
21+
22+
# Add client socket to the queue
23+
$queue->enqueue($client_socket);
24+
25+
# Receive and broadcast messages
26+
while (my $message = <$client_socket>) {
27+
chomp($message);
28+
last if $message eq 'exit'; # Exit loop if client sends 'exit'
29+
print "Received from $client_address:$client_port: $message\n";
30+
31+
# Broadcast message to all connected clients
32+
foreach my $sock ($queue->peek) {
33+
next if $sock == $client_socket;
34+
print $sock $message . "\n";
35+
}
36+
}
37+
38+
# Remove client socket from the queue
39+
$queue->dequeue($client_socket);
40+
close $client_socket;
41+
print "Client $client_address:$client_port disconnected\n";
42+
}
43+
44+
# Create server socket
45+
my $server_socket = IO::Socket::INET->new(
46+
LocalHost => $host,
47+
LocalPort => $port,
48+
Proto => 'tcp',
49+
Listen => 5,
50+
Reuse => 1
51+
) or die "Cannot create socket: $!";
52+
53+
print "Server started, waiting for client connection...\n";
54+
55+
# Accept client connections
56+
while (my $client_socket = $server_socket->accept()) {
57+
# Create a new thread for each client
58+
threads->create(\&handle_client, $client_socket)->detach();
59+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
use strict;
3+
use warnings;
4+
use IO::Socket::INET;
5+
6+
# Server configuration
7+
my $host = 'localhost';
8+
my $port = 7171;
9+
10+
# File to download
11+
my $filename = 'file.txt';
12+
13+
# Connect to server
14+
my $client_socket = IO::Socket::INET->new(
15+
PeerAddr => $host,
16+
PeerPort => $port,
17+
Proto => 'tcp'
18+
) or die "Cannot connect to server: $!";
19+
20+
# Send download request to server
21+
print $client_socket "download $filename\n";
22+
23+
# Open file for writing
24+
open(my $fh, '>', $filename) or die "Failed to open file: $!";
25+
26+
# Receive file content from server and write to file
27+
while (my $chunk = <$client_socket>) {
28+
print $fh $chunk;
29+
}
30+
31+
close $fh;
32+
33+
# Close socket
34+
$client_socket->close();
35+
36+
print "File downloaded successfully: $filename\n";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
use strict;
3+
use warnings;
4+
use IO::Socket::INET;
5+
6+
# Server configuration
7+
my $host = 'localhost';
8+
my $port = 7171;
9+
10+
# File to upload
11+
my $filename = 'file.txt';
12+
13+
# Connect to server
14+
my $client_socket = IO::Socket::INET->new(
15+
PeerAddr => $host,
16+
PeerPort => $port,
17+
Proto => 'tcp'
18+
) or die "Cannot connect to server: $!";
19+
20+
# Send upload request to server
21+
print $client_socket "upload $filename\n";
22+
23+
# Open file for reading
24+
open(my $fh, '<', $filename) or die "Failed to open file: $!";
25+
26+
# Read file content and send to server
27+
while (my $chunk = <$fh>) {
28+
print $client_socket $chunk;
29+
}
30+
31+
close $fh;
32+
33+
# Receive response from server
34+
my $response = <$client_socket>;
35+
chomp($response);
36+
print "Server response: $response\n";
37+
38+
# Close socket
39+
$client_socket->close();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
use strict;
3+
use warnings;
4+
use IO::Socket::INET;
5+
6+
# Server configuration
7+
my $host = 'localhost';
8+
my $port = 7171;
9+
my $upload_dir = 'uploads';
10+
11+
# Create upload directory if it doesn't exist
12+
mkdir $upload_dir unless -d $upload_dir;
13+
14+
# Subroutine to handle file upload
15+
sub handle_upload {
16+
my ($client_socket, $filename) = @_;
17+
my $file_path = "$upload_dir/$filename";
18+
19+
# Open file for writing
20+
open(my $fh, '>', $file_path) or return "Failed to open file: $!";
21+
22+
# Receive file content from client and write to file
23+
while (my $chunk = <$client_socket>) {
24+
print $fh $chunk;
25+
}
26+
27+
close $fh;
28+
return "File uploaded successfully: $file_path";
29+
}
30+
31+
# Subroutine to handle file download
32+
sub handle_download {
33+
my ($client_socket, $filename) = @_;
34+
my $file_path = "$upload_dir/$filename";
35+
36+
# Open file for reading
37+
open(my $fh, '<', $file_path) or return "Failed to open file: $!";
38+
39+
# Read file content and send to client
40+
while (my $chunk = <$fh>) {
41+
print $client_socket $chunk;
42+
}
43+
44+
close $fh;
45+
return "File downloaded successfully: $file_path";
46+
}
47+
48+
# Create server socket
49+
my $server_socket = IO::Socket::INET->new(
50+
LocalHost => $host,
51+
LocalPort => $port,
52+
Proto => 'tcp',
53+
Listen => 5,
54+
Reuse => 1
55+
) or die "Cannot create socket: $!";
56+
57+
print "File Transfer Server started, waiting for client connections...\n";
58+
59+
# Accept client connections
60+
while (my $client_socket = $server_socket->accept()) {
61+
my $client_address = $client_socket->peerhost;
62+
my $client_port = $client_socket->peerport;
63+
print "Connection established with client $client_address:$client_port\n";
64+
65+
# Receive client request (upload or download)
66+
my $request = <$client_socket>;
67+
chomp($request);
68+
my ($action, $filename) = split(/\s+/, $request);
69+
70+
my $response;
71+
if ($action eq 'upload') {
72+
$response = handle_upload($client_socket, $filename);
73+
} elsif ($action eq 'download') {
74+
$response = handle_download($client_socket, $filename);
75+
} else {
76+
$response = "Invalid action";
77+
}
78+
79+
# Send response to client
80+
print $client_socket $response . "\n";
81+
82+
# Close client socket
83+
close $client_socket;
84+
print "Client $client_address:$client_port disconnected\n";
85+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
use strict;
3+
use warnings;
4+
use IO::Socket::INET;
5+
6+
my $host = 'localhost';
7+
my $port = 8080;
8+
my $doc_root = '/path/to/htdocs';
9+
10+
my $server = IO::Socket::INET->new(
11+
LocalHost => $host,
12+
LocalPort => $port,
13+
Proto => 'tcp',
14+
Listen => 5,
15+
Reuse => 1
16+
) or die "Cannot create socket: $!";
17+
18+
print "HTTP Server started at http://$host:$port/\n";
19+
20+
while (my $client = $server->accept()) {
21+
my $request = <$client>;
22+
my ($method, $uri, $version) = split ' ', $request;
23+
my $path = "$doc_root$uri";
24+
25+
if (-f $path) {
26+
open my $file, '<', $path;
27+
print $client "HTTP/1.1 200 OK\r\n";
28+
print $client "Content-Type: text/html\r\n\r\n";
29+
print $client $_ while <$file>;
30+
} else {
31+
print $client "HTTP/1.1 404 Not Found\r\n";
32+
print $client "Content-Type: text/plain\r\n\r\n";
33+
print $client "404 Not Found - The requested resource was not found on this server.\n";
34+
}
35+
36+
close $client;
37+
}

‎Perl/Client Server/UDP Client.pl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
use strict;
3+
use warnings;
4+
use IO::Socket::INET;
5+
6+
my $host = 'localhost';
7+
my $port = 7171;
8+
9+
my $client_socket = IO::Socket::INET->new(
10+
PeerAddr => $host,
11+
PeerPort => $port,
12+
Proto => 'udp'
13+
) or die "Cannot connect to server: $!";
14+
15+
my $message = "Hello from UDP client!";
16+
$client_socket->send($message);
17+
print "Sent message to server: $message\n";

‎Perl/Client Server/UDP Server.pl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
use strict;
3+
use warnings;
4+
use IO::Socket::INET;
5+
6+
my $host = 'localhost';
7+
my $port = 7171;
8+
9+
my $server_socket = IO::Socket::INET->new(
10+
LocalHost => $host,
11+
LocalPort => $port,
12+
Proto => 'udp'
13+
) or die "Cannot create socket: $!";
14+
15+
print "UDP Server started, waiting for client messages...\n";
16+
17+
while (1) {
18+
my $data;
19+
$server_socket->recv($data, 1024);
20+
print "Received from client: $data\n";
21+
}

‎Perl/Client Server/client.pl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
use strict;
3+
use warnings;
4+
use IO::Socket;
5+
6+
# Autoflush
7+
local $| = 1;
8+
9+
# Define server details
10+
my $hostname = 'localhost';
11+
my $port = 7171;
12+
13+
# Create socket
14+
my $socket = IO::Socket::INET->new(
15+
PeerAddr => $hostname,
16+
PeerPort => $port,
17+
Proto => 'tcp',
18+
Timeout => 1
19+
) or die "Could not connect: $!";
20+
21+
# Main loop for sending data
22+
while (1) {
23+
print "\nEnter the data to send: ";
24+
my $send_data = <STDIN>;
25+
chomp($send_data);
26+
if ($send_data) {
27+
my $size = $socket->send($send_data);
28+
print "\nSent data of length: $size\n";
29+
30+
# Receive response from server
31+
my $response;
32+
$socket->recv($response, 1024);
33+
print "Got back: $response\n";
34+
}
35+
}
36+
$socket->close(); # Close socket

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /