gpt4 book ai didi

python - 来回 unix 域套接字锁

转载 作者:行者123 更新时间:2023-11-28 06:47:51 26 4
gpt4 key购买 nike

我正在编写两个程序,一个用 C++ 编写,另一个用 Python 编写,以使用 unix 域套接字相互通信。我想做的是让 C++ 代码向 Python 代码发送一个数字,Python 代码又将另一个数字发送回 C++。这一直持续到 c++ 代码用完要发送的数字并且执行停止。以下是我的代码。我似乎无法在循环的第一次迭代之后运行它们。

我先运行 Python:
python code.py/tmp/1/tmp/2

然后我运行 C++ 代码:
./code/tmp/1/tmp/2

这是输出:

C++ 输出:

发送0个
听力
连接成功
收到5
发送 1
听力



Python 输出:

听...
收到 (0,)
>5
发送 5
听...

C++代码:

static int connFd;

int main(int argc, char* argv[])
{

int recv_sock,
send_sock;

struct sockaddr_un server, client;

///////////////////////////////////////////
//
// setup send
//
///////////////////////////////////////////

/* Create socket on which to send. */
send_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (send_sock < 0)
{
perror("opening unix socket");
exit(1);
}

/* Construct name of socket to send to. */
client.sun_family = AF_UNIX;

strcpy(client.sun_path, argv[1]);

if (connect(send_sock, (struct sockaddr *) &client, sizeof(struct sockaddr_un)) < 0)
{
close(send_sock);
perror("connecting stream socket");
exit(1);
}


///////////////////////////////////////////
//
// setup recv
//
///////////////////////////////////////////

recv_sock = socket(AF_UNIX, SOCK_STREAM, 0);

if(recv_sock< 0)
{
cerr << "Cannot open socket" << endl;
return 0;
}

bzero((char*) &server, sizeof(server));

server.sun_family = AF_UNIX;
strcpy(server.sun_path, argv[2]);

//bind socket
if(bind(recv_sock, (struct sockaddr *)&server, sizeof(server)) < 0)
{
cerr << "Cannot bind" << endl;
return 0;
}

listen(recv_sock, 10);

int X;
for (int i = 0; i < 10; i++)
{
write(send_sock, &i, sizeof(i));
cout << "sent " << i << endl;
cout << "Listening" << endl;
connFd = accept(recv_sock, 0, 0);
if (connFd < 0)
{
cerr << "Cannot accept connection" << endl;
return 0;
}
else
{
cout << "Connection successful" << endl;
read(connFd, &X, sizeof(X));
cout << "received " << X << endl;
}

usleep(2000000);

}


close(send_sock);
close(recv_sock);
unlink(argv[2]);
unlink(argv[1]);


return 0;

}

Python代码:

import socket,os,struct, glob, sys
import random

send_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
recv_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
os.remove(sys.argv[1])
except OSError:
pass

recv_socket.bind(sys.argv[1])



recv_socket.listen(10)
while 1:
print "listening ..."
conn, addr = recv_socket.accept()
data = conn.recv(4)


p = struct.unpack('i',data)
print 'received ', p

if p is '9':
break


l = int(raw_input(">"))
a = struct.pack('i', l)
send_socket.connect(sys.argv[2])
send_socket.sendall(a)
print 'sent ', l

send_socket.close()
conn.close()
recv_socket.close()

我在这种方法中做错了什么?我需要使用线程吗?

谢谢

最佳答案

您在 C++ 代码中以不同方式处理发送和接收套接字:发送套接字在程序开始时绑定(bind)一次,而接收套接字在每次迭代时接受一个新连接。

您当前的 Python 实现在 recv_socket 上接受新连接,并在每次迭代时连接 send_socket,这解释了您面临的问题。

最有效的修复方法是在循环之前连接每个套接字一次,除非您有充分的理由在每次迭代时打开一个新连接。以下是相应的代码 list :

C++

static int connFd;

int main(int argc, char *argv[]) {

int recv_sock, send_sock;

struct sockaddr_un server, client;

///////////////////////////////////////////
//
// setup send
//
///////////////////////////////////////////

/* Create socket on which to send. */
send_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (send_sock < 0) {
perror("opening unix socket");
exit(1);
}

/* Construct name of socket to send to. */
client.sun_family = AF_UNIX;

strcpy(client.sun_path, argv[1]);

if (connect(send_sock, (struct sockaddr *)&client,
sizeof(struct sockaddr_un)) < 0) {
close(send_sock);
perror("connecting stream socket");
exit(1);
}

///////////////////////////////////////////
//
// setup recv
//
///////////////////////////////////////////

recv_sock = socket(AF_UNIX, SOCK_STREAM, 0);

if (recv_sock < 0) {
cerr << "Cannot open socket" << endl;
return 0;
}

bzero((char *)&server, sizeof(server));

server.sun_family = AF_UNIX;
strcpy(server.sun_path, argv[2]);

// bind socket
if (::bind(recv_sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
cerr << "Cannot bind" << endl;
return 0;
}

listen(recv_sock, 10);
connFd = accept(recv_sock, 0, 0);
if (connFd < 0) {
cerr << "Cannot accept connection" << endl;
return 0;
} else {
cout << "Connection successful" << endl;
}

int X;
for (int i = 0; i < 10; i++) {
write(send_sock, &i, sizeof(i));
cout << "sent " << i << endl;
cout << "Listening" << endl;
read(connFd, &X, sizeof(X));
cout << "received " << X << endl;
usleep(2000000);
}

close(send_sock);
close(recv_sock);
unlink(argv[2]);
unlink(argv[1]);

return 0;
}

python

recv_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
send_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
os.remove(sys.argv[1])
except OSError:
pass

recv_socket.bind(sys.argv[1])

recv_socket.listen(10)
conn, addr = recv_socket.accept()

send_socket.connect(sys.argv[2])

while 1:
print "listening ..."
data = conn.recv(4)
p = struct.unpack('i',data)
print 'received ', p

if p is '9':
break


l = int(raw_input(">"))
a = struct.pack('i', l)
send_socket.sendall(a)
print 'sent ', l

send_socket.close()
conn.close()
recv_socket.close()

关于python - 来回 unix 域套接字锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24632280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com