Can't send data with socket and portfowarding with out clothing it, I need to send and receive data throught portfowarding without closing the socket

夙愿已清 提交于 2020-01-03 02:23:27

问题


For a school project I need to do an app that is able to send an receive data with out closing the connection. To do that I have made a draft code that his the code below and on the Server rooter I have open the port with portfowarding, I check that the port was open with https://www.yougetsignal.com/tools/open-ports/. The problem it's that the server that his a Raspberry pi 3b+ can only receive data after the client close the communication. When the server need to read the data that have been send and after send something to the client without closing the socket. Why do I need to close the socket to receive that data when the socket pass by portfowarding, the code work okay when the client his on the same rooter and it connects with the private ip

what works with private ip

-can send and receive data without closing

-if the message send his bigger than 16 bytes it receive the first 16 bytes

-works with serveur that use thread

what works with public ip

-can connect to the serveur

-can receive data if only printwriter close after sending

Test I have done

Execute the code on different operating system

Turn off the firewall of the server

Try to send between a socket server and a python client

check that the port is open

My track

I think that it's the fonction printwriter that doesn't fluch. It can may work with a fonction write that check that the message has been correctly send or something that force to flush, like close but that doesn't close the communication.

The client code

 sock = new Socket( "this code onely work with the private ip , can't send any message if we don't cloth the socket when we use the public ip", 1234);
                    sock.setTcpNoDelay(true);


                    printWriter = new PrintWriter( sock.getOutputStream(),true);
                    try {




                        Thread client = new Thread() {
                            public void run() {
                                printWriter.write( "hello world" );

                                System.out.println("error for write" + printWriter.checkError());
                                printWriter.close();
                                printWriter.flush();

                            }
                        };

                        client.start();
                        inputStreamReader = new InputStreamReader(sock.getInputStream());
                        bufferedReader = new BufferedReader(inputStreamReader); // Obtention du message du client
                        message_entrant = bufferedReader.readLine();
                        sock.close();

The server code

# coding: utf-8

import socket
import threading

class ClientThread(threading.Thread):

    def __init__(self, ip, port, clientsocket):

        threading.Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.clientsocket = clientsocket
        print("[+] Nouveau thread pour %s %s" % (self.ip, self.port, ))

    def run(self):

        print("Connexion de %s %s" % (self.ip, self.port, ))
        print("staart fonction recv")
        r = self.clientsocket.recv(2048)
        print("Ouverture du fichier: ", r, "...")
        fp =("hello, i'm the serveur the code worked")
        self.clientsocket.send(fp)
        self.clientsocket.close()
        print("Client déconnecté...")

tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind(("",1234))


tcpsock.listen(10)
print( "En écoute...")
(clientsocket, (ip, port)) = tcpsock.accept()
newthread = ClientThread(ip, port, clientsocket)
newthread.start()

what prints the serveur when i close printwriter

En écoute...
[+] Nouveau thread pour  54112
Connexion de 86.205.206.28 54112

staart fonction recv
hello world i li
('Ouverture du fichier: ', 'hello world i li', '...')
socket send
Client déconnecté...

Thanks for helping. If I've not been clear please tell me

来源:https://stackoverflow.com/questions/59436409/cant-send-data-with-socket-and-portfowarding-with-out-clothing-it-i-need-to-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!