TCP 服务器获取 ???????而不是真正的信息 [英] TCP Server gets ???????? instead of the real message

查看:49
本文介绍了TCP 服务器获取 ???????而不是真正的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器是用 c# 编写的,并且与我用 c# 制作的客户端一起正常工作,现在我正在尝试制作一个 android 客户端,但服务器没有得到真正的消息,它只是有很多问号.

The server is written in c# and works correctly with a client I made in c#, now i'm trying to make an android client but the server doesn't get the real message, it gets just a lot of question marks.

这里是服务器

TcpListener listen = new TcpListener(IPAddress.Any, 1200);
TcpClient client;
listen.Start();
client = listen.AcceptTcpClient();
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
int data = stream.Read(buffer, 0, client.ReceiveBufferSize);
string message = Encoding.Unicode.GetString(buffer, 0, data);
Console.WriteLine(message);

这是安卓客户端

EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString()
PrintWriter out = new PrintWriter(new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream())),
                true);
out.println(str);

例如,如果我发送消息Hello",服务器会打印?????????????"我发送的任何消息都会发生同样的情况,即使只有 1 个字母

For example if I send the message "Hello", the server prints "???????????" and it happens the same for any message I send, even just 1 letter

我也尝试过类似这样的不同方法,但结果是一样的:

I also tried with different methods like this one but the result is the same:

DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.writeBytes(str);

推荐答案

更改:

string message = Encoding.Unicode.GetString(buffer, 0, data);

进入:

string message = Encoding.UTF8.GetString(buffer, 0, data);

也试试UTF16.我认为java现在使用这种编码.

Try also UTF16. I think java uses this encoding now.

这篇关于TCP 服务器获取 ???????而不是真正的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆