我有非常具体的问题,我不知道如何摆脱它。我在网络服务器上通过php客户端发送一些数据。代码如下所示:
<?php
session_start();
include 'db_con.php';
include('db_functions.php');
error_reporting(E_ALL);
/* Get the port for the WWW service. */
$service_port = $_SESSION['port'];
/* Get the IP address for the target host. */
$address = $_SESSION['ip'];
/* Create a TCP/IP socket. */
try {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
} catch (Exception $e) {
echo "error" . socket_strerror(socket_last_error()) . "\n";
socket_close($socket);
}
try {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
} catch (Exception $e) {
echo "error" . socket_strerror(socket_last_error()) . "\n";
socket_close($socket);
}
$result = socket_connect($socket, $address, $service_port);
//data from database soonTM
$in = returnResults($db);
$data = "";
foreach ($in as $r){
$data .= $r['ring_time'] . ' ' . $r['bell_mode'] . "\r\n";
}
socket_write($socket, $data, strlen($data));
echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
socket_close($socket);
?>
然后将来自数据库的数据发送到TCP服务器并且它工作得很好,因为我得到的数据和我发送的完全相同。
无论如何这是我的TCP服务器代码:
import socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('10.10.10.120', 10000)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)
while True:
# Listen for incoming connections
sock.listen(1)
# Wait for a connection
print('waiting for a connection')
connection, client_address = sock.accept()
try:
print('connection from', client_address)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(2048)
neki = 'hello rok'
print('received "%s"' % data)
if data:
try:
dataFile = open('data.txt', 'w')
try:
dataFile.write(data)
except (Exception) as inst:
print(type(inst))
print("error at writing to file")
except (Exception) as inst:
print(type(inst))
print("error opening file")
print('sending data back to the client')
#connection.sendall(bytes(neki, 'UTF-8'))
connection.sendall(neki)
break
except (Exception) as inst:
print(type(inst))
print("error opening connection")
finally:
# Clean up the connection
connection.close()
它似乎工作得很好,因为在终端它打印我通过TCP客户端发送的东西。但问题是写入data.txt文件。当我第一次发送数据时,不会将任何内容写入文件。如果我再次发送它将按预期工作(即使正确的数据打印在终端中)。如果我将另一行添加到我的数据库并再次发送,旧值将再次写入文件。如果我再次发送,则会添加新值。
答案 0 :(得分:0)
您需要关闭文件。我认为这个问题。请尝试最后添加&#39;阻止这样:
f1 <- function(x) {
if (length(x) > 7) {
return(subset(x, x < quantile(x, probs=0.025))) # only for low outliers
} else {
return(NA)
}
}