android Socket 长连接出错:android.system.ErrnoException: connect failed: ECONNREFUSED Connection refuse

Android 端做Socket 长连接测试 , 报错:提示说连接被拒绝.

java.net.ConnectException: failed to connect to /192.168.0.101 (port 8081): connect failed: ECONNREFUSED (Connection refused)

Android 长连接项目,运行到真实手机上,手机 的iP是:192.168.0.101.

04-06 12:28:56.772 15845-15863/com.example.myapplication W/System.err: java.net.ConnectException: failed to connect to /192.168.0.101 (port 8081): connect failed: ECONNREFUSED (Connection refused)
04-06 12:28:56.773 15845-15863/com.example.myapplication W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
        at java.net.Socket.startupSocket(Socket.java:592)
        at java.net.Socket.tryAllAddresses(Socket.java:128)
        at java.net.Socket.(Socket.java:178)
        at java.net.Socket.(Socket.java:150)
        at com.example.myapplication.ClientActivity$1.run(ClientActivity.java:32)
    Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
        at libcore.io.Posix.connect(Native Method)
        at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
        at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
04-06 12:28:56.774 15845-15863/com.example.myapplication W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)

将ip改成192.168.0.103,这个是PC机的IP也不成功. 尝试过运行到模拟手机上也不行.

以下是client端的代码.

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);
        show = (EditText) findViewById(R.id.et_message );
        new Thread()
        {
            @Override
            public void run()
            {
                try
                {
                    Log.e(TAG,"client Run-->");
                    // 建立连接到远程服务器的Socket
                    Socket socket = new Socket("192.168.0.103", 8081);  // ①
                    // 将Socket对应的输入流包装成BufferedReader
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(socket.getInputStream()));
                    // 进行普通I/O操作
                    String line = br.readLine();
                    show.setText("来自服务器的数据:" + line);
                    // 关闭输入流、socket
                    br.close();
                    socket.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }.start();

解决:服务端未成功部署.服务端应该部署在tomcat 之类的服务器上.

小白学写socket.以下是参考的server代码,第一次把它跟上面的client代码一起发布到android app端,

正确的方式应该是单独出来放在服务器用的是Eclipse,其实没什么要求,eclipse,myeclipse都行的,因为JDK本身就包含了TCP的网络通信API,所以用Socket不需要其他服务器支持就可以了。

package com.example.myapplication;

import android.util.Log;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleServer {
    public static final String TAG="SimpleServer";

    public static void main(String[] args) throws IOException {
        Log.e(TAG,"Server Main-->");
        // 创建一个ServerSocket,用于监听客户端Socket的连接请求
        ServerSocket ss = new ServerSocket( 9901 );
        // 采用循环不断接受来自客户端的请求
        while (true) {
            // 每当接受到客户端Socket的请求,服务器端也对应产生一个Socket
            Socket s = ss.accept();
            OutputStream os = s.getOutputStream();
            os.write( " 来追我呀!如果你追到我,我就跟你一起走可持续发展的中国特色社会主义道路!!!\n"
                    .getBytes( "utf-8" ) );
            Log.e(TAG,"server print------------------");
            // 关闭输出流,关闭Socket
            os.close();
            s.close();
        }
    }
}

参考:https://blog.csdn.net/Wshshushu/article/details/75330563

你可能感兴趣的:(Android)