Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,

I am pretty new to c++, but I am trying to program a two way socket connection (one for sending acts as a client and one for receiving acts as a server).

The receive part works perfectly, but I have a problem with the sending part.

The message want to send only gets send if I close the socket, and there is the problem, I can't close the socket because the server ( in this case an other program) expects the socket to stay open.

Is there any way I can send the message without closing the sending socket?



C++
if (function == "connect" ){
    status_s = getaddrinfo(&host_const[0], &port_const[0], &host_info_s, &host_info_list_s);

    if (status_s != 0)  std::cout << "getaddrinfo error" << gai_strerror(status_s) ;


    std::cout << "Creating a sending socket..."  << std::endl;
    socketfd_s = socket(host_info_list->ai_family, host_info_list->ai_socktype,
                      host_info_list->ai_protocol);
    if (socketfd_s == -1)  std::cout << "socket error " ;

    std::cout << "Host: " << &host_const[0] << std::endl;
    std::cout << "Port: " << &port_const[0] << std::endl;
    std::cout << "function: " << function << std::endl;
    function.erase(std::remove(function.begin(), function.end(), '\n'), function.end());

    std::cout << "Connect()ing..."  << std::endl;
    status_s = connect(socketfd_s, host_info_list_s->ai_addr, host_info_list_s->ai_addrlen);
    if (status_s == -1)  std::cout << "connect error" ;
    cout << "status: " << status_s;
    cout << "socket connector:                       " << socketfd_s ;



    std::cout << "client connected....."  << std::endl;
   }
   else {//if (function != "connect" )
        std::cout << "send()ing message on anwser..."  << std::endl;
        char *msg_s = "some_string";
        int len_s;
        ssize_t bytes_sent_s;
        std::cout << msg_s;
        len_s = strlen(msg_s);
        std::cout << len_s << std::endl;
        std::cout << "Message not sent...."  << std::endl;
        //send(socketfd_s, msg_s, len_s, 0);
        bytes_sent_s = sendto(socketfd_s, msg_s, len_s, 0, host_info_list_s->ai_addr, host_info_list_s->ai_addrlen);
        freeaddrinfo(host_info_list);
        //close(socketfd_s);

        //bytes_sent_s = send(socketfd_s, msg_s, len_s, 0);
        //send(socketfd_s, msg_s, len_s, 0);
        std::cout << "Message sent....\n"  << std::endl;
        std::cin.ignore();
        cout << "param1" << socketfd_s;
        cout << "param2" << len_s;
        cout << "param3" << msg_s;
}



The connect to the socket works perfectly, just the else part is the problem, as you can see I already tried send and sendto, but had the same result.

Ah and the whole thing runs in a while (1) loop to read more than one message.

Thank you
Posted

You should be using send to send your messages, rather than sendto, which is for connectionless sockets. See http://linux.die.net/man/2/sendto[^].
 
Share this answer
 
I suggest you to try the code sample you may find in this page: "Sockets Tutorial"[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900