1827871 Members
1417 Online
109969 Solutions
New Discussion

program in c - error

 
SOLVED
Go to solution
Piotr Kirklewski
Super Advisor

program in c - error

I'm trying to compile the below program but there is an error:

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;
}
Jesus is the King
7 REPLIES 7
Hein van den Heuvel
Honored Contributor

Re: program in c - error

In the line 26 you have addr as input as ouput:

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
Hein van den Heuvel
Honored Contributor

Re: program in c - error

Hmm, 5/10. Did the reply not address the problem for which you asked for help?
Is there an other problem? Do explain.
Or just fat fingered the assignment? Just close.

Cheers,
Hein
Piotr Kirklewski
Super Advisor

Re: program in c - error

Thanks
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
Jesus is the King
Steven Schweda
Honored Contributor

Re: program in c - error

> What would be the reason for that ?

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?
James R. Ferguson
Acclaimed Contributor

Re: program in c - error

Hi:

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...
Steven Schweda
Honored Contributor
Solution

Re: program in c - error

> Do you mean to do something like this
> [...]?

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.
Steven Schweda
Honored Contributor

Re: program in c - error

> Hmm, 5/10. [...]

Smug ingratitude from the helpless is what
makes Forum participation so satisfying for
me.