如何将数据从Java客户端发送到C ++服务器? [英] How can I send data from a Java client to a C++ server?
问题描述
Java客户端可以使用TCP连接到C ++服务器。 Java客户端在Win7上。 C ++服务器在Linux上。
The Java client can connect to the C++ server using TCP. The Java client is on Win7. The C++ server is on Linux.
问题是我无法将数据成功发送到C ++服务器。 Java代码是:
The problem is I cannot send data successfully to the C++ server. The Java code is:
public static void main (String [] args ) throws IOException {
Socket sock = new Socket("10.217.140.200",7000);
String id = "TEST";
char encoding = 'a';
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
oos.flush();
oos.writeObject(encoding);
有用。记住删除 ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
否则会输出4个字符到c ++服务器。
After your kind helps, I now use outputstream and it works. Remember to delete the ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
otherwise it will output 4 chars to c++ server.
推荐答案
您正在使用 ObjectOutputStream
,它 Java对象序列化。这不是(容易)跨语言移植。因为你只发送一个String,你不需要使用 ObjectOutputStream
- 只需使用 OutputStream
返回从套接字。
You're using an ObjectOutputStream
, which does Java object serialisation. This is not (easily) portable across languages. Since you're only sending a String, you don't need to use an ObjectOutputStream
- just use the OutputStream
returned from the socket.
这篇关于如何将数据从Java客户端发送到C ++服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!