简体   繁体   English

c ++和java之间的socket

[英]socket between c++ and java

I wrote a server in c++ and to send messages I set the follow thread: 我用c ++编写了一个服务器并发送消息我设置了以下线程:

void *send(void* v)
{
    string m="";
    while(true)
    {
        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){
        if(input.hasNext()){
            viewText.setText(viewText.getText() + "\nSever: "+input.nextLine());
        }
    }
}

@Override
public void run() {
    try {
        recieve();
        socket.close();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

When I write into the variable M and press enter, nothing happen. 当我写入变量M并按回车键时,没有任何反应。 But when I close the server, all the values of mi wrote before are printed as one string. 但是当我关闭服务器时,之前写的mi的所有值都打印为一个字符串。

I Apologize for the incorrect English. 我为错误的英语道歉。 tnks TNKS

string m="";

m is a class called std::string . m是一个名为std::string的类。

static_cast<void*>(&m),

&m is the address of the std::string structure. &mstd::string结构的地址。 It is not the address of the actual string that this class represents. 它不是此类表示的实际字符串的地址。 You need to obtain the actual contents of the string class, and write that to the socket: 您需要获取字符串类的实际内容,并将其写入套接字:

write(socketfd, m.c_str(), m.length()+1);

This is an over-simplified example, but you can think of a std::string as a class that looks like this: 这是一个过于简化的示例,但您可以将std::string视为一个如下所示的类:

class std::string {

    char *data;
    size_t length;

    // ...
};

Writing the physical address of this pointer, and other internal contents of the string class to the socket accomplishes nothing useful. 将该指针的物理地址和字符串类的其他内部内容写入套接字不会有任何用处。 You need to obtain the actual string data. 您需要获取实际的字符串数据。

Not knowing the type of input in your Java code, the method name nextLine() suggests that it will read a whole line. 不知道Java代码中的input类型,方法名称nextLine()表明它将读取整行。 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; 另一方面,不会通过std::cin >> m;存储换行符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); 要发送多行,您应该使用另一种方式,例如std::getline(std::cin, m); to read input and add newline characters if the way drops them. 如果删除它们的方式读取输入并添加换行符。

std::cin >> m;

Will only read until the first instance of whitespace, or a newline if only one word is written: 只会在空白的第一个实例之前读取,或者如果只写入一个单词则读取换行符:

input.nextLine()

reads until a newline is entered, and spaces don't terminate. 读取直到输入换行符,并且空格不会终止。 To do the equivalent in C++, you need to use getline(std::cin, m) like this (Don't forget to include <string> ): 要在C ++中执行等效操作,您需要像这样使用getline(std::cin, m) (不要忘记包含<string> ):

getline(std::cin, input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM