Search code examples
javapythonsocketsprimitive

Java int to python int


I have a Java server that will accept a connection from a python socket and return an int of the new port that they python script should connect to. However the 'string" that the python script gets back is blatantly unable to be converted.

Here's the python snippet:

s._PORT = int(s._sock.recv(1024))
print s._PORT

And the java side:

int mPort = 1592;
sock.getOutPutStream().write(IOS.getBytes(mPort++));
// IOS being a utility class that I wrote just to make byte conversions easier

Thanks ~Aedon

public static byte[] 
getBytes(Object obj) 
throws java.io.IOException 
{
    ByteArrayOutputStream bos= new ByteArrayOutputStream(); 
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(obj); 
    oos.flush(); 
    oos.close(); 
    bos.close(); 
    byte[] data = bos.toByteArray(); 
    return data;
}

Solution

  • sock.getOutPutStream().write(Integer.toString(mPort))
    
    s._PORT = int(s._sock.recv(1024))