Qemu 套接字通信
Qemu socket communication
我正在尝试在服务器(运行ning on Ubuntu from Qemu with Cortex-A53 cpu)和客户端(运行从我的电脑在 CentOS 上运行),使用套接字。
如果我 运行 仅来自 Centos 的 C++ 代码(client.c 和 server.c)它工作正常。如果我 运行 都来自 Ubuntu,则相同。但是,如果我从 Ubuntu 启动 server.c 并从 CentOS 启动 client.c,则通信无法正常工作。
我使用的 C 代码来自本教程:
https://www.geeksforgeeks.org/socket-programming-cc/
Qemu 命令:
qemu-system-aarch64 -m 2048 -smp 2 -cpu cortex-a53 -M virt -nographic \
-pflash flash0.img \
-pflash flash1.img \
-drive if=none,file=${IMAGE},format=qcow2,id=hd0 -device virtio-blk-device,drive=hd0 \
-drive if=none,id=cloud,file=cloud.img,format=qcow2 \
-device virtio-blk-device,drive=cloud \
-device virtio-net-device,netdev=user0 \
-netdev user,id=user0,hostfwd=tcp::10022-:22 \
-device virtio-serial \
-chardev socket,id=foo,host=localhost,port=8080,server,nowait \
-device virtserialport,chardev=foo,name=test0
当我从 Qemu 运行 server.c 和我的电脑 client.c 时。我看到 server.c 在 accept()
函数处被阻止,client.c 在 read()
函数处被阻止。
如果我 运行 在 Ubuntu 上执行以下命令:
$ sudo lsof -i -P -n | grep LISTEN
我明白了:
systemd-r 644 systemd-resolve 13u IPv4 15994 0t0 TCP 127.0.0.53:53 (LISTEN)
sshd 745 root 3u IPv4 18696 0t0 TCP *:22 (LISTEN)
sshd 745 root 4u IPv6 18699 0t0 TCP *:22 (LISTEN)
server 14164 root 3u IPv4 74481 0t0 TCP *:8080 (LISTEN)
如果我 运行 在 CentOS 上执行相同的命令,我会得到:
qemu-syst 57073 ibestea 10u IPv4 2648807035 0t0 TCP 127.0.0.1:8080 (LISTEN)
qemu-syst 57073 ibestea 13u IPv4 2648807037 0t0 TCP *:10022 (LISTEN)
欢迎任何帮助。
问题是我试图通过使用主机地址和端口从服务器部分连接到套接字,但要访问 Qemu 数据我必须使用文件描述符 /dev/vport3p1 连接到套接字。
server.c 文件应类似于以下内容:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#define PORT 8080
#define DEV_PATH "/dev/vport3p1"
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
int dev_fd;
printf("SERVER: get an internet domain socket\n");
if ((dev_fd = open(DEV_PATH, O_RDWR)) == -1) {
perror("open");
exit(1);
}
else {
printf("SERVER: opened file descriptor first time = %d\n", dev_fd);
}
valread = read( dev_fd , buffer, 1024);
printf("%s\n",buffer );
valread = write(dev_fd , hello , strlen(hello));
printf("Hello message sent\n");
return 0;
}
我正在尝试在服务器(运行ning on Ubuntu from Qemu with Cortex-A53 cpu)和客户端(运行从我的电脑在 CentOS 上运行),使用套接字。
如果我 运行 仅来自 Centos 的 C++ 代码(client.c 和 server.c)它工作正常。如果我 运行 都来自 Ubuntu,则相同。但是,如果我从 Ubuntu 启动 server.c 并从 CentOS 启动 client.c,则通信无法正常工作。
我使用的 C 代码来自本教程: https://www.geeksforgeeks.org/socket-programming-cc/
Qemu 命令:
qemu-system-aarch64 -m 2048 -smp 2 -cpu cortex-a53 -M virt -nographic \
-pflash flash0.img \
-pflash flash1.img \
-drive if=none,file=${IMAGE},format=qcow2,id=hd0 -device virtio-blk-device,drive=hd0 \
-drive if=none,id=cloud,file=cloud.img,format=qcow2 \
-device virtio-blk-device,drive=cloud \
-device virtio-net-device,netdev=user0 \
-netdev user,id=user0,hostfwd=tcp::10022-:22 \
-device virtio-serial \
-chardev socket,id=foo,host=localhost,port=8080,server,nowait \
-device virtserialport,chardev=foo,name=test0
当我从 Qemu 运行 server.c 和我的电脑 client.c 时。我看到 server.c 在 accept()
函数处被阻止,client.c 在 read()
函数处被阻止。
如果我 运行 在 Ubuntu 上执行以下命令:
$ sudo lsof -i -P -n | grep LISTEN
我明白了:
systemd-r 644 systemd-resolve 13u IPv4 15994 0t0 TCP 127.0.0.53:53 (LISTEN)
sshd 745 root 3u IPv4 18696 0t0 TCP *:22 (LISTEN)
sshd 745 root 4u IPv6 18699 0t0 TCP *:22 (LISTEN)
server 14164 root 3u IPv4 74481 0t0 TCP *:8080 (LISTEN)
如果我 运行 在 CentOS 上执行相同的命令,我会得到:
qemu-syst 57073 ibestea 10u IPv4 2648807035 0t0 TCP 127.0.0.1:8080 (LISTEN)
qemu-syst 57073 ibestea 13u IPv4 2648807037 0t0 TCP *:10022 (LISTEN)
欢迎任何帮助。
问题是我试图通过使用主机地址和端口从服务器部分连接到套接字,但要访问 Qemu 数据我必须使用文件描述符 /dev/vport3p1 连接到套接字。
server.c 文件应类似于以下内容:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#define PORT 8080
#define DEV_PATH "/dev/vport3p1"
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
int dev_fd;
printf("SERVER: get an internet domain socket\n");
if ((dev_fd = open(DEV_PATH, O_RDWR)) == -1) {
perror("open");
exit(1);
}
else {
printf("SERVER: opened file descriptor first time = %d\n", dev_fd);
}
valread = read( dev_fd , buffer, 1024);
printf("%s\n",buffer );
valread = write(dev_fd , hello , strlen(hello));
printf("Hello message sent\n");
return 0;
}