title: In perl socket programming how to send a data from client and receive it from server
body: <p>I am using Socket module to perform socket programming in Perl.And now I want to send one data from the client and receive it from the Server side. How I will achive this. Please help.</p>
  
  <p>Given below in the code i used</p>
  
  <p>server</p>
  
  <pre><code>#!/usr/bin/perl -w
  # Filename : server.pl
  
  use strict;
  use IO::Socket;
  use Socket;
  use Sys::Hostname;
  use constant BUFSIZE =&gt; 1024;
  
  # use port 7890 as default
  my $port = shift || 7890;
  my $proto = getprotobyname('tcp');
  my $server = "localhost";  # Host IP running the server
  
  # create a socket, make it reusable
  socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
     or die "Can't open socket $!\n";
  setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1)
     or die "Can't set socket option to SO_REUSEADDR $!\n";
  
  # bind to a port, then listen
  bind( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
     or die "Can't bind to port $port! \n";
  
  listen(SOCKET, 5) or die "listen: $!";
  print "SERVER started on port $port\n";
  
  # accepting a connection
  my $client_addr;
  my $val = 100;
  
  while ($client_addr = accept(NEW_SOCKET, SOCKET)) {
     # send them a message, close connection
     my $name = gethostbyaddr($client_addr, AF_INET );
     print NEW_SOCKET "Smile from the server";
     print NEW_SOCKET $val;
     print "Connection recieved from $name\n";
     close NEW_SOCKET;
  }
  </code></pre>
  
  <p>client</p>
  
  <pre><code>#!/usr/bin/perl -w
  # Filename : client.pl
  
  use strict;
  use IO::Socket;
  use Socket;
  use Sys::Hostname;
  use constant BUFSIZE =&gt; 1024;
  
  # initialize host and port
  my $host = shift || 'localhost';
  my $port = shift || 7890;
  my $server = "localhost";  # Host IP running the server
  
  # create the socket, connect to the port
  socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2])
     or die "Can't create a socket $!\n";
  connect( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
     or die "Can't connect to port $port! \n";
  
  my $line;
  my $req = 1000;
  while ($line = &lt;SOCKET&gt;) {
          print "$line\n";
  }
  close SOCKET or die "close: $!";
  </code></pre>