我有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.
我會很高興得到一些幫助。 謝謝你的助手:)
「它去抓」,請告訴我們你得到的例外。你可以調試它/看看你的logcat – 2014-11-01 12:07:58
我wrrited ..「源找不到」 – RonYamin 2014-11-01 12:15:53