Reputation: 11
I'm not very experienced in Java and javafx but I'm trying to create a chat app using a JavaFx client and a python server. I use a controller to interact with my app :
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
public class MainController implements Initializable {
private Connection mainConnection;
@FXML
private TextField messageEntry;
@FXML
protected void sendMessageAction(ActionEvent e) {
mainConnection.sendMessage(messageEntry.getText());
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
try {
mainConnection = new Connection("127.0.0.1", 1500);
} catch (IOException e) {
e.printStackTrace();
}
mainConnection.sendMessage("Connection initialized");
}
}
Here is my class Connection :
public class Connection {
private OutputStream outputStream;
private PrintWriter printWriter;
private Socket socket;
public Connection(String ipadressString, int port) throws UnknownHostException, IOException {
socket = new Socket(ipadressString, port);
outputStream = socket.getOutputStream();
printWriter = new PrintWriter(outputStream);
}
public void sendMessage(String message) {
System.out.println(message);
printWriter.print(message);
printWriter.flush();
}
}
In my controller initialization, I create a Connection object and I call :mainConnection.sendMessage("Connection initialized");
My server receive correctly this message. Then, when I click the button and call the method sendMessage()
, nothing happen and my server doesn't receive anything but no error appear... Moreover, the method is correctly called because the System.out.println(message)
print the message in my console.
Python server
import socket
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(("127.0.0.1",1500))
server.listen(5)
connexion, info = server.accept()
print(info)
while True :
message = connexion.recv(4096)
if (message != b""):
print(message)
connexion.close()
server.close()
firstOverview.fxml file :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="671.0" prefWidth="789.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.rfaur.spokee.MainController">
<children>
<TextField fx:id="messageEntry" layoutX="91.0" layoutY="630.0" prefHeight="26.0" prefWidth="683.0" />
<Button layoutX="14.0" layoutY="630.0" mnemonicParsing="false" onAction="#sendMessageAction" text="Envoyer" />
</children>
</AnchorPane>
And my mainApp Class :
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class mainApp extends Application {
private static BorderPane root;
private AnchorPane messagePane;
@Override
public void start(Stage primaryStage) throws Exception {
root = (BorderPane) FXMLLoader.load(getClass().getResource("view/mainView.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setTitle("My app");
setScene();
}
public static void main(String[] args) {
launch(args);
}
public void setScene() throws Exception {
messagePane = (AnchorPane) FXMLLoader.load(getClass().getResource("view/firstOverview.fxml"));
root.setBottom(messagePane);
}
}
What I tried :
I tried different things and it seems like the problem comes from the server. I tried to catch TCP request using Wireshark and when I click my Send Button, there is actualy a request catched by Wireshark. I guess that means my app correctly works...
I see on wireshark that the first message sent with mainConnection.sendMessage("Connection initialized")
in my initialize
method comes from the port 63867. That message is correctly received on my server.
But when I try to send a message using the button, the source port is now 63868 and nothing is received on th server... Maybe the problem comes from that alteration.
I have tried to make 3 different servers using Java and Python but no one works... So my app sends a message but it is never received by my server. I'm using a Connection Sharing from my phone... Don't know if it's impacting.
Sorry for spelling mistakes, I've done my best but I'm not good in english!
Upvotes: 0
Views: 191
Reputation: 11
Unfortunately, it was just a mistake coming from me... I was calling two times my controller. I realized and it's all ok now !
Upvotes: 1