So while programming sockets using Java and Python, I stumbled upon something weird.
When sending a message using Java to the receiving end of the Python socket, it splits the message into 2 parts, even though this was not intended.
I probably made a mistake somewhere that's causing this problem, but I really don't know what it is.
You can see that Java sends "Test1" in one command and Python only receives parts of that message:
https://i.stack.imgur.com/0827b.png
Pyhton Server Socket Source:
'''
Created on 23 okt. 2014
@author: Rano
'''
#import serial
import socket
HOST = ''
PORT = 1234
running = True;
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
skt.bind((HOST, PORT))
skt.listen(1)
conne, addr = skt.accept()
#ser = serial.Serial('/dev/tty.usbmodem411', 9600)
while running == True:
data = conne.recvall(1024)
if(data == "quit"):
running = False
break
rawrecvstring = data + ""
recvstring = rawrecvstring.split("|")
print(recvstring[0])
#_______________________ABOVE IS RECEIVE_______________UNDER IS SEND_______________________#
# sendstring = ser.readline()
# if sendstring != "":
# conne.sendall(sendstring)
conne.close()
#ser.close()
And the Java Socket send function:
private String message;
private DataOutputStream out;
private BufferedReader in;
private Socket socket;
private boolean socketOnline;
public SocketModule(String IP, int Port){
try {
socket = new Socket(IP, Port);
out = new DataOutputStream(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
void setMessage(String s){
try {
out.writeBytes(s);
out.flush();
System.out.println("message '" + s + "' sent!\n");
} catch (IOException e) {
e.printStackTrace();
}
};
Any ideas as to why the message is being split?