• Android与server通信中的方法(TCP)高效的安全完整


    我以前一直使用sockets实现emulator和PC进行通讯,卡了几天,最后请教其它人最终能够连接了。  错误原因是在IP和port,IP要用本机IP(在CMD输入ipconfig。注意:每次开机本机IP都不同,ADSL是动态IP),port用emulator的名称,5554或5556.java sever文件要另外建javaproject执行。在androidproject执行不了java(run application) 


    client代码: 
    Java代码  
    package com.yarin.android.Examples_08_04;   
      
    import java.io.BufferedReader;   
    import java.io.BufferedWriter;   
    import java.io.InputStreamReader;   
    import java.io.OutputStreamWriter;   
    import java.io.PrintWriter;   
    import java.net.InetAddress;   
    import java.net.Socket;   
      
    import android.app.Activity;   
    import android.os.Bundle;   
    import android.util.Log;   
    import android.view.View;   
    import android.view.View.OnClickListener;   
    import android.widget.Button;   
    import android.widget.EditText;   
    import android.widget.TextView;   
      
    public class Activity01 extends Activity   
    {   
        private final String        DEBUG_TAG   = "Activity01";   
           
        private TextView    mTextView=null;   
        private EditText    mEditText=null;   
        private Button      mButton=null;   
          
        @Override  
        public void onCreate(Bundle savedInstanceState)   
        {   
            super.onCreate(savedInstanceState);   
            setContentView(R.layout.main);   
               
            mButton = (Button)findViewById(R.id.Button01);   
            mTextView=(TextView)findViewById(R.id.TextView01);   
            mEditText=(EditText)findViewById(R.id.EditText01);   
               
            //登陆   
            mButton.setOnClickListener(new OnClickListener()   
            {   
                public void onClick(View v)   
                {   
                    Socket socket = null;   
                    String message = mEditText.getText().toString() + "
    ";    
                    try    
                    {      
                        //创建Socket   
                        socket = new Socket("116.29.27.138",5554); //查看本机IP,每次开机都不同   
                        //socket=new Socket("192.168.1.110",50000);   
                        //向server发送消息   
                        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);         
                        out.println(message);    
                           
                        //接收来自server的消息   
                        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));    
                        String msg = br.readLine();    
                           
                        if ( msg != null )   
                        {   
                            mTextView.setText(msg);   
                        }   
                        else  
                        {   
                            mTextView.setText("数据错误!");   
                        }   
                        //关闭流   
                        out.close();   
                        br.close();   
                        //关闭Socket   
                        socket.close();    
                    }   
                    catch (Exception e)    
                    {   
                        // TODO: handle exception   
                        Log.e(DEBUG_TAG, e.toString());   
                    }   
                }   
            });   
        }   
    }  
    
    package com.yarin.android.Examples_08_04; 
    
    import java.io.BufferedReader; 
    import java.io.BufferedWriter; 
    import java.io.InputStreamReader; 
    import java.io.OutputStreamWriter; 
    import java.io.PrintWriter; 
    import java.net.InetAddress; 
    import java.net.Socket; 
    
    import android.app.Activity; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.TextView; 
    
    public class Activity01 extends Activity 
    { 
    private final String DEBUG_TAG = "Activity01"; 
    
    private TextView mTextView=null; 
    private EditText mEditText=null; 
    private Button mButton=null; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    
    mButton = (Button)findViewById(R.id.Button01); 
    mTextView=(TextView)findViewById(R.id.TextView01); 
    mEditText=(EditText)findViewById(R.id.EditText01); 
    
    //登陆 
    mButton.setOnClickListener(new OnClickListener() 
    { 
    public void onClick(View v) 
    { 
    Socket socket = null; 
    String message = mEditText.getText().toString() + "
    "; 
    try 
    {
    //创建Socket 
    socket = new Socket("116.29.27.138",5554); //查看本机IP,每次开机都不同 
    //socket=new Socket("192.168.1.110",50000); 
    //向server发送消息 
    PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);      
    out.println(message); 
    
    //接收来自server的消息 
    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
    String msg = br.readLine(); 
    
    if ( msg != null ) 
    { 
    mTextView.setText(msg); 
    } 
    else 
    { 
    mTextView.setText("数据错误!"); 
    } 
    //关闭流 
    out.close(); 
    br.close(); 
    //关闭Socket 
    socket.close(); 
    } 
    catch (Exception e) 
    { 
    // TODO: handle exception 
    Log.e(DEBUG_TAG, e.toString()); 
    } 
    } 
    }); 
    } 
    } 
    







    server端代码: 
    Java代码  
    package com.yarin.android.Examples_08_04;   
      
    import java.io.BufferedReader;   
    import java.io.BufferedWriter;   
    import java.io.InputStreamReader;   
    import java.io.OutputStreamWriter;   
    import java.io.PrintWriter;   
    import java.net.ServerSocket;   
    import java.net.Socket;   
      
    public class Server implements Runnable   
    {   
        public void run()   
        {   
            try  
            {   
                //创建ServerSocket   
                ServerSocket serverSocket = new ServerSocket(5554);   
                while (true)   
                {   
                    //接受客户端请求   
                    Socket client = serverSocket.accept();   
                    System.out.println("accept");   
                    try  
                    {   
                        //接收客户端消息   
                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));   
                        String str = in.readLine();   
                        System.out.println("read:" + str);     
                        //向服务器发送消息   
                        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(client.getOutputStream())),true);         
                        out.println("server message");    
                        //关闭流   
                        out.close();   
                        in.close();   
                    }   
                    catch (Exception e)   
                    {   
                        System.out.println(e.getMessage());   
                        e.printStackTrace();   
                    }   
                    finally  
                    {   
                        //关闭   
                        client.close();   
                        System.out.println("close");   
                    }   
                }   
            }   
            catch (Exception e)   
            {   
                System.out.println(e.getMessage());   
            }   
        }   
        //main函数。开启服务器   
        public static void main(String a[])   
        {   
            Thread desktopServerThread = new Thread(new Server());   
            desktopServerThread.start();   
        }   
    }  


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    JVM中java类的加载时机(转载:http://blog.csdn.net/chenleixing/article/details/47099725)
    自定义filter包
    Tomcat中Listener的使用范例(转载http://cywhoyi.iteye.com/blog/2075848)
    Quartz简单使用
    PAT A1119 Pre- and Post-order Traversals [前序后序求中序]
    PAT A1115 Counting Nodes in a BST [二叉搜索树]
    PAT A1110 Complete Binary Tree [完全二叉树]
    PAT A1102 Invert a Binary Tree [反转二叉树]
    PAT A1099 Build A Binary Search Tree [二叉搜索树]
    PAT A1094 The Largest Generation [树的遍历]
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4821182.html
Copyright © 2020-2023  润新知