android 模拟器和电脑服务器端用socket通讯

之前一直会用sockets来实现emulator和PC进行通讯,卡了几天,最后请教其他人终于可以连接了。  错误原因是在IP和端口,IP要用本机IP(在CMD输入ipconfig,注意:每次开机本机IP都不同,ADSL是动态IP),端口用emulator的名称,5554或5556.java sever文件要另外建java工程运行,在android工程运行不了java(run application)


客户端代码:
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;  
    /** Called when the activity is first created. */ 
    @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() + "\r\n";   
                try   
                {     
                    //创建Socket  
                    socket = new Socket("116.29.27.138",5554); //查看本机IP,每次开机都不同  
                    //socket=new Socket("192.168.1.110",50000);  
                    //向服务器发送消息  
                    PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);        
                    out.println(message);   
                      
                    //接收来自服务器的消息  
                    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;
/** Called when the activity is first created. */
@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() + "\r\n";
try
{
//创建Socket
socket = new Socket("116.29.27.138",5554); //查看本机IP,每次开机都不同
//socket=new Socket("192.168.1.110",50000);
//向服务器发送消息
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);     
out.println(message);

//接收来自服务器的消息
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());
}
}
});
}
}






服务器端代码:
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();  
    }  

 

 

:博客涉及的源码请在千寻资源库www.qxzyk.com 下载获取,谢谢支持。

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值