如何让Android上的客户端监听C#上的服务器?
我在安卓上有一个客户端,在c#上有一个服务器。客户端向服务器发送消息,一切正常。但我需要服务器根据请求发送指定目录中的文件夹和文件列表,但我不知道如何做,因为客户端无法从服务器收到任何消息。客户端用于侦听的部分代码不起作用,应用程序只是在我关闭服务器应用程序之前停止运行,然后再次运行,但仍然不读取任何内容。 提前谢谢 客户:如何让Android上的客户端监听C#上的服务器?,c#,android,tcp,client,C#,Android,Tcp,Client,我在安卓上有一个客户端,在c#上有一个服务器。客户端向服务器发送消息,一切正常。但我需要服务器根据请求发送指定目录中的文件夹和文件列表,但我不知道如何做,因为客户端无法从服务器收到任何消息。客户端用于侦听的部分代码不起作用,应用程序只是在我关闭服务器应用程序之前停止运行,然后再次运行,但仍然不读取任何内容。 提前谢谢 客户: package com.app.client.app; import android.app.Activity; import android.os.Bundle; im
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);
}
}
}
使用系统;
使用系统文本;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Sockets;
使用System.Windows.Forms;
公共类服务{
公共静态void Main(){
试一试{
IPAddress ipAd=IPAddress.Parse(“192.168.1.2”);
//使用本地m/c IP地址,以及
//在客户端中使用相同的方法
/*初始化侦听器*/
TcpListener myList=新的TcpListener(ipAd,4444);
/*在指定的端口开始侦听*/
myList.Start();
WriteLine(“服务器正在端口4444上运行…”);
Console.WriteLine(“本地端点为:”+
myList.LocalEndpoint);
WriteLine(“等待连接…”);
m:
套接字s=myList.AcceptSocket();
Console.WriteLine(“从“+s.RemoteEndPoint”接受连接);
字节[]b=新字节[100];
int k=s.Receive(b);
char cc='';
字符串测试=null;
Console.WriteLine(“接收…”);
对于(int i=0;i
好的,我找到了解决您问题的方法
在发送文本的c#服务器中:
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
s.Close();
发送数据后,请确保关闭此处的套接字。这就是为什么你的应用程序会挂起。它正在等待来自服务器的更多输入
另外,在Java中更改此选项,您将收到
String text = "";
String finalText = "";
while ((text = in.readLine()) != null) {
finalText += text;
}
txt.setText(finalText);
请注意,您是如何在该循环中执行2个readInputs的。while语句执行一个。。而设置文本只会做一件事。。所以你基本上是在一个循环中读两行。更改到我上面发布的内容将修复此问题谢谢您的代码示例 仅供参考: 您需要设置以下权限:
<uses-permission android:name="android.permission.INTERNET" />
要避免NetworkOnMainThreadException,您需要在新线程中执行通信代码 在Android客户端中:
@Override
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
connectSocket("Hello");
}
}).start();
}
不
Console.WriteLine(“已接收…”)代码>打印?只是好奇为什么你不选择使用SOAP方法,性能?我编写了完全相同的应用程序(c#服务器向android发送文件列表)。我有资料来源,但我不久前就停止了工作。当我下班回家时,我会给你贴上它(如果那时你还没有找到答案,我的答案仍然有效)。@Allen我没有想到SOAP,只是找到了一些教程并遵循了它them@dymmeh那太好了,谢谢,期待着!我已经将您的代码复制并粘贴到一个项目中,并将我的代码完全复制并粘贴到项目中,每次都能完美地工作。text+=urldecker.decode(输入,“UTF-8”)@dymmeh你能告诉我为什么你改变了之前评论中提到的行吗,因为我真的不明白,我一开始就把它们放在那里,因为我在我的应用程序中就是这样做的。然而,我在没有测试的情况下测试了它,发现它不需要工作。我想在那里添加额外的代码不会有帮助,所以我删除了它。对不起,我把你弄糊涂了!