Here is a running code. You will have to clean it up more.
// 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;
} The errors you made were not in the socket creation. This is what the Stevens Book say for bind at INADDR_ANYBut 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).Hence you see the
Your IP: 0.0.0.0 The errors were in specifying the amount of data to be received and sent using the recvfrom and sendto functions. You will understand while looking at my code.
Another thing, I am not good in Linux Programming, but I think there should be a way of determining the specific error that occurred under some function. e.g GetLastError in Windows. Find that. Then it will be easier to find out errors in your program. And if you are thinking of doing some serious network programming better to buy UNIX® Network Programming Volume by W.R. Stevens.