Socket send only on close

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
    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,

Merl111
1) try send "some_string\n", not "some_string".
2) try use fsync(socketfd_s)
Oh sending with \n worked, thank you very much, I forgot the line terminator, shame on me...

Best regards,

Merl
Topic archived. No new replies allowed.