2015-02-06 14:38:14
0
I am trying to get a two socket (one for sending and the other one for receiving) working.
Right now I can receive but I can't send. The server is written in java and I am trying to connect with a client written in c++.
This is the client part, I am opening a socket for sending in an if statement and if the expression is false I want to send a message, but it doesn't work.
if (function == "connect_to_server" ){
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 << "socket : " << socketfd_s ;
std::cout << "'Client connected....." << std::endl;
}
else
std::cout << "send()ing total 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;
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 connection to the server is established, but the string does not get send to the server.
Ah and the whole thing runs in a while (1){} to be able to receive more then one message.
EDIT: I forgot to mention, after starting the client nothing is sent, but if I kill it with ctrl - c the message is sent to the server.
thx in advance,
merl