So I'm making a program that gets the IP Address of an inputed website, then sends the IP Address to an inputed host. This program has no real meaning, but just a learning experiment. So I sucessfully made the program, then I wanted to try out recv(), and it produces some weird output; here is the source:
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* exit() */
#include <errno.h> /* herror() */
#include <netdb.h> /* gethostbyname() */
#include <sys/types.h> /* bind() accept() */
#include <sys/socket.h> /* bind() accept() */
#define PORT 3490
main(int argc, char *argv[]) {
int sockfd, n_sockfd, sin_size, len;
char *host_addr, *recv_msg;
struct hostent *host;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
if (argc != 3) {
fprintf(stderr, "usage: %s [hostname] [ip address]\n",
argv[0]);
exit(1);
}
if ((host = gethostbyname(argv[1])) == NULL) {
herror("gethostbyname");
exit(1);
}
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = inet_addr(argv[2]);
memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
if ((bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr))) == -1) {
perror("bind");
exit(1);
}
if ((listen(sockfd, 10)) == -1) {
perror("socket");
exit(1);
}
sin_size = sizeof(struct sockaddr_in);
if ((n_sockfd = accept(sockfd, (struct sockaddr *)&their_addr,
&sin_size)) == -1) {
perror("accept");
exit(1);
}
close(sockfd);
host_addr = inet_ntoa(*((struct in_addr *)host->h_addr));
len = strlen(host_addr);
send(n_sockfd, host_addr, len, 0);
len = strlen("\n");
send(n_sockfd, "\n", len, 0);
recv(n_sockfd, recv_msg, 10, 0);
printf("%s\n", recv_msg);
close(n_sockfd);
}
And here is my command line stuff:
The program in action:
~/c/network/testing $ ./hostinfo google.com 127.0.0.1
hello
+FVfv
~/c/network/testing $
And the same computer interacting with it:
~ $ telnet 127.0.0.1 3490
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
64.233.167.99
hello
Connection closed by foreign host.
~ $
How what is with the +FVfv?
Thanks for reading,
Octal