0
我開始在Android上,並在我的第一個應用程序中,我需要建立一個PC和Android表之間的通信。我需要的是,staqtic IP直接通信,當我有幾臺PC和平板電腦時,每張桌子只能與PC通信。Android平板電腦和PC之間的通信
從平板電腦到PC的通訊已經在工作,但來自PC表我不能得到數據轉移
的Android側
public class Server implements Runnable
{
@Override
public void run()
{
while (always==true)
{
while(start2==false)
{
}
try
{
InetAddress serverAddr = InetAddress.getByName("192.168.173.133");
updatetrack("\nServer: Start connecting\n");
//*DatagramSocket socket = new DatagramSocket(SERVERPORT2, serverAddr);/
DatagramSocket socket = new DatagramSocket(SERVERPORT2);
byte[] buf = new byte[17];
DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVERPORT2);
//*DatagramPacket packet = new DatagramPacket(buf, buf.length);/
updatetrack("Server: Receiving\n");
socket.receive(packet);
updatetrack("Server: Message received: '" + new String(packet.getData()) + "'\n");
updatetrack("Server: Succeed!\n");
start2=false;
}
catch (Exception e)
{
updatetrack("Server: Error!\n");
start2=false;
}
}
}
}
192.168.173.133是表IP和SERVERPORT2是4445 當我開始它仍然顯示後等待數據的應用「服務器:在接收」但
C#代碼
public static void Main()
{
IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.173.133"), 4445);
string hostname = Dns.GetHostName();
byte[] data = Encoding.ASCII.GetBytes(hostname);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
sock.SendTo(data, iep2);
sock.Close();
}
我想這是任何愚蠢的以爲我忘了,但讀書很多論壇和書籍後,我停止了在這一點上
任何建議將受到歡迎
謝謝亞歷山大,它的工作原理 – josocjo