J'écris un programme pour vérifier si un port est ouvert dans C. Une ligne en particulier copie l'un des arguments dans un tableau char. Cependant, quand j'essaye de compiler, il indique:C: types incompatibles dans l'affectation
error: incompatible types in assignment
Hérite le code. L'erreur est sur la cession de addr
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv) {
u_short port; /* user specified port number */
char addr[1023]; /* will be a copy of the address entered by u */
struct sockaddr_in address; /* the libc network address data structure */
short int sock = -1; /* file descriptor for the network socket */
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 */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (connect(sock,(struct sockaddr *)&address,sizeof(address)) == 0) {
printf("%i is open\n", port);
}
if (errno == 113) {
fprintf(stderr, "Port not open!\n");
}
close(sock);
return 0;
}
Je suis nouveau à C, donc je ne sais pas pourquoi il le ferait.
Alors, comment puis-je changer pour résoudre ce problème? –
La réponse vous dit ... les deux le font. –