- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- program in c - error
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2011 09:55 PM
05-19-2011 09:55 PM
gcc -o portcheck portcheck.c
portcheck.c: In function ‘main’:
portcheck.c:26: error: incompatible types when assigning to type ‘char[1023]’ from type ‘char *’
Please help
==============================================
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
u_short port; /* user specified port number */
char addr[1023]; /* the address */
struct sockaddr_in address; /* address structures */
struct hostent *host_info; /* host info structure */
short int sock = -1; /* the socket descriptor */
port = atoi(argv[1]);
addr = strncpy(addr, argv[2], 1023);
bzero((char *)&address, sizeof(address)); /* init addr struct */
address.sin_addr.s_addr = inet_addr(addr); /* assign the address */
address.sin_port = htons(port); /* translate int2port num */
/*
* Three simple steps:
* 1. Open the master socket locally
* 2. Try to connect to hostbyport, if it works
* print the successful message.
* 3. If no route then complain with vulgarity
* (it is just a rapid prototype after all)
* Otherwise do nothing.
*/
sock = socket(AF_INET, SOCK_STREAM, 0);
if(connect(sock,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("%i is open on %s\n", port, argv[2]);
if (errno == 113) fprintf(stderr, "F*^k - no route to host\n");
close(sock);
return 0;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2011 10:12 PM
05-19-2011 10:12 PM
Re: program in c - error
addr = strncpy(addr, argv[2], 1023);
It is not a proper L value valid for output as provided.
Just drop it and use only:
strncpy(addr, argv[2], 1023);
If you need the output then you need something like:
char *p;
:
p = strncpy(addr, argv[2], 1023);
hth,
Hein
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2011 10:43 AM
05-21-2011 10:43 AM
Re: program in c - error
Is there an other problem? Do explain.
Or just fat fingered the assignment? Just close.
Cheers,
Hein
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2011 11:08 AM
05-21-2011 11:08 AM
Re: program in c - error
Now it compiles fine but it always says the host is active even if there is no such IP in the network.
What would be the reason for that ?
Reagrds
Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2011 01:03 PM
05-21-2011 01:03 PM
Re: program in c - error
Uh, bad code?
> sock = socket(AF_INET, SOCK_STREAM, 0);
Did that work? How would you know?
> if (errno == 113) fprintf(stderr, "F*^k - no route to host\n");
How does that help you for any other value of
errno?
man strerror
> return 0;
Always? How informative is that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2011 02:20 PM
05-21-2011 02:20 PM
Re: program in c - error
Do you mean to do something like this (replace the body of your code with this)?
...
sock = socket(AF_INET, SOCK_STREAM, 0);
if (bind(sock,(struct sockaddr *)&address,sizeof(address)) == -1) {
perror("bind");
exit(1);
}
if (connect(sock,(struct sockaddr *)&address,sizeof(address)) == -1) {
perror("connect");
exit(1);
}
printf("%i is open on %s\n", port, argv[2]);
close(sock);
exit(0);
}
Now, if I compile and run this like:
# ./a.out 22 127.0.0.1
bind: Address already in use
# ./a.out 123 127.0.0.1
123 is open on 127.0.0.1
Better?
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2011 04:02 PM
05-21-2011 04:02 PM
Solution> [...]?
I was guessing more like this:
alp $ type portcheck.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
u_short port; /* user specified port number */
struct sockaddr_in address; /* address structures */
struct hostent *host_info; /* host info structure */
short int sock = -1; /* the socket descriptor */
int sts = 0;
if (argc != 3)
exit( EINVAL);
port = atoi(argv[1]);
bzero((char *)&address, sizeof(address)); /* init addr struct */
address.sin_addr.s_addr = inet_addr(argv[2]); /* assign the address */
address.sin_port = htons(port); /* translate int2port num */
address.sin_family = AF_INET; /* Address family. */
sock = socket( address.sin_family, SOCK_STREAM, 0);
if (sock < 0)
{
fprintf( stderr, " socket(): %s\n", strerror( errno));
sts = errno;
}
else
{
if (connect( sock, (struct sockaddr *)&address, sizeof(address)) == 0)
{
fprintf( stderr, "Port %d is open on %s.\n", port, argv[2]);
}
else
{
fprintf( stderr, " connect(): %s\n", strerror( errno));
sts = errno;
}
close(sock);
}
exit( sts);
}
alp $ cc portcheck /define = (_POSIX_EXIT)
alp $ link portcheck
alp $ exec portcheck 80 10.0.0.9
Port 80 is open on 10.0.0.9.
alp $ exec portcheck 81 10.0.0.9
connect(): connection refused
I don't write enough socket code to know
much, but I can find plenty of good code from
which to steal some working bits.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2011 06:01 PM
05-21-2011 06:01 PM
Re: program in c - error
Smug ingratitude from the helpless is what
makes Forum participation so satisfying for
me.