Well, I cant figure out how to get a line into a variable from a socket, and do some checking on it. Its this piece of code iam troubled with:
print $client "username:"; $login=<$client>; print $login;
Ignore the last code, since I first have to figure out this line to fix the others :) I need the user to type down a username, and put it in the variable $login. Then the script does some checking on it. Doesnt seem to work though.. :( This is the entire script as it is now :
#!/bin/perl use IO::Socket; use IO::Select; $nconnections_new = 0; $login = ""; $maxconn = SOMAXONN; $local=IO::Socket::INET->new(Proto=>"tcp", LocalPort=>"1234", Listen=> +$maxconn, Reuse=>1) or die "Can't open socket\n"; while ($client = $local->accept) { $client->autoflush(1); &start; sub start { print $client "This is a test BBS!\n\r"; print $client "(C)reate new user (L)ogin"; print $client "\n\r"; $msg = ""; while ( <$client>) { next unless /\S/; if (/c|C/i) { print $client "<<<Create new user>>>\n\r"; print $client "Login must be at least 6 characters.\n\r"; &login; } elsif (/l|L/i) { &logon; } } sub login { print $client "username:"; $login=<$client>; print $login; chop $login; if ($login eq "") { print $client "Must enter a username!\n\r"; &login; } open (FILE1, "users.txt") || print "Error occured while opening file"; while (<FILE1>) { ($first, $second ) = split /:/; if ($login eq $first) { do { print $client "Sorry this username has been taken, try again!\n\r"; &login; } while ($login eq $first); } } if (length($login) > 6) { print $client "Your username was longer than 6 characters!\n\r"; &login; } close (FILE1); $pass = password(); sub password { print $client "Password:"; $password=<STDIN>; chop $password; print $client "\n\rRe-enter password:"; $password2=<STDIN>; chop $password2; if ($password ne $password2) { print $client "Passwords do not match, try again!"; &password; } return $password; } open (FILE, ">>users.txt") || print "Error occured while opening file" +; print FILE "$login:$pass\n"; close (FILE); print "File written..\n\r"; exit; } } sub logon { print $client "test"; } close $client; }

In reply to Cant get user input from sockets into variables. by thran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.