【问题标题】:java.net.ConnectException: Connection refused: connectjava.net.ConnectException:连接被拒绝:连接
【发布时间】:2017-08-10 17:51:41
【问题描述】:

当我尝试从电脑“客户端”向手机“服务器”发送一个简单的“hello world”消息时遇到问题,并且 我尝试了所有可能的解决方案,例如:首先启动手机上的服务器应用程序,然后启动客户端但没有工作, 我也改了ip地址还是不行 我关闭了防火墙也没有发生任何事情

服务器代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
 
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);}
 
 
 public class Server{
 
    public  void main(String args[]) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(5000);} catch (IOException e) {
            e.printStackTrace();
        }
 
        while (true) {
                // Wait for a client connection.
            Socket clientSocket = null;
            try {
                clientSocket = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
 
            // Create and start a thread to handle the new client
 
                    try {
                        // Get the socket's InputStream, to read bytes
                        // from the socket
                        InputStream in = clientSocket.getInputStream();
                        // wrap the InputStream in a reader so you can
                        // read a String instead of bytes
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(in, Charset.forName("UTF-8")));
                        // Read from the socket and print line by line
                        String  line;
                        while ((line = reader.readLine()) != null) {
                            Toast.makeText(getApplicationContext(),line,Toast.LENGTH_LONG).show();
                        }
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        // This finally block ensures the socket is closed.
                        // A try-with-resources block cannot be used because
                        // the socket is passed into a thread, so it isn't
                        // created and closed in the same block
                        try {
                            clientSocket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
 
                    }
 
 
            }
        }
 
 
 }}

客户代码:

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
 
public class ClientClass {
    public static void main(String args[]) {
        try (Socket socket = new Socket("192.168.1.3", 5000);) {
            // We'll reach this code once we've connected to the server
 
            // Write a string into the socket, and flush the buffer
            OutputStream outStream = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(
                    new OutputStreamWriter(outStream, StandardCharsets.UTF_8));
            writer.println("Hello world!");
            writer.flush();
        } catch (IOException e) {
            // Exception should be handled.
            e.printStackTrace();
        }
    }
}

错误:

java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at ClientClass.main(ClientClass.java:10)
BUILD SUCCESSFUL (total time: 2 seconds)

【问题讨论】:

  • 您的服务器是否在 Android 上运行?
  • 是的 4.3 android 版本

标签: android sockets networking tcp


【解决方案1】:

在 MainActivity 类中,名为 Server 的内部类永远不会被实例化,“main”方法也不会被调用。

请查看此Tutorial post,以便了解如何使用带有 android 设备的套接字实现服务器的示例。

【讨论】:

    【解决方案2】:

    我遇到了同样的错误,并且我设法知道在我的情况下导致此错误的原因,您必须检查两件事:

    1-Router 配置,如果您使用 WiFi 连接,因为您必须正确转发您的端口。(How to Set Up Port Forwarding on a Router,根据路由器类型而有所不同)

    2-这里的IP地址是否是您手机的正确IP地址:

    Socket socket = new Socket("192.168.1.3", 5000);

    (How To Check Your Android IP Address)

    【讨论】:

    • 谢谢大家,问题出在服务器上,我没有给它上网权限,现在可以了
    • 不客气,是的,这也很重要。
    猜你喜欢
    • 2023-03-21
    • 2015-05-18
    • 2018-06-10
    • 2014-05-17
    • 2013-11-05
    • 2015-09-09
    • 1970-01-01
    • 2017-05-14
    相关资源
    最近更新 更多