2

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;
}
5
  • Post your IOS.getBytes() Commented Dec 23, 2010 at 18:06
  • This will only work if the thing that comes out of s._sock.recv(1024) is a character string "1592". Does your IOS.getBytes(x) do such string conversion and write the four ascii characters 1,5,9,2? Commented Dec 23, 2010 at 18:11
  • So do print s._sock.recv(1024) and see what you're getting! Commented Dec 23, 2010 at 18:23
  • 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; } Commented Dec 23, 2010 at 18:36
  • What I recieve from the server is ¼f ?sr ?java.lang.Integer?Gáñ˜üç8? ?I ?valuexr ?java.lang.Numberå¼ò??öaï? xp ? 9 Commented Dec 23, 2010 at 18:38

1 Answer 1

2
sock.getOutPutStream().write(Integer.toString(mPort))

s._PORT = int(s._sock.recv(1024))
Sign up to request clarification or add additional context in comments.

4 Comments

kind of worked. Now when I display it in python I can see what I'm sending. Did the conversion and got a ValueError, I expected this since I'm still getting a few special characters before the int string
Convert the string to byte[] (using for instance the IOS class in the question), and then this would work.
I did, had to, write() only takes byte[]'s.
I just did a work around, put an '@' in front of my int and just split it python side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.