java 发送socket给c_C ++ socket,客户端无法向java服务器发送消息 (C++ socket, client can't send message to java server)...

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值