如何使Android上的客户端在C#上监听服务器?

我有Android上的客户端和C#上的服务器。 客户端发送消息到服务器,这很好。 但我需要服务器发送请求列表中的文件夹和文件在指定的目录,不知道该怎么做,因为客户端没有得到任何消息从服务器。 客户端的部分代码是用来监听的,但是应用程序只是在closures服务器应用程序之前进行了一些操作,然后再次运行,但是仍然不会读取任何内容。 提前致谢

客户:

package com.app.client.app; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import android.util.Log; public class my_activity extends Activity { private TextView txt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button)findViewById(R.id.button1); txt = (TextView)findViewById(R.id.textView1); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub connectSocket("Hello"); } }); } private void connectSocket(String a){ try { InetAddress serverAddr = InetAddress.getByName("192.168.1.2"); Log.d("TCP", "C: Connecting..."); Socket socket = new Socket(serverAddr, 4444); message = "1"; PrintWriter out = null; BufferedReader in = null; try { Log.d("TCP", "C: Sending: '" + message + "'"); out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out.println(message); while ((in.readLine()) != null) { txt.append(in.readLine()); } Log.d("TCP", "C: Sent."); Log.d("TCP", "C: Done."); } catch(Exception e) { Log.e("TCP", "S: Error", e); } finally { socket.close(); } } catch (UnknownHostException e) { // TODO Auto-generated catch block Log.e("TCP", "C: UnknownHostException", e); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block Log.e("TCP", "C: IOException", e); e.printStackTrace(); } } } 

服务器:

 using System; using System.Text; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Windows.Forms; public class serv { public static void Main() { try { IPAddress ipAd = IPAddress.Parse("192.168.1.2"); // use local m/c IP address, and // use the same in the client /* Initializes the Listener */ TcpListener myList=new TcpListener(ipAd,4444); /* Start Listeneting at the specified port */ myList.Start(); Console.WriteLine("The server is running at port 4444..."); Console.WriteLine("The local End point is :" + myList.LocalEndpoint ); Console.WriteLine("Waiting for a connection....."); m: Socket s=myList.AcceptSocket(); Console.WriteLine("Connection accepted from " + s.RemoteEndPoint); byte[] b=new byte[100]; int k=s.Receive(b); char cc = ' '; string test = null; Console.WriteLine("Recieved..."); for (int i = 0; i < k-1; i++) { Console.Write(Convert.ToChar(b[i])); cc = Convert.ToChar(b[i]); test += cc.ToString(); } switch (test) { case "1": break; } ASCIIEncoding asen=new ASCIIEncoding(); s.Send(asen.GetBytes("The string was recieved by the server.")); Console.WriteLine("\nSent Acknowledgement"); /* clean up */ goto m; s.Close(); myList.Stop(); Console.ReadLine(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } } } 

好的,我发现你的问题的解决scheme。

在您发送文本的c#服务器中:

 ASCIIEncoding asen = new ASCIIEncoding(); s.Send(asen.GetBytes("The string was recieved by the server.")); s.Close(); 

一旦发送数据,确保在这里closures套接字。 这就是为什么你的应用程序将挂起。 IT正在等待来自服务器的更多input

另外,请在您收到的Java中更改为此

 String text = ""; String finalText = ""; while ((text = in.readLine()) != null) { finalText += text; } txt.setText(finalText); 

注意你在循环中做了两个readInput。 while语句做一个..并设置文本做一个..所以你基本上试图在一个循环中读取2行。 改变到我上面发布的将修复

要避免NetworkOnMainThreadException,您需要在新线程中执行通信代码。

在Android客户端中:

  @Override public void onClick(View v) { new Thread(new Runnable() { public void run() { connectSocket("Hello"); } }).start(); } 

谢谢你的代码示例!

仅供参考:

您需要设置以下权限:

  <uses-permission android:name="android.permission.INTERNET" />