Q:

My program doesn't send message or doesn't receive to socket, the ip is 127.0.0.1

when I use sockets the program server process doesn't receive any message from the client process/class.
The input port to the user is 5555, but when the program exits the Client's constructor, the port number of sin doesn't match (I think it's because of htons), same goes to the ip address.
please help me fix this.
this is my server code:
#include "SocketUDP.h"
/*
* class constructor
*/
SocketUDP::SocketUDP() {
    sock = socket(AF_INET, SOCK_DGRAM, 0);

    if (sock < 0)
        perror("error creating socket");
}

/*
* class destructor
*/
SocketUDP::~SocketUDP() {
    close(sock);
}

/*
* this function recieves a message from client/server
* @param - the length of the message
*/
std::string SocketUDP::RecieveMessage(){
    unsigned int from_len = sizeof(struct sockaddr_in);
    char buffer[4096];
    memset(&buffer, 0, sizeof(buffer));
    int bytes = recvfrom(sock, buffer, sizeof(buffer), 0,
        (struct sockaddr *) &from, &from_len);

    if (bytes < 0)
        perror("error reading from socket");

    return std::string(buffer);
}

This is the client:
  #include "UDPClient.h" 

  /*
  * class constructor
  */
  UDPClient::UDPClient(char * ip, int port) {
      memset(&sin, 0, sizeof(sin));
      sin.sin_addr.s_addr = inet_addr(ip);
      sin.sin_family = AF_INET;
      sin.sin_port = htons(port);
  }

 /*
 * class destructor
 */
UDPClient::~UDPClient() {
    // TODO Auto-generated destructor stub
}

 /*
 * this function sends a message to the client/server
 * @param - the message
  */
  int UDPClient::SendMessage(std::string st){
      int sent_bytes = sendto(sock, st.c_str(), st.length(), 0,
            (struct sockaddr *) &sin, sizeof(sin));
      if (sent_bytes < 0)
          perror("error writing to socket");

      return sent_bytes;
  }

A:

You are missing a call to bind() in the server. This is where you tell the OS on which port (5555) it should listen for incoming UDP packets.
It is quite confusing if you omit bind()in the server. In this case the OS selects a random port to receive from, which is usually not what one wants.
The class name UDPSocket indicates that this is just a wrapper around a UDP socket, and not a server. A server would have a bind() call in addition, and an endless loop where it processes requests. Perhaps you omitted the server code by accident?

