你好,游客 登录 注册 搜索
背景:
阅读新闻

Android Socket通信(下)

[日期:2012-06-02] 来源:Linux社区  作者:htttw [字体:大 中 小]

layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/btnConnect"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/connect" />  
  17.   
  18.     <EditText  
  19.         android:id="@+id/sendMsg"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:inputType="text" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/btnSend"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="@string/send" />  
  29.   
  30. </LinearLayout>  

不要忘了,在AndroidManifest.xml中添加访问网络权限:
<uses-permission android:name="android.permission.INTERNET" />

把上面的代码编译并下载到手机中

3.

服务器端的代码,用c++实现:

server.c

  1. #include <sys/types.h>   
  2. #include <sys/socket.h>   
  3. #include <netinet/in.h>   
  4. #include <arpa/inet.h>   
  5. #include <unistd.h>   
  6. #include <stdlib.h>   
  7. #include <stdio.h>   
  8.   
  9. #define PORT 9500   
  10. #define MAX_BUFFER 1024   
  11.   
  12.   
  13. int main()  
  14. {  
  15.   /* create a socket */  
  16.   int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);  
  17.     
  18.   struct sockaddr_in server_addr;  
  19.   server_addr.sin_family = AF_INET;  
  20.   server_addr.sin_addr.s_addr = inet_addr("192.168.1.100");  
  21.   server_addr.sin_port = htons(PORT);  
  22.     
  23.   /* bind with the local file */  
  24.   bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));  
  25.     
  26.   /* listen */  
  27.   listen(server_sockfd, 5);  
  28.     
  29.   int size;  
  30.   char buffer[MAX_BUFFER + 1];  
  31.   int client_sockfd;  
  32.   struct sockaddr_in client_addr;  
  33.   socklen_t len = sizeof(client_addr);  
  34.     
  35.   /* accept a connection */  
  36.   printf("waiting connection...\n");  
  37.   client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_addr, &len);  
  38.   printf("connection established!\n");  
  39.       
  40.   while(1)  
  41.   {  
  42.     printf("waiting message...\n");  
  43.       
  44.     /* exchange data */  
  45.     size = read(client_sockfd, buffer, MAX_BUFFER);  
  46.     buffer[size] = '\0';  
  47.     printf("Got %d bytes: %s\n", size, buffer);  
  48.   }  
  49.   
  50.   /* close the socket */  
  51.   close(client_sockfd);  
  52.         
  53.   return 0;  
  54. }  

Makefile:

  1. all: server.c  
  2.     gcc -g -Wall -o server server.c  
  3.   
  4. clean:  
  5.     rm -rf *.o server  

4.

运行结果:

代码不过多解释了,注释挺详细的,有关pc机上的tcp通信,可以参考:

http://www.linuxidc.com/Linux/2012-06/61802htm

最后,我把这个服务器和客户端两个程序都上传上来,供大家下载:

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/6月/2日/Android Socket通信(下)/

linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款