qq_25267095
qq_25267095
2016-01-04 07:22

两个Android模拟器间Socket通信的问题

10
  • android
  • socket

代码如下
客户端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

Socket socket = null;
String buffer = "";
TextView txt1;
Button send;
EditText ed1;
String geted1;
public Handler myHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (msg.what == 0x11) {
            Bundle bundle = msg.getData();
            txt1.append("server:" + bundle.getString("msg") + "\n");
        }
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt1 = (TextView) findViewById(R.id.TextView);
    send = (Button) findViewById(R.id.button);
    ed1 = (EditText) findViewById(R.id.EditText);
    new MyThread("link building").start();
    send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            geted1 = ed1.getText().toString();
            txt1.append("client:" + geted1 + "\n");
            // 启动线程 向服务器发送和接收信息
            new MyThread(geted1).start();
        }
    });

}

class MyThread extends Thread {

    public String txt1;

    public MyThread(String str) {
        txt1 = str;
    }

    @Override
    public void run() {
        // 定义消息
        Message msg = new Message();
        msg.what = 0x11;
        Bundle bundle = new Bundle();
        bundle.clear();
        try {
            // 连接服务器 并设置连接超时为5秒
            socket = new Socket();
            socket.connect(new InetSocketAddress("10.0.2.2", 5555), 5000);
            // 获取输入输出流
            OutputStream os = socket.getOutputStream();
            BufferedReader bff = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // 读取发来服务器信息
            String line = null;
            buffer = "";
            while ((line = bff.readLine()) != null) {
                buffer = line + buffer;
            }

            // 向服务器发送信息
            os.write(txt1.getBytes("gbk"));
            os.flush();
            bundle.putString("msg", buffer.toString());
            msg.setData(bundle);
            // 发送消息 修改UI线程中的组件
            myHandler.sendMessage(msg);
            // 关闭各种输入输出流
            bff.close();
            os.close();
            socket.close();
        } catch (SocketTimeoutException aa) {
            // 连接超时 在UI界面显示消息
            bundle.putString("msg", "connect failed");
            msg.setData(bundle);
            // 发送消息 修改UI线程中的组件
            myHandler.sendMessage(msg);
        } catch (IOException e) {
            bundle.putString("msg", "error");
            msg.setData(bundle);
            // 发送消息 修改UI线程中的组件
            myHandler.sendMessage(msg);
            e.printStackTrace();
        }
    }
}

}

服务器:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.os.Vibrator;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

public static ServerSocket serverSocket = null;
public static TextView textview1;



String buffer = "";
public static Handler mHandler = new Handler() {
    @Override
    public void handleMessage(android.os.Message msg) {
        if (msg.what==0x11) {
            Bundle bundle = msg.getData();
            textview1.append("client:"+bundle.getString("msg")+"\n");

        }
    };
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textview1 = (TextView) findViewById(R.id.textview1);


    new Thread() {
        public void run() {
            Bundle bundle = new Bundle();
            bundle.clear();
            OutputStream output;
            String str = "wait Link ";
            try {
                serverSocket = new ServerSocket(5555);

                while(true) {
                    Message msg = new Message();
                    msg.what = 0x11;
                    try {
                        Socket socket = serverSocket.accept();
                        output = socket.getOutputStream();
                        output.write(str.getBytes("UTF-8"));
                        output.flush();
                        socket.shutdownOutput();
                        //mHandler.sendEmptyMessage(0);
                        BufferedReader bff = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String line = null;
                        buffer = "";
                        while ((line = bff.readLine()) != null) {
                            buffer = line + buffer;
                        }
                        bundle.putString("msg", buffer.toString());
                        msg.setData(bundle);
                        // 发送消息 修改UI线程中的组件
                        mHandler.sendMessage(msg);
                        bff.close();
                        output.close();
                        socket.close();
                    } catch (IOException e) {
                        bundle.putString("msg", "connect failed");
                        msg.setData(bundle);
                        // 发送消息 修改UI线程中的组件
                        mHandler.sendMessage(msg);
                        e.printStackTrace();
                    }
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        };
    }.start();
}

}

问题是Socket链接始终无法建立,请问是什么原因

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答