【发布时间】:2021-08-12 08:30:40
【问题描述】:
我有一个使用 Eclipse 在 Java 中运行的服务器(这里是完整代码):
public class Main {
public static void main(String[]args) {
// BBDD connection var
Connection con = null;
// Sockets
ServerSocket server = null;
try {
server = new ServerSocket(5010);
} catch (IOException e2) {System.out.println("Error at creating server, check if the port is in use");System.exit(-1);}
/*** MySQL driver loading***/
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("MySQL driver is UP");
} catch (ClassNotFoundException e1) {System.out.println("Error loading MySQL driver");}
/*** Stablishing connection with the ddbb ***/
try {
con= (Connection) DriverManager.getConnection("jdbc:mysql://IP/Database", "username", "password");
System.out.println("Connection stablished");
} catch (SQLException e) {
System.out.println("SQL ERROR");
e.printStackTrace();
}
/*** Assigning thread for client ***/
System.out.println("Listening for new requests...");
while (true) {
try {
Socket clientSocket;
clientSocket = server.accept();
System.out.println("Connection stablished");
DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
ThreadForClient client = new ThreadForClient(con, clientSocket, dis, dos);
Thread thread = new Thread(cliente);
thread .start();
} catch (IOException e) {System.out.println("Error making client socket");}
}
}
class ThreadForClient implements Runnable {
Connection con;
Socket clientSocket;
DataInputStream dis;
DataOutputStream dos;
public ThreadForClient(Connection con, Socket s, DataInputStream dis, DataOutputStream dos) {
this.con = con;
this.clientSocket = s;
this.dis = dis;
this.dos = dos;
}
@Override
public void run() {
try {
int opc = dis.readInt();
switch (opc) {
case 1:
String email = dis.readUTF();
Boolean result = existeUsuario(email);
dos.writeBoolean(result);
break;
default: break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Boolean userExists(String email) throws SQLException { // 1
email = email.toLowerCase();
Statement stmt;
stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select * from users where email = '"+email+"'");
boolean result = rs.first();
return result;
}
}
如您所见,客户端发送 2 个值并接收 1 个,第一个用于切换,这将指示要执行的内容,现在我只有 on 选项(检查 ddbb 中是否存在电子邮件),第二个值是电子邮件,因为代码已经进入开关。
服务器完全正常工作,我已经在 Eclipse 中使用客户端测试项目对其进行了测试(这是我想在 Android Studio 中做的):
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket(HOST, PORT);
System.out.println("Conection succesful");
DataOutputStream dos;
DataInputStream dis;
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
dos.writeInt(1);
dos.writeUTF("email@example.com");
Boolean bool = dis.readBoolean();
if (bool) {
System.out.println("Email exists in the BBDD.");
} else {
System.out.println("Email does not exist in the BBDD.");
}
socket.close();
} catch (Exception e) {System.out.println("Server connection failed");}
}
现在我想做的是在 Android Studio 中执行相同的客户端,以便它可以连接到服务器。
我试过了:
public class SocketConnection {
UserExists ue;
public SocketConnection() throws IOException {
connect();
}
public void connect() {
ue = new UserExists();
ue.execute();
}
}
class UserExists extends AsyncTask<Void, Void, Void> {
int PORT = 5010;
String HOST = "192.168.1.5";
private Socket socket;
private DataOutputStream dos;
private DataInputStream dis;
@Override
protected Void doInBackground(Void... voids) {
try {
socket = new Socket(HOST, PORT);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
} catch (Exception e) {
System.out.println("Socket error");
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
当我运行它时,它不会给我任何错误,但是如果我在 doInBackground 底部记录套接字值以查看套接字值,它会说它是空的。
我也尝试过使用 Thread 和 Handle,但我无法得到服务器返回给我的主线程的结果。
【问题讨论】:
标签: java mysql eclipse android-studio sockets