TCP请求由客户端发送,但服务器未接收到,使用JavaFX客户端和Python服务器。

huangapple go评论106阅读模式
英文:

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(&quot;127.0.0.1&quot;, 1500);
	    } catch (IOException e) {
		    e.printStackTrace();
	    }
	    mainConnection.sendMessage(&quot;Connection initialized&quot;);

    }

}

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(&quot;Connection initialized&quot;);
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((&quot;127.0.0.1&quot;,1500))
server.listen(5)

connexion, info = server.accept()
print(info)

while True :
	message = connexion.recv(4096)
	if (message != b&quot;&quot;):
		print(message)

connexion.close()
server.close()

firstOverview.fxml file :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.control.TextField?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;

&lt;AnchorPane prefHeight=&quot;671.0&quot; prefWidth=&quot;789.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;com.rfaur.spokee.MainController&quot;&gt;
	&lt;children&gt;
		&lt;TextField fx:id=&quot;messageEntry&quot; layoutX=&quot;91.0&quot; layoutY=&quot;630.0&quot; prefHeight=&quot;26.0&quot; prefWidth=&quot;683.0&quot; /&gt;
		&lt;Button layoutX=&quot;14.0&quot; layoutY=&quot;630.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#sendMessageAction&quot; text=&quot;Envoyer&quot; /&gt;
	&lt;/children&gt;
&lt;/AnchorPane&gt;

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(&quot;view/mainView.fxml&quot;));
		Scene scene = new Scene(root);
		primaryStage.setScene(scene);
		primaryStage.show();
		primaryStage.setTitle(&quot;My app&quot;);
		setScene();

	}

	public static void main(String[] args) {
		launch(args);
	}

	public void setScene() throws Exception {
		messagePane = (AnchorPane) FXMLLoader.load(getClass().getResource(&quot;view/firstOverview.fxml&quot;));
		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(&quot;Connection initialized&quot;) 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 !

huangapple
  • 本文由 发表于 2020年7月31日 21:54:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63193198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定