Java client receives unexpedted data from Python server

Posted by: mabeljuhi on 25 October 2020, 3:57 pm EST

  • Posted 25 October 2020, 3:57 pm EST

    I was creating a python server with a java client, I manage to send the data from client to the server, however, when it is sent back, the value is not what I expect

    Python Server

    def handle_client(self, client): # Takes client socket as argument.

    “”“Handles a single client connection.”“”

    data = []
    
    # get size of data
    amt = client.recv(self.SIZE)
    amt = int.from_bytes(amt, byteorder="big", signed=True)
    
    
    client.send("True".encode())
    
    # get type of entre
    val = client.recv(self.SIZE)
    val = val.decode()
    val = val.split(", ")
    
    while amt > 0:
        data.append(val[len(val) - amt])
        amt -= 1
    
    if val[0] == "login":
        # check database
        u = User()
        check = u.login(val[1],val[2])
    
        if check:
            # add once login is confirmed
            self.clients[val[1]] = data
        else:
            b = "false"
            client.send(b.encode() + '\n')
    

    Java Client

    Socket s = new Socket();

    s.connect(new InetSocketAddress(voids[1], 7800), 2000);

    DataOutputStream dos = new DataOutputStream(this.s.getOutputStream());

    DataInputStream ois = new DataInputStream(this.s.getInputStream());

    sendPackets(this.al);

    b = ois.readByte();

    private void sendPackets(HashMap<String, Object> data) throws IOException, InterruptedException {

    if (data.containsKey(“Writer”)) {

    PrintWriter writer = (PrintWriter) data.get(“Writer”);

    data.remove(“Writer”);

    writer.write(data.size());

    writer.flush();

    } else if (data.containsKey(“ObjectStream”)) {

    DataOutputStream writer = (DataOutputStream)data.get(“ObjectStream”);

    data.remove(“ObjectStream”);

    DataInputStream reader = (DataInputStream)data.get(“InputStream”);

    data.remove(“InputStream”);

    writer.writeInt(data.size());

    writer.flush();

        String val = String.valueOf(data.get("type"));
    
        Byte buf = reader.readByte();
    
        if (data.containsKey("email")) {
            if (data.containsKey("type")) {
                val = val + ", ";
            }
            val = val + data.get("email");
        }
        if (data.containsKey("username")) {
            if (data.containsKey("email") || data.containsKey("type")) {
                val = val + ", ";
            }
            val = val + data.get("username");
        }
        if (data.containsKey("password")) {
            if (data.containsKey("username")) {
                val = val + ", ";
            }
            val = val + data.get("password");
        }
    
        writer.writeBytes(val);
    }
    

    }

    If any parts of the codes that are missing that is needed, please tell me

    When the string false is sent from Python to Java, the data received is 114. I do not think it is the value I sent from python as when I was debugging it, it receives 114 even when the python server has not sent the String yet

  • Posted 26 October 2020, 2:06 pm EST

    Hello,

    This is not a general forum. Please ask only ActiveReports question here.

    Thanks,

    Mohit

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels