I'm trying to make a communication system between python server and php client. Php client will connect to python server and send a message and close to make completely. But when I connect and send message again, python server can't receive this message while in opening then client can't get you are client111111111111111111111111111111111111111111
line, but when I telnet on commandline to connect to server then everything work well. Please help! Sorry my bad english.
Here is my code.
python server
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()
try:
print >>sys.stderr, 'connection from', client_address
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(1024)
print >>sys.stderr, 'received "%s"' % data
if data:
print >>sys.stderr, 'sending data back to the client'
connection.sendall('you are client111111111111111111111111111111111111111111')
else:
print >>sys.stderr, 'no more data from', client_address
break
finally:
# Clean up the connection
connection.close()
php client
socket.php
<?php
$fp = fsockopen("127.0.0.1", 10000, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />
";
} else {
fwrite($fp, "You message");
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
Update question : Message sent via ajax when press a button event
<script type="text/javascript">
function buttonPress() {
$.ajax({
type : 'post',
url : 'socket.php',
success : function(mess){
}
});
}
</script>