// My First Internet Application // By Matthew Cudmore, 2005 #include <iostream> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> // For gethostname #define MYPORT 4689 #define QUEMAX 10 using namespace::std; int main(void) { int sockfd, new_fd, sin_size; socklen_t sl = sizeof(struct sockaddr); struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address information // SOCK_STREAM or SOCK_DGRAM if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){ // ERROR cout << "\n-- SOCKET error\n"; return 0; } my_addr.sin_family = AF_INET; // host byte order their_addr.sin_family = AF_INET; // Choose one method: // 1) my_addr.sin_port = htons(MYPORT); // short, network byte order // 2) my_addr.sin_port = htons(0); // choose an unused port at random my_addr.sin_port = htons(MYPORT); // short, network byte order their_addr.sin_port = htons(MYPORT); // Choose one method: // 1) my_addr.sin_addr.s_addr = inet_addr("10.12.110.57"); // 2) inet_aton("10.12.110.57", &(my_addr.sin_addr)); // 3) my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // use my IP address my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // use my IP address memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { // ERROR cout << "\n-- BIND error\n"; return 0; } // ################################################################### char Buffer[100]; int BtsMvd; cout << "Simple sockets by Matthew Cudmore...\n"; cout << "Your IP: " << inet_ntoa(my_addr.sin_addr) << "\n"; GetChoice: cout << "\nEnter choice action (x = exit, s = send, a = accept): "; cin.getline(Buffer, 99); if (Buffer[0] == 'x') { close (sockfd); return 0; } else if (Buffer[0] == 's') { cout << "Enter the IP of the receiving machine: "; cin.getline(Buffer, 99); their_addr.sin_addr.s_addr = inet_addr(Buffer); memset(&(their_addr.sin_zero), '\0', 8); cout << "Enter a message to send to the receiving machine: "; cin.getline(Buffer, 99); if ((BtsMvd = sendto(sockfd, Buffer, strlen(Buffer), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) != -1){ cout << "\nMessage sent. Awaiting reply..."; } else { cout << "\nMessage could not be sent."; goto GetChoice; } while (1) { if ((BtsMvd = recvfrom(sockfd, Buffer, strlen(Buffer), 0, (struct sockaddr *)&their_addr, &sl)) == -1) { // ERROR cout << "\n-- recvfrom error\n"; return 0; } if (BtsMvd) { if (Buffer[BtsMvd - 1] == '*') { cout << "\nReturn received."; break; } else { cout << "\nReturn-request ignored."; } } } } else if (Buffer[0] == 'a') { cout << "Waiting for incoming return-requests..."; BtsMvd = 0; while (1) { if ((BtsMvd = recvfrom(sockfd, Buffer, strlen(Buffer), 0, (struct sockaddr *)&their_addr, &sl)) == -1) { // ERROR cout << "\n-- recvfrom error\n"; return 0; } if (BtsMvd) break; } cout << "\nReturn-request received from: " << inet_ntoa(their_addr.sin_addr); BtsMvd = strlen(Buffer); Buffer[BtsMvd] = '*'; Buffer[BtsMvd + 1] = '\0'; if ((BtsMvd = sendto(sockfd, Buffer, strlen(Buffer), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) > 0){ cout << "\nA return (" << Buffer << ") has been sent back."; } else { cout << "\nCould not return request."; } } goto GetChoice; return 0; }
// My First Internet Application // By Matthew Cudmore, 2005 #include <iostream> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> // For gethostname #define MYPORT 4689 #define QUEMAX 10 #define BUFFER_LEN 100 #ifndef SOCKET_ERROR #define SOCKET_ERROR -1 #endif using namespace::std; int main(void) { int sockfd, new_fd, sin_size; int sl = sizeof(struct sockaddr); struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address information // SOCK_STREAM or SOCK_DGRAM if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR ) { // ERROR cout << "\n-- SOCKET error\n"; return 0; } my_addr.sin_family = AF_INET; // host byte order their_addr.sin_family = AF_INET; // Choose one method: // 1) my_addr.sin_port = htons(MYPORT); // short, network byte order // 2) my_addr.sin_port = htons(0); // choose an unused port at random my_addr.sin_port = htons(MYPORT); // short, network byte order their_addr.sin_port = htons(MYPORT); // Choose one method: // 1) my_addr.sin_addr.s_addr = inet_addr("10.12.110.57"); // 2) inet_aton("10.12.110.57", &(my_addr.sin_addr)); // 3) my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // use my IP address my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // use my IP address memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct if (bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == SOCKET_ERROR) { // ERROR cout << "\n-- BIND error\n"; return 0; } // ################################################################### char Buffer[BUFFER_LEN]; int BtsMvd; cout << "Simple sockets by Matthew Cudmore...\n"; cout << "Your IP: " << inet_ntoa(my_addr.sin_addr) << "\n"; GetChoice: cout << "\nEnter choice action (x = exit, s = send, a = accept): "; cin.getline(Buffer, 99); if (Buffer[0] == 'x') { closesocket(sockfd); return 0; } else if (Buffer[0] == 's') { cout << "Enter the IP of the receiving machine: "; cin.getline(Buffer, 99); their_addr.sin_addr.s_addr = inet_addr(Buffer); memset(&(their_addr.sin_zero), '\0', 8); cout << "Enter a message to send to the receiving machine: "; cin.getline(Buffer, 99); if ((BtsMvd = sendto(sockfd, Buffer, sizeof(Buffer), 0,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) != -1) { cout << "\nMessage sent. Awaiting reply..."; } else { cout << "\nMessage could not be sent."; goto GetChoice; } while (1) { if ((BtsMvd = recvfrom(sockfd, Buffer, sizeof(Buffer), 0,(struct sockaddr *)&their_addr, &sl)) == -1) { // ERROR cout << "\n-- recvfrom error\n"; return 0; } if ( BtsMvd ) { BtsMvd = strlen(Buffer); if (Buffer[ BtsMvd - 1 ] == '*') { cout << "\nReturn received."; break; } else { cout << Buffer<<" \nReturn-request ignored."; } } } } else if (Buffer[0] == 'a') { cout << "Waiting for incoming return-requests..."; BtsMvd = 0; while (1) { if ((BtsMvd = recvfrom(sockfd, Buffer, sizeof(Buffer), 0,(struct sockaddr *)&their_addr, &sl)) == SOCKET_ERROR) { // ERROR cout << "\n-- recvfrom error\n"; return 0; } if (BtsMvd) { break; } } cout << "\nReturn-request received from: " << inet_ntoa(their_addr.sin_addr); BtsMvd = strlen(Buffer); Buffer[ BtsMvd ] = '*'; Buffer[ BtsMvd + 1 ] = '\0'; their_addr.sin_family = AF_INET; their_addr.sin_port = htons(MYPORT); their_addr.sin_addr.s_addr =inet_addr( inet_ntoa(their_addr.sin_addr) ); memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct if ((new_fd = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR) { // ERROR cout << "\n-- SOCKET error\n"; return 0; } if ((BtsMvd = sendto(new_fd, Buffer, sizeof(Buffer), 0,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) != SOCKET_ERROR ) { cout << "\nA return (" << Buffer << ") has been sent back."; } else { cout << "\nCould not return request."; } } goto GetChoice; return 0; }
But if we specify a wildcard IP address, the kernel does not choose the local IP address until either the socket is connected (TCP) or a datagram is sent on the socket (UDP).
Your IP: 0.0.0.0
char Buffer[100]; struct hostent *h; // MEEEEE if (gethostname(Buffer, sizeof(Buffer))) { // ERROR cout << "\n-- HOST error 1\n"; return 0; } if ((h = gethostbyname(Buffer)) == 0x0) { // ERROR cout << "\n-- HOST error 2\n"; return 0; } cout << "Your IP: " << inet_ntoa(*((struct in_addr *)h->h_addr)) << "\n";
| DaniWeb Message | |
| Cancel Changes | |