473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Perl socket

I cant find mistake in following code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Socket;
  4.  
  5. $host=<STDIN>;
  6. chomp($host);
  7. $port=<STDIN>;
  8.  
  9. $paddr=sockaddr_in($port, inet_aton($host));
  10. $proto=getprotobyname('tcp');
  11.  
  12. socket(SOKET, PF_INET, SOCK_STREAM, $proto) or die "$!";
  13.  
  14. connect(SOKET, $paddr);
  15.  
  16. if(connect(SOKET, $paddr)){
  17.     print "1\n";
  18. } else {
  19.     print "0\n";
  20. }
  21.  
Aug 27 '07 #1
8 1939
numberwhun
3,509 Expert Mod 2GB
First, you don't have the "use strict" and "use warnings" pragmas at the beginning of your code. Without those there to do away with the little errors, most people won't look at your code.

Second, you haven't exactly told us what error you are receiving.

Third, you may want to also chomp() the input for the $port variable.

Lastly, please place any and all code that you put in your post into code tags. Doing so takes some of the work off of our friendly neighborhood moderators that have to clean up posts. (If you don't know how to use code tags, simply look at the "Reply Guidelines" next to your posting message window.)

Regards,

Jeff
Aug 27 '07 #2
Lastly, please place any and all code that you put in your post into code tags. Doing so takes some of the work off of our friendly neighborhood moderators that have to clean up posts. (If you don't know how to use code tags, simply look at the "Reply Guidelines" next to your posting message window.)
Sorry, I am new on this forum.

and about program: it just print "0".
Sep 1 '07 #3
numberwhun
3,509 Expert Mod 2GB
Now mind you, I have zero experience working with sockets, but I notice in your code that you are doing the 'connect' and then using the same connect statement in an if as a test. Are you trying to see if its connected?

I don't think that that will work the way you have it because I doubt that the connect statement returns anything beyond the connection. I think a better way to do this would be to say:

Expand|Select|Wrap|Line Numbers
  1. connect(SOKET, $paddr) or die "Could not connect:  $!";
  2.  
That will do the connection for you, but if it doesn't work, it will run the die statement and the "$!" will print the error that was produced.

Regards,

Jeff
Sep 1 '07 #4
No errors. So... I made it?
Sep 1 '07 #5
numberwhun
3,509 Expert Mod 2GB
No errors. So... I made it?
I would say.

Jeff
Sep 1 '07 #6
KevinADC
4,059 Expert 2GB
I would also chomp() $port to be safe:

$port=<STDIN>;
chomp($port);
Sep 1 '07 #7
numberwhun
3,509 Expert Mod 2GB
I would also chomp() $port to be safe:

$port=<STDIN>;
chomp($port);
Yeah, that was the third thing I suggested. Hopefully he did it.
Sep 1 '07 #8
KevinADC
4,059 Expert 2GB
Yeah, that was the third thing I suggested. Hopefully he did it.

Ahh.... I missed that.
Sep 2 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
0
by: Danny Jensen | last post by:
I need to test if certain processes on a unix box were running. I wanted to use whatsup gold to do the testing. First I needed to go to the whatsup configure>monitors & services menu to add this...
3
by: Daniel Moree | last post by:
I'm attempting to use a perl script to interface with my Visual Basic 6 program using Winsock. I've got my program setup to connect and works great if i connect to another winsock program, but it...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
1
by: bobano | last post by:
Hi everyone, I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol....
4
by: Ignoramus6539 | last post by:
There were some strange requests to my server asking for config.php file (which I do not have in the requested location). I did some investigation. Seems to be a virus written in perl,...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
0
by: muraley | last post by:
Hi, This client-server script does bi-directional communication. Setup i used: The server script on windows 2003 server and the client on a linux machine. Since i faced issues in getting the...
0
by: crawfordr | last post by:
Hello, I have created a perl script that connects to a specific socket (Ip address/port) using protocall of TCP. It is the server socket script. There is also coding to manage multiple handles by...
6
darksteel21
by: darksteel21 | last post by:
Hi to all, I am currently developing an application in perl that is capable of connecting to a server and sends information.. the server has a deamon that listens to the socket connections. It...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.