Android和C#tcp连接失败
问题描述:
我有2个程序,其中一个是android应用程序(客户端),另一个是c#wpf应用程序(服务器)。我的建议是加载服务器,然后加载客户端,客户端自动连接到服务器以及获得新客户端的服务器标识符。当服务器标识一个新的客户端时,服务器自动发送一条包含CliendID的消息给客户端。客户端从服务器收到一条消息,然后他可以向服务器发送一条包含ClientID(他得到的)和他的位置的消息。所有这一切都必须在线程中,因为我希望服务器和客户端也做其他事情。连接不成功,它在两个应用程序中都会遇到“找不到的东西”。Android和C#tcp连接失败
客户端代码的java:
//Define variables
String host ="192.168.43.145";
int port = 13000;
Socket socket;
Writer writer;
BufferedReader reader;
// OnCreate function
socket= new Socket(host, port);
writer= new OutputStreamWriter(socket.getOutputStream(), "latin1");
reader= new BufferedReader(new InputStreamReader(socket.getInputStream(), "latin1"));
// Get reply:
String IdObserverstring = reader.readLine();
writer.write(IdObserverstring);
writer.write(LatitudeForSend);
writer.write(LongtitudeForSend);
writer.flush();
C#代码:
// Define variables :
TcpListener listenerForAndroid;
IPAddress ip;
bool IsListener;
int port, IDObser;
string ipString;
public BinaryReader rd;
public MemoryStream memory;
public BinaryWriter wr;
Socket sender;
// Constructor of application
// Ip local host
ipString = "192.168.43.145";
port = 13000();
// Converting the ip string to ipAddress
ip = IPAddress.Parse(ipString);
/* Initializes the Listener */
listenerForAndroid = new TcpListener(ip, port);
byte[] byteRecived = new byte[1024];
try
{
// Start Listeneting at the specified port
listenerForAndroid.Start();
// Now the server is running and waiting for a client connection.
sender = listenerForAndroid.AcceptSocket(); // Client Accepted
IDObser++;
ASCIIEncoding asen = new ASCIIEncoding();
sender.Send(asen.GetBytes(IDObser.ToString() + "\n")); // Sending the IDObserver to the client
int k345 = sender.Receive(byteRecived); \\ waiting for recieve ID+Location from android device
// Here I want to read from the byteRecived the bytes (all the bytes from the android device is : int(IDObserver), Double (lat), Double (long)) but I dont know how.
我会很高兴得到一些帮助。 谢谢你的助手:)
答
我设法运行你的C#代码经过小的更正(因为语法错误),并且它成功地接受来自TCP客户端的连接并按预期工作。
首先,确保你的IP是正确的。如果是,请尝试使用一些TCP调试工具独立调试应用程序(我喜欢这个https://code.google.com/p/serialcom/)。
我不明白为什么你会得到一个异常“未找到源”。我的猜测是,它不是一个例外说明,但从您的IDE的一些消息(如这样的:http://hmemcpy.com/2014/08/debug-symbols-dotpeek-and-long-paths-in-visual-studio/)
“它去抓”,请告诉我们你得到的例外。你可以调试它/看看你的logcat – 2014-11-01 12:07:58
我wrrited ..“源找不到” – RonYamin 2014-11-01 12:15:53