Issue
I am trying to do a simple communication between a server (running on Ubuntu from Qemu with Cortex-A53 cpu) and a client (running on CentOS from my pc), using sockets.
If I run the C++ code only from Centos (both client.c and server.c) it works fine. Same if I run both from Ubuntu. But if I start the server.c from Ubuntu and client.c from CentOS the communication doesn't work.
The C code I'm using is from this tutorial: https://www.geeksforgeeks.org/socket-programming-cc/
The Qemu command:
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
When I run server.c from Qemu and client.c from my pc. I see that the server.c it's blocked at accept()
function and client.c it's blocked at read()
function.
If I run the following command on Ubuntu:
$ sudo lsof -i -P -n | grep LISTEN
I get this:
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)
If I run the same command on CentOS I get this:
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)
Any help is welcome.
Solution
The problem was that I was trying to connect to the socket from server part by using host address and port, but to access the Qemu data I had to connect to the socket using file descriptor /dev/vport3p1.
The server.c file should look something similar to this:
#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;
}
Answered By - yuy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.