英文:
TCP request sent by client but never reveived by server using JavaFX client and Python server
问题
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 ipAddressString, int port) throws UnknownHostException, IOException {
socket = new Socket(ipAddressString, 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 correctly receives this message. However, when I click the button and call the method sendMessage()
, nothing happens, and my server doesn't receive anything, but no error appears. Moreover, the method is correctly called because the System.out.println(message)
prints 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)
connection, info = server.accept()
print(info)
while True:
message = connection.recv(4096)
if message != b"":
print(message)
connection.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 requests using Wireshark, and when I click my Send Button, there is actually a request caught by Wireshark. I guess that means my app works correctly.
I see on Wireshark that the first message sent with mainConnection.sendMessage("Connection initialized")
in my initialize
method comes from port 63867, and 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 the server. Maybe the problem comes from that alteration.
I have tried to make three different servers using Java and Python, but none of them work. My app sends a message, but it is never received by my server. I'm using Connection Sharing from my phone; I don't know if it's impacting.
Sorry for any spelling mistakes; I've done my best, but I'm not good in English!
英文:
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!
答案1
得分: 1
很抱歉,这只是我犯的一个错误... 我调用了两次我的控制器。我意识到了,现在一切都没问题了!
英文:
Unfortunately, it was just a mistake coming from me... I was calling two times my controller. I realized and it's all ok now !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论