php服务端与android客户端socket通信

转自:http://swerit.iteye.com/blog/1291829


php服务端的代码要在类似apache的服务器中运行,代码如下: 

Php代码   收藏代码
  1.  <?php     
  2.     //确保在连接客户端时不会超时     
  3.     set_time_limit(0);     
  4.         
  5.     //设置IP和端口号     
  6.     $address='127.0.0.1';     
  7.     $port=3333;    //调试的时候,可以多换端口来测试程序!     
  8.         
  9.     //创建一个SOCKET     
  10.     if(($sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP))<0)     
  11.     {     
  12.     echo "socket_create() 失败的原因是:".socket_strerror($sock)."<br>";     
  13.     }     
  14.         
  15.     //绑定到socket端口     
  16.     if(($ret=socket_bind($sock,$address,$port))<0)     
  17.     {     
  18.     echo "socket_bind() 失败的原因是:".socket_strerror($ret)."<br>";     
  19.     }     
  20.         
  21.     //开始监听     
  22.     if(($ret=socket_listen($sock,4))<0)     
  23.     {     
  24.     echo "socket_listen() 失败的原因是:".socket_strerror($ret)."<br>";     
  25.     }     
  26.         
  27.     do {     
  28.          if (($msgsock = socket_accept($sock)) < 0)     
  29.          {     
  30.              echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "/n";   
  31.              echo "/nThe Server is Stop……/n";  
  32.              break;     
  33.          }     
  34.         
  35.          //发到客户端     
  36.          $msg ="<font color=red>Welcome To Server!</font><br>";     
  37.          socket_write($msgsock$msgstrlen($msg));  
  38.          socket_close($msgsock);     
  39.            
  40.          echo "/nThe Server is running……/n";  
  41.          printf("/nThe Server is running……/n");  
  42.     } while (true);     
  43.         
  44.     socket_close($sock);     
  45. ?>    



android客户端在同一台机器上的模拟器中运行,代码如下: 
Java代码   收藏代码
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.io.OutputStream;  
  4. import java.net.Socket;  
  5. import java.net.UnknownHostException;  
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9.   
  10. public class SocketTest extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         try {  
  18.               
  19.             System.out.println("准备连接");  
  20.             Socket socket = new Socket("10.0.2.2"3333);  
  21.             System.out.println("连接上了");  
  22.               
  23.             Intent intent = new Intent();  
  24.             intent.setClass(SocketTest.this, ConnectActivity.class);  
  25.             SocketTest.this.startActivity(intent);            
  26.             InputStream inputStream = socket.getInputStream();  
  27.             byte buffer[] = new byte[1024*4];  
  28.             int temp = 0;  
  29.             String res = null;  
  30.             //从inputstream中读取客户端所发送的数据  
  31.             System.out.println("接收到服务器的信息是:");  
  32.               
  33.             while ((temp = inputStream.read(buffer)) != -1){  
  34.                 System.out.println(new String(buffer, 0, temp));  
  35.                 res += new String(buffer, 0, temp);  
  36.             }  
  37.               
  38.             System.out.println("已经结束接收信息……");  
  39.               
  40.             socket.close();  
  41.             inputStream.close();  
  42.               
  43.         } catch (UnknownHostException e) {  
  44.             e.printStackTrace();  
  45.         } catch (IOException e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.           
  49.     }  
  50. }  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值