首页 > 代码库 > Android Client 与 C# Server 的Socket通信

Android Client 与 C# Server 的Socket通信

C# Socket Server

 

1.建立C# SocketServer

 

       

 1 private void ServerStart()
 2          {
 3              //创建IPEndPoint
 4              IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
 5              //创建Socket实例
 6              server_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 7              //绑定Socket与IPEndPoint
 8              server_Socket.Bind(ipep);
 9              //设置Socket收听模式
10              server_Socket.Listen(10);
11  
12             while (true)
13              {
14                  try
15                  {
16                      //接受Andorid信息
17                      client_Socket = server_Socket.Accept();
18                      client_Thread = new Thread(new ThreadStart(ReceiveAndroidData));
19                      client_Thread.Start();
20                  }
21                  catch (Exception ex)
22                  {
23                      MessageBox.Show("start error: " + ex.Message);
24                  }
25               
26             }
27          }

 

 

2.开启一个Theard,执行

       

1 server_Thread = new Thread(new ThreadStart(ServerStart));
2          server_Thread.Start();

 

 

3.介绍方法

 

       

private void ReceiveAndroidData()
         {
             bool keepalive = true;
             Socket socketclient = client_Socket;
             Byte[] buffer = new Byte[1024];
 
            //根据收听到的客户端套接字向客户端发送信息
             IPEndPoint clientep = (IPEndPoint)socketclient.RemoteEndPoint;
             lstServer.Items.Add("Andorid Client:" + clientep.Address + ""+clientep.Port);
             string str = "connect server----- ";
             byte[] data = new byte[1024];
             data = Encoding.ASCII.GetBytes(str);
             socketclient.Send(data, data.Length, SocketFlags.None);
 
            while (keepalive)
             {
                 //在套接字上接收客户端发送的信息
                 int buffer_lenght = 0;
                 try
                 {
                     buffer_lenght = socketclient.Available;
 
                    socketclient.Receive(buffer, 0, buffer_lenght, SocketFlags.None);
                     if (buffer_lenght == 0)
                         continue;
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("receive error:" + ex.Message);
                     return;
                 }
                 clientep = (IPEndPoint)socketclient.RemoteEndPoint;
                 string strAndroid_CMD = System.Text.Encoding.ASCII.GetString(buffer).Substring(0, buffer_lenght);
 
                 //对根据strAndroid_CMD到数据库中查询值
                 

 

 

Android Client:

1. OnCreate方法中执行

 

 

try {
   Socket socket = new Socket("10.0.2.2", 1234);

    
   PrintWriter out = new PrintWriter(new BufferedWriter(
     new OutputStreamWriter(socket.getOutputStream())), true);
   out.println(cameraid);
  } catch (UnknownHostException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 

代码很就简单

==================================

需要注意的是:

1.添加internet访问权限:<uses-permission android:name="android.permission.INTERNET" />。

2.127.0.0.1 IP被模拟器占用了,而本身PC的IP为10.0.2.2,如果本机测试的时候需要注意这一点。


Android Client 与 C# Server 的Socket通信