Hey!! Ahh, all day I've been trying to learn something new, so reading over tutorials I compiled my first socket app!! The only problem is, it isn't working the way I would like it to, and I was hoping you gyus could help me figure out the problems.. I appologize if it is a little messy at this time, I'm quite distrought and ready to pull my hair out so I didn't yet take the care to fine-ture it and organize the code.. I'll do that once I can get it to work and figure out how to manipulate these damn sockets lol.. For the time being, i want to work with Datagram sockets, not streaming.. Thanks..
My app: I want it to be simple and console-based, you have two options: To send or receive a message. If you choose to send, you enter the receiver's IP address and a message to send to them. If the message is received, it is returned with an asterisk (*) appended to the end of it. If you choose to receive, the app will wait until a return-request is received, then it will return the data with an asterisk (*) appended to the end of it.. Simple? Yea.. I will make it more advanced etc after I can get the following working properly, please help me with that:
// 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;
}
The problems I've encountered: Can't retrieve my own IP address (I found out that was because INADDR_ANY is equal to zero).. When sending or waiting for data, the console kinda goes neutral and I can type and stuff when I shouldn't be able to.. It won't continue through the code, perhaps it's waiting for the receiving computer to respond with an ACK... Hmm, please heeeeellllppppp!!!! :cheesy: