android socket通信(下)

来源:互联网 发布:淘宝店铺冻结能退货吗 编辑:程序博客网 时间:2024/05/22 14:34




在android socket通信(上),我们完成了一个模拟器上运行的android socket通信实例程序:

http://blog.csdn.net/htttw/article/details/7574372



今天我们将它移植到真实的android手机上,不过要先确保环境配置正确,请参考上一讲



主机的lwan0的ip地址是路由器自动分配的:192.168.1.100,android手机的ip地址是路由器自动分配的:192.168.1.101,可以在主机上ping手机,理论上是通的,不过很奇怪,我经常会碰到ping不通的情况,然后我在android手机里装了一个模拟终端,ping主机,一般都是通的,难道是android手机的问题?




下面直接上代码,和上一讲的代码基本没有差别,改动的部分如下:

1.

ip地址修改过了

2.

端口由9400改为了9500(呵呵,这是任意的,不改也可以的)



2.

src/RealclientActivity.java

[java] view plaincopy
  1. package real.client.com;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintStream;  
  5. import java.net.Socket;  
  6. import java.net.UnknownHostException;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15.   
  16. public class RealclientActivity extends Activity  
  17. {  
  18.   /* 服务器地址 */  
  19.   private final String SERVER_HOST_IP = "192.168.1.100";  
  20.   
  21.   /* 服务器端口 */  
  22.   private final int SERVER_HOST_PORT = 9500;  
  23.     
  24.   private Button btnConnect;  
  25.   private Button btnSend;  
  26.   private EditText editSend;  
  27.   private Socket socket;  
  28.   private PrintStream output;  
  29.   
  30.   
  31.   public void toastText(String message)  
  32.   {  
  33.     Toast.makeText(this, message, Toast.LENGTH_LONG).show();  
  34.   }  
  35.   
  36.   public void handleException(Exception e, String prefix)  
  37.   {  
  38.     e.printStackTrace();  
  39.     toastText(prefix + e.toString());  
  40.   }  
  41.   
  42.   /** Called when the activity is first created. */  
  43.   @Override  
  44.   public void onCreate(Bundle savedInstanceState)  
  45.   {  
  46.     super.onCreate(savedInstanceState);  
  47.     setContentView(R.layout.main);  
  48.   
  49.     initView();  
  50.   
  51.     btnConnect.setOnClickListener(new Button.OnClickListener()  
  52.     {  
  53.       @Override  
  54.       public void onClick(View v)  
  55.       {  
  56.         initClientSocket();  
  57.       }  
  58.     });  
  59.       
  60.     btnSend.setOnClickListener(new Button.OnClickListener()  
  61.     {  
  62.       @Override  
  63.       public void onClick(View v)  
  64.       {  
  65.         sendMessage(editSend.getText().toString());  
  66.       }  
  67.     });  
  68.   }  
  69.     
  70.   public void initView()  
  71.   {  
  72.     btnConnect = (Button)findViewById(R.id.btnConnect);  
  73.     btnSend = (Button)findViewById(R.id.btnSend);  
  74.     editSend = (EditText)findViewById(R.id.sendMsg);  
  75.   
  76.     btnSend.setEnabled(false);  
  77.     editSend.setEnabled(false);  
  78.   }  
  79.   
  80.   public void closeSocket()  
  81.   {  
  82.     try  
  83.     {  
  84.       output.close();  
  85.       socket.close();  
  86.     }  
  87.     catch (IOException e)  
  88.     {  
  89.       handleException(e, "close exception: ");  
  90.     }  
  91.   }  
  92.     
  93.   private void initClientSocket()  
  94.   {  
  95.     try  
  96.     {  
  97.       /* 连接服务器 */  
  98.       socket = new Socket(SERVER_HOST_IP, SERVER_HOST_PORT);  
  99.   
  100.       /* 获取输出流 */  
  101.       output = new PrintStream(socket.getOutputStream(), true"utf-8");  
  102.         
  103.       btnConnect.setEnabled(false);  
  104.       editSend.setEnabled(true);  
  105.       btnSend.setEnabled(true);  
  106.     }  
  107.     catch (UnknownHostException e)  
  108.     {  
  109.       handleException(e, "unknown host exception: " + e.toString());  
  110.     }  
  111.     catch (IOException e)  
  112.     {  
  113.       handleException(e, "io exception: " + e.toString());  
  114.     }  
  115.   }  
  116.     
  117.   private void sendMessage(String msg)  
  118.   {  
  119.     output.print(msg);  
  120.   }  
  121. }  


layout/main.xml

[plain] view plaincopy
  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

[cpp] view plaincopy
  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:

[plain] view plaincopy
  1. all: server.c  
  2.     gcc -g -Wall -o server server.c  
  3.   
  4. clean:  
  5.     rm -rf *.o server  


4.
运行结果:






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

http://blog.csdn.net/htttw/article/details/7519964



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

http://download.csdn.net/detail/htttw/4307666

0 0
原创粉丝点击