Client Socket Connection to Java server

T

TechieInsights

I am having problems with a socket connection to a Java server. In
java I just open the socket, pass the length and then pass the bits
across the socket.

I created a socket object:

import socket

class MySocket:
def __init__(self, host='localhost', port = 28192, buffsize = 1024):
socket.setdefaulttimeout(10)

self.host = host
self.port = port
self.buffsize = buffsize
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((host, port))

def send(self, data):
self.socket.send(data)

def receive(self):
return self.socket.recv(self.buffsize)

def sendAndReceive(self, data):
self.send(data)
return self.receive()

def close(self):
self.socket.close()

But the java server gives the error:
WARNING: <Incoming> Message length invalid. Discarding

The data is of type string (xml). Am I doing something wrong? I know
you have to reverse the bits when communicating from C++ to Java.
Could this be the problem? I figured it would not because it said the
length was invalid. I just started looking at python sockets
tonight... and I don't have a real deep base with socket connections
as it is... any help would be greatly appreciated.

Greg
 
B

bieffe62

I am having problems with a socket connection to a Java server.  In
java I just open the socket, pass the length and then pass the bits
across the socket.

I created a socket object:

import socket

class MySocket:
        def __init__(self, host='localhost', port = 28192, buffsize = 1024):
                socket.setdefaulttimeout(10)

                self.host = host
                self.port = port
                self.buffsize = buffsize
                self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.socket.connect((host, port))

        def send(self, data):
                self.socket.send(data)

        def receive(self):
                return self.socket.recv(self.buffsize)

        def sendAndReceive(self, data):
                self.send(data)
                return self.receive()

        def close(self):
                self.socket.close()

But the java server gives the error:
WARNING: <Incoming> Message length invalid.  Discarding

The data is of type string (xml).  Am I doing something wrong?  I know
you have to reverse the bits when communicating from C++ to Java.
Could this be the problem? I figured it would not because it said the
length was invalid.  I just started looking at python sockets
tonight... and I don't have a real deep base with socket connections
as it is... any help would be greatly appreciated.

Greg

What is the server protocol? What are you sending? Saying 'xml' is not
enough to understand the problem ...
byte order could be a problem only if the message include binary
representations
of 2-bytes or 4-bytes integer. With XML this should not be the case

Ciao
 
M

MRAB

TechieInsights said:
I am having problems with a socket connection to a Java server. In
java I just open the socket, pass the length and then pass the bits
across the socket.

I created a socket object:

import socket

class MySocket:
def __init__(self, host='localhost', port = 28192, buffsize = 1024):
socket.setdefaulttimeout(10)

self.host = host
self.port = port
self.buffsize = buffsize
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((host, port))

def send(self, data):
self.socket.send(data)

I recommend sendall() instead of send():

self.socket.sendall(data)

send() doesn't guarantee to send all the data, so multiple sends might
be needed to send it all. sendall() does that for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top