tehet
February 28th, 2008, 06:03 PM
Hi,
First question:
Compiling the code below, either with or without -Wall, I get the following warning:
$ gcc -Wall client.c -o client
client.c:36:34: warning: multi-character character constant
I've marked out line 36 in the code. Apparently gcc has issues with '/0' but after looking at man memset() I don't see the problem (though it compiles and runs). Replacing with 'NULL' gives the same warning and replacing with NULL gives
client.c:36: warning: passing argument 2 of ‘memset’ makes integer from pointer without a cast
:confused:
What's happening here? (Or) is it a harmless warning that can be ignored?
Second question:
How come the error check precedes printf("Connecting ...");? e.g. (there's no server yet so that's why it fails)
$ ./client
Enter server IP:
10.1.1.1
Enter port number:
9999
Creating socket ... succeeded.
failed. : Connection refused
Connecting to remote socket ...
Thanks.
/*
client.c
Socket exercise - Stream client
Mostly lifted from
http://beej.us/guide/bgnet/output/html/multipage/index.html
*/
#include <stdio.h> /* printf, scanf, etc */
#include <stdlib.h> /* ? */
#include <string.h> /* memset() */
#include <sys/types.h> /* socket(), connect(), etc */
#include <sys/socket.h> /* socket(), connect(), etc */
#include <netinet/in.h> /* inet_addr() */
#include <arpa/inet.h> /* inet_addr() */
#include <errno.h> /* perror() */
int main()
{
int sockfd;
struct sockaddr_in server_address;
char *server_ip;
unsigned short int server_port;
printf("Enter server IP:\n");
scanf("%s", server_ip);
printf("Enter port number:\n");
scanf("%hu", &server_port);
printf("Creating socket ... ");
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("failed. ");
exit(1);
}
else printf("succeeded.\n");
server_address.sin_family = AF_INET;
server_address.sin_port = htons(server_port);
server_address.sin_addr.s_addr = inet_addr(server_ip);
memset(server_address.sin_zero, '/0', sizeof server_address.sin_zero); /* = line 36 = */
printf("Connecting to remote socket ... ");
if ((connect(sockfd, (struct sockaddr *)&server_address, sizeof server_address)) == -1) {
perror("failed. ");
exit(1);
}
else printf("succeeded.\n");
return 0;
}
First question:
Compiling the code below, either with or without -Wall, I get the following warning:
$ gcc -Wall client.c -o client
client.c:36:34: warning: multi-character character constant
I've marked out line 36 in the code. Apparently gcc has issues with '/0' but after looking at man memset() I don't see the problem (though it compiles and runs). Replacing with 'NULL' gives the same warning and replacing with NULL gives
client.c:36: warning: passing argument 2 of ‘memset’ makes integer from pointer without a cast
:confused:
What's happening here? (Or) is it a harmless warning that can be ignored?
Second question:
How come the error check precedes printf("Connecting ...");? e.g. (there's no server yet so that's why it fails)
$ ./client
Enter server IP:
10.1.1.1
Enter port number:
9999
Creating socket ... succeeded.
failed. : Connection refused
Connecting to remote socket ...
Thanks.
/*
client.c
Socket exercise - Stream client
Mostly lifted from
http://beej.us/guide/bgnet/output/html/multipage/index.html
*/
#include <stdio.h> /* printf, scanf, etc */
#include <stdlib.h> /* ? */
#include <string.h> /* memset() */
#include <sys/types.h> /* socket(), connect(), etc */
#include <sys/socket.h> /* socket(), connect(), etc */
#include <netinet/in.h> /* inet_addr() */
#include <arpa/inet.h> /* inet_addr() */
#include <errno.h> /* perror() */
int main()
{
int sockfd;
struct sockaddr_in server_address;
char *server_ip;
unsigned short int server_port;
printf("Enter server IP:\n");
scanf("%s", server_ip);
printf("Enter port number:\n");
scanf("%hu", &server_port);
printf("Creating socket ... ");
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("failed. ");
exit(1);
}
else printf("succeeded.\n");
server_address.sin_family = AF_INET;
server_address.sin_port = htons(server_port);
server_address.sin_addr.s_addr = inet_addr(server_ip);
memset(server_address.sin_zero, '/0', sizeof server_address.sin_zero); /* = line 36 = */
printf("Connecting to remote socket ... ");
if ((connect(sockfd, (struct sockaddr *)&server_address, sizeof server_address)) == -1) {
perror("failed. ");
exit(1);
}
else printf("succeeded.\n");
return 0;
}