Broken Pipe in Network Programming

J

janicehwang1325

Hi,

I have server running on localhost and a client is connected to it from
the same station, which is the localhost. However, when i try to
connect another client to the server, it gives me Broken pipe error in
the second client and the server side prompt that there is a
segmentation fault (core dumped). What is the cause of this problem and
how can i solve it?
 
A

Anno Siegel

Hi,

I have server running on localhost and a client is connected to it from
the same station, which is the localhost. However, when i try to
connect another client to the server, it gives me Broken pipe error in
the second client and the server side prompt that there is a
segmentation fault (core dumped). What is the cause of this problem and
how can i solve it?

You must be joking! Show your code.

Anno
 
J

janicehwang1325

here my server code:

use threads;

#!/usr/bin/perl

use POSIX ":sys_wait_h";
use IO::Socket::SSL;
use IO::Handle;
use Time::Local;
use DBI;

my ($sock, $s, $v_mode);

unless (@ARGV == 1) { die "usage: perl $0 <Port Number>" };
($port) = @ARGV;

# Check to make sure that we were not accidentally run in the wrong^M
# directory:^M
unless (-d "certs") {
if (-d "../certs") {
chdir "..";
} else {
die "No CA Certs\n";
}
}

if(!($sock = IO::Socket::SSL->new( Listen => 10,
LocalPort => $port,
Proto => 'tcp',
ReuseAddr => 1,
SSL_verify_mode => 0x01,
SSL_use_cert => 1,
SSL_passwd_cb => sub {return
"haha"},
SSL_key_file =>
'certs/server_key.pem',
)) ) {
warn "unable to create socket: ", &IO::Socket::SSL::errstr, "\n";
exit(0);

}
warn "SSL socket created: $sock.\n";

#run as daemon
chdir '/' or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";

warn "waiting for next connection......\n";

sub handle_connection{
$dbh = DBI->connect("DBI:mysql:testing", "root",
'!@#zxc'),{RaiseError => 1, AutoCommit => 0} || die ("Cannot connect to
database");
$outpath = "/home/extol/heartbeat_testing/alert.log";
$s = shift;
$output = shift || $s;
$exit = 0;

my ($peer_cert, $subject_name, $issuer_name, $date, $str);

if( ! $s ) {
warn "error: ", $s->errstr, "\n";
$exit = 1;
break;
}
while(<$s>){
if( ref($s) eq "IO::Socket::SSL" && $s->connected()) {
$subject_name =
$s->peer_certificate("subject");
$issuer_name = $s->peer_certificate("issuer");
if($_ =~ /MRTG ([\d.]+) (\w+) (\d+) (\d+) (\d+)
(\d+) (\d+) (\d+)/)
{
# here the program wil process the
log and save into database
...................
}
else{
$exit = 1;
}
last if $exit;
}
$dbh->disconnect;
}

while(1){
while(($s = $sock->accept())) {
$s->autoflush(1);
$thread = threads->create(\&handle_connection, $s);
$id = $thread->tid();
print "the thread id is $id\n";
$thread->detach;

}
}
 
M

Mumia W.

here my server code:
[...]

#run as daemon
chdir '/' or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
[...]

You should close these file handles before you open them with different
targets.
 
B

Ben Morrow

Quoth "Mumia W. said:
here my server code:
[...]

#run as daemon
chdir '/' or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
[...]

You should close these file handles before you open them with different
targets.

Unless you're particularly concerned to catch errors from close, you
don't need to. Perl will close them for you (and drop any error on the
floor).

Ben
 
T

Ted Zlatanov

here my server code:

use threads;

#!/usr/bin/perl

Are you sure this is your code? The first two lines should be
switched unless you just don't intend to run directly from the command
line. Also, the whole thing doesn't compile. Try posting a simple
example that illustrates the error, not snippets from your source
code. Then it's much easier to help you.

The threads library, in my experience, can cause strange errors. Try
a test without it and make sure things work there. The
IO::Socket::SSL module, for example, may not be thread-safe (I don't
know this, I'm giving an example of why threads should be disabled to
make sure things work without them).

Also, try using IO::Socket without SSL. Does that work?

Generally with debugging you want to start with a simple situation and
work your way up to what breaks, not start with a complex situation
and try to find the bug(s).

Ted
 
X

xhoster

Hi,

I have server running on localhost and a client is connected to it from
the same station, which is the localhost. However, when i try to
connect another client to the server, it gives me Broken pipe error in
the second client and the server side prompt that there is a
segmentation fault (core dumped). What is the cause of this problem and
how can i solve it?

You have a bug in line 23 of the server, and another in line 17 of the
client.

Xho
 
X

xhoster

here my server code:

use threads;

#!/usr/bin/perl

use POSIX ":sys_wait_h";
use IO::Socket::SSL;
use IO::Handle;
use Time::Local;
use DBI;

No use strict?


Why use threads rather than forking? (Especially since you are mixing fork
with threads?)

Xho
 
M

Mumia W.

Ben said:
Unless you're particularly concerned to catch errors from close, you
don't need to. Perl will close them for you (and drop any error on the
floor).

Ben

Oh, thanks. I learned something today.
 
J

janicehwang1325

I tried to program with preforking server and it works well for both
IO::Socket as well as IO::Socket::SSL. However, due to my supervisor's
request, he wants me to do it with threads. I did debug the program
from the very beginning using a simple server and client and it works
perfectly.

As a result for my debugging yesterday, i found out that if i lock or
using threads->join for my server program, the second client will wait
till the first client to finish. That's why i am wondering could it be
the problem of the database? since when i try to run the first client
until there is nothing to send to server from that client, then i run
the second one and it works! I wondering the inserting data into the
database cause the problem.
 
T

Ted Zlatanov

I tried to program with preforking server and it works well for both
IO::Socket as well as IO::Socket::SSL. However, due to my supervisor's
request, he wants me to do it with threads. I did debug the program
from the very beginning using a simple server and client and it works
perfectly.

As a result for my debugging yesterday, i found out that if i lock or
using threads->join for my server program, the second client will wait
till the first client to finish. That's why i am wondering could it be
the problem of the database? since when i try to run the first client
until there is nothing to send to server from that client, then i run
the second one and it works! I wondering the inserting data into the
database cause the problem.

OK, does IO::Socket work with threads without SSL?

With threads but without database interaction, do things work? (Try
writing the data to a file instead of the database.)

Keep eliminating variables. The simplest thing that fails is the
easiest thing to debug.

Without seeing your code, we'll keep giving you generic advice like
the above.

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,530
Members
48,251
Latest member
Amelia8778

Latest Threads

Top