windowsとvirtualbox上のcentos間を通信させたいのですが,上手くいきません.
Windows-Linux間をTCPで通信するを参考にしたのですが,クライアント側であるwindows側で受信ができていません.
問題として何が考えられるのでしょうか?
構成
- server :CentOS 7, x86_64(64bit) on Virtualbox
- client :Windows10 (64bit)
CentOS および virtualbox は現時点で最新のものをインストールしました(したつもりです).
- CentOS (http://ftp.tsukuba.wide.ad.jp/Linux/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso)
- VirtualBox (https://www.virtualbox.org/wiki/Downloads/VirtualBox-5.2.22-126460-Win.exe)
クライアント側
visual studio 2017 でビルドしています.
c++
1#include "stdafx.h" 2 3#include <stdio.h> 4#include <winsock.h> 5 6#define PORT_NO_SERVER (12345) 7#define IP_ADDRESS_SERVER "192.168.55.254" 8 9int main(void) 10{ 11 WSADATA wsaData; 12 struct sockaddr_in server; 13 SOCKET sfd = -1; 14 char buf[32] = { 0 }; 15 int rc = 0; 16 17 WSAStartup(MAKEWORD(2, 0), &wsaData); 18 19 sfd = socket(AF_INET, SOCK_STREAM, 0); 20 21 server.sin_family = AF_INET; 22 server.sin_port = htons(PORT_NO_SERVER); 23 server.sin_addr.S_un.S_addr = inet_addr(IP_ADDRESS_SERVER); 24 25 rc = connect(sfd, (struct sockaddr *)&server, sizeof(server)); 26 if (rc < 0) { 27 printf("connect() failed(%d)\n", rc); 28 exit(EXIT_FAILURE); 29 } 30 while (1) { 31 memset(buf, 0, sizeof(buf)); 32 int rs = recv(sfd, buf, sizeof(buf), 0); 33 34 printf("recv %d byte, %s\n", rs, buf); 35 } 36 37 WSACleanup(); 38 39 return 0; 40}
windows側から見たvirtualbox host-only network.
サーバ側
centos 上の VSCode でプログラムを記述し,gcc でコンパイルしました.
C
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <unistd.h> 5#include <sys/types.h> 6#include <sys/socket.h> 7#include <netinet/in.h> 8#include <errno.h> 9 10#define MAX_CLIENTS (1) 11#define MSGBUF_SIZE (1024) 12 13int main(void) 14{ 15 int sfd = -1; 16 struct sockaddr_in client; 17 int socklen = sizeof(client); 18 int ac_sfd = -1; 19 int rc = 0; 20 21 struct sockaddr_in addr = { 22 .sin_family = AF_INET, 23 .sin_port = htons(12345), 24 .sin_addr = { 25 .s_addr = INADDR_ANY, 26 }, 27 }; 28 29 /* ソケットの作成 */ 30 sfd = socket(AF_INET, SOCK_STREAM, 0); 31 if (sfd < 0) { 32 perror("socket"); 33 goto error_end; 34 } 35 36 rc = bind(sfd, (struct sockaddr *)&addr, sizeof(addr)); 37 if (rc < 0) { 38 perror("bind"); 39 goto close_sfd_end; 40 } 41 42 rc = listen(sfd, MAX_CLIENTS); 43 if (rc < 0) { 44 perror("listen"); 45 goto close_sfd_end; 46 } 47 48 /* 要求受付 */ 49 ac_sfd = accept(sfd, (struct sockaddr *)&client, &socklen); 50 if (ac_sfd < 0) { 51 perror("accept"); 52 goto close_sfd_end; 53 } 54 55 while (1) { 56 /* メッセージ送信 */ 57 char msgbuf[MSGBUF_SIZE] = "HELLO WORLD!"; 58 ssize_t ws = write(ac_sfd, msgbuf, strlen(msgbuf)); 59 if (ws < 0) { 60 perror("write"); 61 goto close_all_end; 62 } 63 64 sleep(1); 65 } 66 67 close_all_end: 68 close(ac_sfd); 69 close_sfd_end: 70 close(sfd); 71 error_end: 72 73 return 0; 74}
- (VSCode上でいくつかのヘッダファイルを開けないと言われているが関係ある???)
⇒stdio.hも該当しているのですが,以前IDEを用いないでcのプログラムをコンパイルした際に,特に併せてコンパイルしなくても機能を利用できたため関係ないと考えています.
- windows -> centos の ping は通っている
- centos -> windows の ping は通らない
→ネットワーク構成の問題?
⇒以前質問させていただいた,ネットワーク構成についての疑問
以上,お気づきの点がございましたらお教えください.
よろしくお願いいたします.
回答3件
あなたの回答
tips
プレビュー