Ido Ikar Ido Ikar - 1 year ago 82
Java Question

socket between c++ and java

I wrote a server in c++ and to send messages I set the follow thread:

void *send(void* v)
string m="";
std::cin >> m;
write(socketfd, static_cast<void*>(&m), m.length()+1);

to read the messages I wrote the follwing code:

public void recieve() throws IOException{
while (true){
viewText.setText(viewText.getText() + "\nSever: "+input.nextLine());

public void run() {
try {
catch (IOException e) {
// TODO Auto-generated catch block

When I write into the variable M and press enter, nothing happen. But when I close the server, all the values of m i wrote before are printed as one string.

I Apologize for the incorrect English.

Answer Source

Not knowing the type of input in your Java code, the method name nextLine() suggests that it will read a whole line. Line is sequence of characters which is terminated with newline character. On the other hand, no newline character will be stored via std::cin >> m;, so all data until terminating the sending side will be treated as one line.

To send multiple lines, you should use another way such as std::getline(std::cin, m); to read input and add newline characters if the way drops them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download