gpt4 book ai didi

java - 从 Android,无法连接到我的 Java ServerSocket

转载 作者:行者123 更新时间:2023-12-01 17:05:02 24 4
gpt4 key购买 nike

我是 Java 初学者,我想在我的台式电脑上创建一个服务器(运行外部 IP 地址,已验证),并在带有 CM11 的三星 Galaxy S3 上创建一个客户端。在服务器上:我在端口 6666 上创建了一个 ServerSocket(不要问为什么)和一个 while(),其中包含 Socket ( server = .accept(); )。

在客户端:我创建了一个 Socket 客户端 (client = new Socket(HostName, port); ),并将其放入 try{}catch(){} 中:如果连接失败,应用程序将在 TextView 中显示“服务器已关闭以进行维护”。

错误:我什至无法连接到我的服务器,但外部 IP 可与终端仿真器中的 ping 配合使用...我不知道为什么。我的客户端、服务器上是否有错误,或者我需要在 list (android)中添加权限?(应用程序运行时,Android 不会在控制台中返回任何错误)--

有什么想法吗? 这就是为什么我不想显示应用程序的名称。

服务器:

    package <HIDDEN>;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class MainClass {
public static final String version ="0.1A";
public static final int port = 6666;
private static ServerSocket Service;
private static DataInputStream fromClient;
private static DataOutputStream toClient;
private static Socket server;
static InetAddress Host;

public static void main(String[] args) {

System.out.println("Welcome to <HIDDEN>'s Server! Version ## "+version+" ##");



try {
Service = new ServerSocket(port); // INITIALIZE SERVER SOCKET
System.out.println("Socket created at "+Service.getInetAddress()+" ");
} catch (IOException e) {
System.err.println("Error While Creating ServerSocket ' Service ' !!!\n");
e.printStackTrace();

}

while(true) {
try {
server = Service.accept();
System.out.println("Opened Socket on ");
if(server != null ) {System.out.println("ACCEPTING CONNECTION!");}
fromClient = new DataInputStream(server.getInputStream()); // INITIALIZE AND ACCEPT SOCKET
toClient = new DataOutputStream(server.getOutputStream()); // CONNECTION AND STREAMS

toClient.writeBoolean(com.<HIDDEN>.FileLoader.verifyLogger(String.valueOf(fromClient.readChar())));

} catch(IOException e) {
System.err.println("Error While Initializing A Dialog With Client! "+ "\033FATAL ERROR\033");
e.printStackTrace();
}
}
}

}

客户端:[仅 JAVA 文件]

package <HIDDEN>;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;


public class login_activity extends Activity {
public static String username, password;
public static final String HostName = "84.227.165.225";
public static final int port = 6666;
private Socket client;
private DataOutputStream toServer;
private DataInputStream fromServer;

@Override
protected void onCreate(Bundle savedInstanceState) { /*ONCREATE VOID*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}


public void Login(View view) {
EditText username_et = (EditText) findViewById(R.id.username_et); // EditTexts
EditText password_et = (EditText) findViewById(R.id.password_et); //---
TextView instructions_tw = (TextView) findViewById(R.id.instructions_tw);
password = password_et.getText().toString();
username = username_et.getText().toString();
try {
client = new Socket(HostName, port); //INITIALIZE SOCKET
System.out.print("Initialized Socket, connecting to Server "+HostName+":"+port+"\n");
toServer = new DataOutputStream(client.getOutputStream()); //System.out.print doesn't works on android...
}catch (Exception UnkownHostException) {
instructions_tw.setText("Please Log In Or Register To <NOPE, LOL>\n\n!! THE SERVER IS DOWN FOR MAINTENANCE");
}

try {
toServer.writeBytes(username+"???"+password);
fromServer = new DataInputStream(client.getInputStream());
boolean accepted = false;
if(fromServer.readBoolean()) {
Intent i = new Intent(this, mainmenu_activity.class);
startService(i);
} else {instructions_tw.setText("Please Log In Or Register To <HIDDEN>\n\n!! UNCORRECT PASSWORD OR USERNAME");}
/* THE CODE IS NEVER GOING TO THIS WAY ;( */
}catch(Exception UnkownHostException){

}


}
}

感谢您的帮助

最佳答案

我认为您需要将 INTERNET 权限添加到 list 文件中才能使用 Android 的网络套接字。

Android 文档说:

INTERNET Allows applications to open network sockets.

您应该将如下内容添加到 list 文件中:

<uses-permission android:name="android.permission.INTERNET" />

添加 - 还要确保可以从 Android 设备访问服务器:服务器和设备应该位于同一网络(wifi 连接)中,或者服务器应该可以从互联网访问。

关于java - 从 Android,无法连接到我的 Java ServerSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26043428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com