C# 通过UDP连接接收的数据不';无法在文本视图中显示
编辑:我用工作代码更新了帖子 我正在尝试将PC上的文本文件发送到手机,并在文本视图中显示 在我用C#制作的PC服务器上,它可以毫无错误地完成工作。此外,手机上的代码工作时没有任何错误 我认为问题在于编码,文本视图是空白的,它甚至没有显示“Hello world”示例文本 这部分是在PC上C# 通过UDP连接接收的数据不';无法在文本视图中显示,c#,android,textview,udp,C#,Android,Textview,Udp,编辑:我用工作代码更新了帖子 我正在尝试将PC上的文本文件发送到手机,并在文本视图中显示 在我用C#制作的PC服务器上,它可以毫无错误地完成工作。此外,手机上的代码工作时没有任何错误 我认为问题在于编码,文本视图是空白的,它甚至没有显示“Hello world”示例文本 这部分是在PC上 class Program { static int port = 11000; static void Main(string[] args) { Se
class Program
{
static int port = 11000;
static void Main(string[] args)
{
ServerThread();
}
static public void ServerThread()
{
Thread t = new Thread(Recieve);
t.Start();
}
static public void Recieve()
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.10"), port);
UdpClient client = new UdpClient(port);
while(true)
{
Console.WriteLine("Server listening");
client.Connect(ipEndPoint);
Byte[] recieveBytes = client.Receive(ref ipEndPoint);
if (Convert.ToInt16(Encoding.ASCII.GetString(recieveBytes))==0)
{
Console.WriteLine("Shut down");
break;
}
Console.WriteLine("Contact");
Thread.Sleep(1000);
Console.WriteLine("Sending file");
string text = File.ReadAllText(@"C:\Users\.............\kviz.txt");
Byte[] sendBytes = Encoding.Unicode.GetBytes(text);
client.Connect(ipEndPoint);
client.Send(sendBytes, sendBytes.Length);
}
Console.WriteLine("Server closing");
client.Close();
}
}
这部分在电话里
public class MainActivity extends Activity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.textView1);
new ClientAsyncTask().execute();
//new ServerAsyncTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ClientAsyncTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String result = "";
try {
Send();
result = Recieve();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
text.setText(result);
}
}
public void Send()
{
String s = "1";
int port = 11000;
byte[] message = s.getBytes();
int length = s.length();
try {
InetAddress local = InetAddress.getByName("192.168.0.11");
DatagramSocket socketSend = new DatagramSocket(port);
DatagramPacket p = new DatagramPacket(message, length, local, port);
socketSend.send(p);
socketSend.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String Recieve()
{
String recieveString = "";
try {
InetAddress local = InetAddress.getByName("192.168.0.11");
byte[]recieveMessage = new byte[1000];
int port = 11000;
DatagramSocket socketRecieve = new DatagramSocket(port);
DatagramPacket recievePacket = new DatagramPacket(recieveMessage, recieveMessage.length, local, port);
socketRecieve.receive(recievePacket);
recieveString = new String(recievePacket.getData());
Log.i("InfoTest", recieveString);
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return recieveString;
}
}
公共类MainActivity扩展活动{
私有文本查看文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.textView1);
新建ClientAsyncTask().execute();
//新建ServerAsyncTask().execute();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
公共类ClientAsyncTask扩展了AsyncTask{
@凌驾
受保护字符串doInBackground(无效…参数){
字符串结果=”;
试一试{
Send();
结果=接收();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回结果;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
text.setText(结果);
}
}
公共无效发送()
{
字符串s=“1”;
int端口=11000;
字节[]消息=s.getBytes();
int length=s.length();
试一试{
InetAddress local=InetAddress.getByName(“192.168.0.11”);
DatagramSocket socketSend=新的DatagramSocket(端口);
DatagramPacket p=新的DatagramPacket(消息、长度、本地、端口);
socketSend.send(p);
socketSend.close();
}捕获(SocketException e){
e、 printStackTrace();
}捕获(未知后异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共字符串receive()
{
字符串receiveString=“”;
试一试{
InetAddress local=InetAddress.getByName(“192.168.0.11”);
字节[]接收消息=新字节[1000];
int端口=11000;
DatagramSocket socketRecieve=新的DatagramSocket(端口);
DatagramPacket ReceivePacket=新的DatagramPacket(ReceiveMessage,ReceiveMessage.length,local,port);
socketRecieve.receive(receivepacket);
ReceiveString=新字符串(receivePacket.getData());
Log.i(“InfoTest”,receiveString);
}捕获(SocketException e){
e、 printStackTrace();
}捕获(未知后异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回receiveString;
}
}
在您的服务器上尝试Encoding.UTF8.GetBytes()
。我只是尝试了一下,它不起作用。也许我在手机上的线程设置有问题?我刚刚检查过,String()默认为UTF16。嗯,那个页面上似乎有冲突的数据。顶部显示UTF16,但当您检查实际使用时,它显示使用默认字符集,即Android上的UTF8。我注意到我的代码中有一个错误:IPEndPoint IPEndPoint=new IPEndPoint(IPAddress.Any,11000)
应该是IPEndPoint-IPEndPoint=new-IPEndPoint(IPAddress.Parse(“192.168.0.10”),11000代码>,但这仍然不能解决问题。现在我想我应该让这部分receivestring=newstring(receivepacket.getData(),0,receivepacket.getLength())代码>不同