为什么我的插座不工作?

时间:2015-12-14 19:22:01

标签: python sockets

我已经[Errno 10048],然后如果我解决了,我得到[Errno 10013],如果我解决了,我得到[Errno 10053],如果我解决了,我得到[Errno 10048]再次。我的代码出了什么问题?

这是服务器:

#!/usr/bin/env python

import socket

host = ''
port = 65535
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(backlog)
while 1:
    client, address = s.accept()
    data = client.recv(size)
    if data:
        client.send(data)
    client.close() 

这是我的客户:

#!/usr/bin/env python

import socket

host = 'localhost'
port = 65535
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
    txt = raw_input('')
    s.send(txt)

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

您的客户端从不连接到服务器(请注意portimport socket host = 'localhost' port = 65535 size = 1024 while True: txt = raw_input('') s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) s.send(txt) r = s.recv(size) print r s.close() 变量未使用。由于服务器在每次回显后关闭连接,我通过在每次用户输入后重新创建连接来修复客户端:

include_once 'PHPExcel.php';
$sheet = new PHPExcel();


$servername =
$username = 
$password =
$dbname = 
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT firstname, lastname, phonenumber FROM people where answer='true' LIMIT ".$start.",10 ";

$result = $conn->query($sql);


if ($result->num_rows > 0) {
        // output data of each row
        $activeSheet=$sheet->getActiveSheet();
        while($row = $result->fetch_assoc()) {

        $activeSheet->setCellValue('A1',' $row["firstname"]');
        $activeSheet->setCellValue('B1','$row["lastname"]');
        $activeSheet->setCellValue('C1','$row["phonenumber"]');
        }
    } else {
        echo "0 results";
}
$conn->close();         
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="report.xlsx"');
header('Cache-Control: max-age=0');

$objWriter=PHPExcel_IOFactory::createWriter($sheet,'Excel2007');
$objWriter->save('php://output'); 
exit;