ludwig125のブログ

頑張りすぎずに頑張る父

c++のメモ

概要

c++についての自分用のメモ書き

g++

g++ がサポートしている規格

$ g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 $  g++ -v --help 2>/dev/null | grep -E "^\s+\-std=.*$"
  -std=<standard>          Assume that the input sources are for <standard>
  -std=f2003                  Conform to the ISO Fortran 2003 standard
  -std=f2008                  Conform to the ISO Fortran 2008 standard
  -std=f2008ts                Conform to the ISO Fortran 2008 standard
  -std=f95                    Conform to the ISO Fortran 95 standard
  -std=gnu                    Conform to nothing in particular
  -std=legacy                 Accept extensions to support legacy code
  -std=c++03                  Conform to the ISO 1998 C++ standard revised by
  -std=c++0x                  Deprecated in favor of -std=c++11
  -std=c++11                  Conform to the ISO 2011 C++ standard
  -std=c++1y                  Conform to the ISO 201y(7?) C++ draft standard
  -std=c++98                  Conform to the ISO 1998 C++ standard revised by
  -std=c11                    Conform to the ISO 2011 C standard (experimental
  -std=c1x                    Deprecated in favor of -std=c11
  -std=c89                    Conform to the ISO 1990 C standard
  -std=c90                    Conform to the ISO 1990 C standard
  -std=c99                    Conform to the ISO 1999 C standard
  -std=c9x                    Deprecated in favor of -std=c99
  -std=gnu++03                Conform to the ISO 1998 C++ standard revised by
  -std=gnu++0x                Deprecated in favor of -std=gnu++11
  -std=gnu++11                Conform to the ISO 2011 C++ standard with GNU
  -std=gnu++1y                Conform to the ISO 201y(7?) C++ draft standard
  -std=gnu++98                Conform to the ISO 1998 C++ standard revised by
  -std=gnu11                  Conform to the ISO 2011 C standard with GNU
  -std=gnu1x                  Deprecated in favor of -std=gnu11
  -std=gnu89                  Conform to the ISO 1990 C standard with GNU
  -std=gnu90                  Conform to the ISO 1990 C standard with GNU
  -std=gnu99                  Conform to the ISO 1999 C standard with GNU
  -std=gnu9x                  Deprecated in favor of -std=gnu99
  -std=iso9899:1990           Conform to the ISO 1990 C standard
  -std=iso9899:199409         Conform to the ISO 1990 C standard as amended in
  -std=iso9899:1999           Conform to the ISO 1999 C standard
  -std=iso9899:199x           Deprecated in favor of -std=iso9899:1999
  -std=iso9899:2011           Conform to the ISO 2011 C standard (experimental

引数

引数を2倍して出力する

参考 - C++で文字列を数値に変換する方法 - C++と色々 - http://www7b.biglobe.ne.jp/~robe/cpphtml/html03/cpp03042.html

https://github.com/ludwig125/work/blob/master/src/cpp/test/argv.cpp

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char** argv)
{
    for(int i = 1; i < argc; i++){
        cout << "argv "<< i << " : " << argv[i] << endl;
        cout << "double argv: " << atoi(argv[i])*2 << endl;
    }
    return 0;
}

実行結果

$ g++ argv.cpp 
[~/git/work/src/cpp/test ] $ ./a.out 13 83
argv 1 : 13
double argv: 26
argv 2 : 83
double argv: 166

型変換

型を確認

#include <iostream>                                                     
#include <stdlib.h>
#include <typeinfo>
using namespace std;

int main(int argc, char** argv)
{
    int a,b;
    for(int i = 1; i < argc; i++){
        cout << "argv "<< i << " : " << argv[i] << endl;
    }

  // 引数の型
    cout << typeid(argv[1]).name() << " : " << argv[1] << endl;

    a = atoi(argv[1]);
    b = atoi(argv[2]);

    // 変換後の型
    cout << typeid(a).name() << " : " << a << endl;

    return 0;
}

実行結果

$ ./a.out 10 30
argv 1 : 10
argv 2 : 30
Pc : 10
i : 10

typeidの出力

Uses of typeid in C++ - Codeforces

bool = b
char = c
unsigned char = h
short = s
unsigned short = t
int = i
unsigned int = j
long = l
unsigned long = m
long long = x
unsigned long long = y
float = f
double = d
long double = e
string = Ss
int[] = A_i
double[] = A_d
vector<int> = St6vectorIiSaIiE

c++のstringへの変換について

to_stringが使えない

あまり考えずにto_stringを使おうとしたら、以下のコンパイルエラーが出た

error: ‘to_string’ is not a member of ‘std’

c++11にするとか、コンパイル時の指定が必要とか書いてある c++ - "to_string" isn't a member of "std"? - Stack Overflow

面倒なので他の方法を探してみる

stringstreamを使う

stringstreamを使った方法が見つかった

参考 - [O] C++ で int 型の値を string 型にするときは stringstream - 文字列⇔数値 - code snippets - C++11で数字→文字列はstd::to_string()、文字列→数字はstd::stoi()とかstd::stod()とか - minus9d's diary

やってみる

超適当なコード

$ cat stringToint.cpp 
#include <iostream>
#include <string>

#include <sstream>

int main(void) {
    int   a = 3;
    float b = 3.14;
    double c = 2.99792458;
    std::string d= "this is test";

    std::string str;
    std::stringstream ss;    

    ss << a;
    str = ss.str();
    std::cout << str << std::endl;

    ss.str("");
    ss << b;
    str = ss.str();
    std::cout << str << std::endl;

    ss.str("");
    ss << c;
    str = ss.str();
    std::cout << str << std::endl;

    ss.str("");
    ss << d;
    str = ss.str();
    std::cout << str << std::endl;
}

実行結果

$ g++ stringToint.cpp
$ ./a.out 
3
3.14
2.99792
this is test

vector

vectorのソート

参考: - http://blog.sarabande.jp/post/62062199465 - http://kaworu.jpn.org/cpp/std::sort

https://github.com/ludwig125/work/blob/master/src/cpp/test/sort_vector.cpp

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
                                                                               
    vector<int> v;
    v.push_back ( 3 );
    v.push_back ( 4 );
    v.push_back ( 1 );
    v.push_back ( 2 );

    std::sort(v.begin(), v.end(), std::greater<int>() );
    for(vector<int>::iterator it = v.begin(); it != v.end(); it++){
        cout << *it << endl;
    }

    return 0;
}

実行結果

[~/git/work/src/cpp/test ] $ ./a.out 
4
3
2
1
[~/git/work/src/cpp/test ] $

文字列を受け取って1文字ずつvectorに格納

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string str;
    getline(cin,str);

    string tmp;
    // 'abcde'のような文字列を一文字ずつvectorに格納
    vector<string> v;
    for(int i = 0; i < (int)str.size(); i++){
        char ch = str[i];
        cout << ch << " " << endl;

//        v.push_back(string(ch));
        // 直接charをvectorに格納しようとしたらコンパイルエラーになったのでstringの変数に入れた
        tmp = ch;
        v.push_back(tmp);
    }

    cout << "output vector" << endl;
    for(vector<string>::iterator it = v.begin(); it != v.end(); it++){
        cout << *it << endl;
    }

    return 0;
}

c++でmsgpackを使ってみる

msgpackとは

MessagePack: It's like JSON. but fast and small.

MessagePackは、効率の良いバイナリ形式のオブジェクト・シリアライズ フォーマットです。
JSONの置き換えとして使うことができ、様々なプログラミング言語をまたいでデータを交換することが可能です。
しかも、JSONよりも速くてコンパクトです。
例えば、小さな整数値はたった1バイト、短い文字列は文字列自体の長さ+1バイトでシリアライズできます。

らしい

仕事で使う機会があったのでc++でサンプルコードを実行してみる

msgpackのインストール

MessagePack: It's like JSON. but fast and small.C/C++ msgpack のページに従う

準備

` インストール条件

gcc >= 4.1.0
cmake >= 2.8.0

gccバージョン確認

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.3' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) 

cmake確認

なかったのでインストール

sudo apt-get install cmake 
$ cmake -version
cmake version 2.8.12.2

msgpackのインストール

msgpackのgitはここ

github.com

$ git clone https://github.com/msgpack/msgpack-c.git
$ cd msgpack-c
$ cmake .
$ make
$ sudo make install

msgpackのバージョンを確認

msgpackのサンプルコードを実行

msgpack-c/QUICKSTART-CPP.md at master · msgpack/msgpack-c · GitHub

First program(単なるデータのシリアライズとデシリアライズ

コード

以下を hello.cpp という名前で保存

#include <msgpack.hpp>
#include <vector>
#include <string>
#include <iostream>

int main(void) {
        // serializes this object.
        std::vector<std::string> vec;
        vec.push_back("Hello");
        vec.push_back("MessagePack");

        // serialize it into simple buffer.
        msgpack::sbuffer sbuf;
        msgpack::pack(sbuf, vec);

        // deserialize it.
        msgpack::object_handle oh =
            msgpack::unpack(sbuf.data(), sbuf.size());

        // print the deserialized object.
        msgpack::object obj = oh.get();
        std::cout << obj << std::endl;  //=> ["Hello", "MessagePack"]

        // convert it into statically typed object.
        std::vector<std::string> rvec;
        obj.convert(rvec);
}

コンパイル

$ g++ -Ipath_to_msgpack/include hello.cpp -o hello

実行
 $ ./hello 
["Hello", "MessagePack"]

Streaming into an array or map(配列やマップのシリアライズとデシリアライズ

msgpack-c/QUICKSTART-CPP.md at master · msgpack/msgpack-c · GitHub

ここにはシリアライズはあるが、デシリアライズの処理がないので、自分で作ってみる

$ cat stream.cpp 
#include <msgpack.hpp>
#include <iostream>
#include <string>

#include <sstream>

int main(void) {
        // serializes multiple objects into one message containing an array using msgpack::packer.
        msgpack::sbuffer buffer;

        msgpack::packer<msgpack::sbuffer> pk(&buffer);
        pk.pack_array(4);
        pk.pack(std::string("Log message ... 1"));
        pk.pack(std::string("Log message ... 2"));
        pk.pack(std::string("Log message ... 3"));
        pk.pack(std::string("Log message ... 4"));

        // serializes multiple objects into one message containing a map using msgpack::packer.
        msgpack::sbuffer buffer2;

        msgpack::packer<msgpack::sbuffer> pk2(&buffer2);
        pk2.pack_map(2);
        pk2.pack(std::string("x"));
        pk2.pack(3);
        pk2.pack(std::string("y"));
        pk2.pack(3.4321);
//        pk2.pack(4);


        /*---- 以下自作部分 ----*/
        
        // unpack array
        msgpack::unpacked msg;
        msgpack::unpack(msg, buffer.data(), buffer.size());
        msgpack::object obj = msg.get();
        msgpack::object_array obj_array = obj.via.array;

        std::string str[4];
        for (int i = 0; i < 4; i++) {
             (obj_array.ptr[i]).convert(str[i]);
             std::cout << "str[" << i << "]: " << str[i] << std::endl;
        }

        // unpack map
        msgpack::unpacked msg2;
        msgpack::unpack(msg2, buffer2.data(), buffer2.size());
        msgpack::object obj2 = msg2.get();
        msgpack::object_map obj_map = obj2.via.map;

        for (int i = 0; i < 2; i++) {
            std::string map_key;
            //std::cout << "key: " << obj_map.ptr[i].key;
            (obj_map.ptr[i].key).convert(map_key);


            // 以下、mapのvalの方は、intの場合とfloatの場合があるので一旦floatにconvert
            // してからstringに変換した
            float f;
            // obj_map.ptr[i].valはmsgpackのobject
            // 一旦floatにconvertしてからそれを文字列に変換した
            (obj_map.ptr[i].val).convert(f);
            std::stringstream ss;
            ss << f;

            std::cout << "key: " << map_key << " val: " << ss.str() << std::endl;


            // これを実行するとintは2, floatは4と表示される
            //std::cout << "type: " << (obj_map.ptr[i].val).type << std::endl;

            // 以下のようなif文で判定できるが、このプログラムでは一括してfloatに変換している
            //if ( (obj_map.ptr[i].val).type == msgpack::type::POSITIVE_INTEGER ) {
            //}
        }
        

        //  $ g++ -Ipath_to_msgpack/include stream.cpp -o stream
}

実行結果

$ g++ -Ipath_to_msgpack/include stream.cpp -o stream
$ ./stream 
str[0]: Log message ... 1
str[1]: Log message ... 2
str[2]: Log message ... 3
str[3]: Log message ... 4
key: x val: 3
key: y val: 3.4321

上の修正は以下を参考にした

msgpackのオブジェクトとしてここを参考にした

    MSGPACK_OBJECT_NIL                  = 0x00,
    MSGPACK_OBJECT_BOOLEAN              = 0x01,
    MSGPACK_OBJECT_POSITIVE_INTEGER     = 0x02,
    MSGPACK_OBJECT_NEGATIVE_INTEGER     = 0x03,
    MSGPACK_OBJECT_FLOAT32              = 0x0a,
    MSGPACK_OBJECT_FLOAT64              = 0x04,
    MSGPACK_OBJECT_FLOAT                = 0x04,
#if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT)
    MSGPACK_OBJECT_DOUBLE               = MSGPACK_OBJECT_FLOAT, /* obsolete */
#endif /* MSGPACK_USE_LEGACY_NAME_AS_FLOAT */
    MSGPACK_OBJECT_STR                  = 0x05,
    MSGPACK_OBJECT_ARRAY                = 0x06,
    MSGPACK_OBJECT_MAP                  = 0x07,
    MSGPACK_OBJECT_BIN                  = 0x08,
    MSGPACK_OBJECT_EXT                  = 0x09              
  • 注意点として、msgpackのバージョンによるのか、関数名や引数が参照渡しかポインタ渡しかなどが違うみたい
  • 単純に例と同じようにやってもうまく行かなかった

User-defined classes(MSGPACK_DEFINE)

msgpack-c/QUICKSTART-CPP.md at master · msgpack/msgpack-c · GitHub

You can use serialize/deserializes user-defined classes using MSGPACK_DEFINE macro.

とある通り、ユーザ定義のクラスをMSGPACK_DEFINEというマクロを使ってシリアライズ/デシリアライズできる

$ cat msgpack_define.cpp
#include <msgpack.hpp>
#include <vector>
#include <string>

#include <iostream>

class myclass {
//private:
//    std::string m_str;
//    std::vector<int> m_vec;
public:
    std::string m_str;
    std::vector<int> m_vec;

    MSGPACK_DEFINE(m_str, m_vec);

    myclass() {
        m_str = "default";
        m_vec.push_back(1);
        m_vec.push_back(2);
        m_vec.push_back(3);
    }
};

int main(void) {
        std::vector<myclass> vec;
        // add some elements into vec...

        vec.push_back(myclass());


        // you can serialize myclass directly
        msgpack::sbuffer sbuf;
        msgpack::pack(sbuf, vec);

        msgpack::object_handle oh =
            msgpack::unpack(sbuf.data(), sbuf.size());

        msgpack::object obj = oh.get();

        // you can convert object to myclass directly
        std::vector<myclass> rvec;
        obj.convert(rvec);

        for(std::vector<myclass>::iterator it = rvec.begin(); it != rvec.end(); ++it) {
            std::cout << (*it).m_str << std::endl;

            for(std::vector<int>::iterator m_vec_it = (*it).m_vec.begin(); m_vec_it != (*it).m_vec.end(); ++m_vec_it) {
                std::cout << (*m_vec_it) << std::endl;
            }
        }
}

実行結果

$ g++ -Ipath_to_msgpack/include msgpack_define.cpp -o msgpack_define
$ ./msgpack_define 
default
1
2
3

MSGPACK_DEFINEの定義

v1_1_cpp_adaptor · msgpack/msgpack-c Wiki · GitHub

ソースコードは以下 - https://github.com/msgpack/msgpack-c/blob/55b51c506fee9ce496e9b98aca33cadade681479/include/msgpack/adaptor/define_decl.hpp#L28-L42

#define MSGPACK_DEFINE_ARRAY(...) \
    template <typename Packer> \
    void msgpack_pack(Packer& pk) const \
    { \
        msgpack::type::make_define_array(__VA_ARGS__).msgpack_pack(pk); \
    } \
    void msgpack_unpack(msgpack::object const& o) \
    { \
        msgpack::type::make_define_array(__VA_ARGS__).msgpack_unpack(o); \
    }\
    template <typename MSGPACK_OBJECT> \
    void msgpack_object(MSGPACK_OBJECT* o, msgpack::zone& z) const \
    { \
        msgpack::type::make_define_array(__VA_ARGS__).msgpack_object(o, z); \
    }

同じソースコードに下に以下も定義されているので、MSGPACK_DEFINE がMSGPACK_DEFINE_ARRAYに置き換わる
#define MSGPACK_DEFINE MSGPACK_DEFINE_ARRAY

c++11コンパイル

c++11でコンパイル

$ cat c11_sample.cpp 
#include <iostream>

int main(){
    std::cout << "test" << std::endl;
}

g++で今まで通りコンパイル

$ g++ c11_sample.cpp 
$ ./a.out 
test

c++11でコンパイル

$ g++ -std=c++11 c11_sample.cpp 
$ ./a.out 
test

参考:C++11,C++14(C++xx)などの機能を有効にする

pythonトラブル対応

エラー

AttributeError

AttributeError: module 'json' has no attribute 'dump'

誤ったインタプリタです: そのようなファイルやディレクトリはありません

pip3を使おうとしたらこんなエラーが

[~/git/work/src/python/memo/pandas] $ pip3 install pandas_ply
/home/ludwig125/.pyenv/pyenv.d/exec/pip-rehash/pip: /home/ludwig125/.pyenv/versions/3.5.4/bin/pip3: /home/前のユーザ名/.pyenv/versions/3.5.4/bin/python3.5: 誤ったインタプリタです: そのようなファイルやディレクトリはありません

Ubuntu14.0.4でホスト名とユーザ名を変更する - ludwig125のブログ

これでユーザ名を変えてしまったせいみたい

解決できないのでpyenvごと削除して作り直す

[~] $ pyenv versions
* system (set by /home/ludwig125/.python-version)
  3.5.4
[~] $ pyenv uninstall 3.5.4
pyenv: remove /home/ludwig125/.pyenv/versions/3.5.4? yes
[~] $ 


[~] $  pyenv install 3.5.4
Downloading Python-3.5.4.tar.xz...
-> https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz
Installing Python-3.5.4...
Installed Python-3.5.4 to /home/ludwig125/.pyenv/versions/3.5.4

$ pyenv local 3.5.4で移動

これでpip3を使えるようにになった

[~] $ 
[~] $ pip3 --version
pip 9.0.1 from /home/ludwig125/.pyenv/versions/3.5.4/lib/python3.5/site-packages (python 3.5)
[~] $ 

Ubuntu14.0.4でGoogleChromeが起動しない

問題

VMUbuntuChromeをアップデートしたあとで、 Google Chromeのアイコンを押しても起動しなくなってしまった

コマンドでgoogle-chromeをしたら以下のエラーが

$ google-chrome
[11644:11689:1103/213846.450610:FATAL:nss_util.cc(632)] NSS_VersionCheck("3.26") failed. NSS >= 3.26 is required. Please upgrade to the latest NSS, and if you still get this error, contact your distribution maintainer.
中止 (コアダンプ)

解決方法

askubuntu.com

これらを参考にパッケージをアップグレード

sudo apt-get upgrade
sudo apt-get update

これで開けるようになった

[shingo-virtual-machine ~/git/work] $ google-chrome
ATTENTION: default value of option force_s3tc_enable overridden by environment.
ATTENTION: default value of option force_s3tc_enable overridden by environment.
[18255:18293:1103/215655.013441:ERROR:simple_version_upgrade.cc(164)] File structure does not match the disk cache backend.
[18255:18293:1103/215655.014000:ERROR:simple_backend_impl.cc(659)] Simple Cache Backend: wrong file structure on disk: /home/shingo/.cache/google-chrome/Default/Cache
[18255:18255:1103/215655.772279:ERROR:account_tracker.cc(328)] OnGetTokenFailure: Invalid credentials.
[18255:18255:1103/215655.779440:ERROR:account_tracker.cc(328)] OnGetTokenFailure: Invalid credentials.

Ubuntuのパスワードを求められたので入力したら解決

apt-get参考

qiita.com

VMのUbuntuにWebサーバを立てる

概要

  • 色々苦戦したのでメモ
  • やったこと

  • 1.VM上のubuntupythonで簡易的にweb serverを立ち上げる ポート8181

  • 2.VM内のブラウザで localhost:8181 → 当然見られる
  • 3.ホストPCのWindowsのブラウザで、VMのプライベートIPアドレスを指定して192.168.19.137:8181を見る → これも見られる
  • 4.しかしiphoneからはアクセスできない
  • 5.ルータのネットワーク外からポート8181のアクセスが来た時に、VMのWebサーバに行くようにポートフォワード(ポート開放)する
  • 6.ルータがポートフォワードできるIPアドレスの範囲がVMIPアドレスと違うー
  • 7.なので、VMIPアドレスをポートフォワードできるものに変更
  • 8.ファイアウォールの設定も変更しよう ⇒ ufwで8181のポートをallowに
  • 9.まだつながらない → 面倒だから 自宅ルータから直接VMをブリッジで接続しよう
  • 10.出来た iphoneやノートPCのブラウザからUbuntu内のファイルの情報が見られた
  • 11.グローバルIPアドレスを設定して見られないか確認

ここまで2,3日

手順

1.VM上のubuntupythonで簡易的にweb serverを立ち上げる ポート8181

python3の入っている環境で以下の通り、Webサーバ立ち上げる

ポートは8181

$ python3 -m http.server --cgi 8181
Serving HTTP on 0.0.0.0 port 8181 (http://0.0.0.0:8181/) ...

2.VM内のブラウザで localhost:8181 → 当然見られる

f:id:ludwig125:20171028223645p:plain

3.ホストPCのWindowsのブラウザで、VMのプライベートIPアドレスを指定して192.168.19.137:8181を見る → これも見られる

Windows側のブラウザでは上のlocalhostの部分をVMIPアドレスにしたものを表示 - VMIPアドレスはifconfigで取得した(IPV4のもの)

4.しかしiphoneからはアクセスできない

みられない

会社PCからも見られない

pingもtracerouteもVMのアドレスに対してやってもtimeoutする(ホストPCであるWindowsIPアドレスに対しては問題なし)

$ ping 192.168.19.137
PING 192.168.19.137 (192.168.19.137): 56 data bytes
ping: sendto: Host is down
ping: sendto: Host is down
Request timeout for icmp_seq 0
ping: sendto: Host is down
Request timeout for icmp_seq 1
ping: sendto: Host is down
Request timeout for icmp_seq 2

$ traceroute -v 192.168.19.137
traceroute to 192.168.19.137 (192.168.19.137), 64 hops max, 52 byte packets
 1  * *^C

なぜつながらないのか?

あとからもう少しわかることだったが、ここで繋がらなかった理由はこの時点のVMのネットワークの設定がNATになっていて、ホストPC側の設定が正しくなかったからかもしれない

5.ルータのネットワーク外からポート8181のアクセスが来た時に、VMのWebサーバに行くようにポートフォワード(ポート開放)する

家のルータはソフトバンク光の 光BBユニット(EWMTA2.3)だった

そうしたら以下のように言われた

f:id:ludwig125:20171028224431p:plain

転送先IPアドレスに誤りがあります。IPアドレスの範囲内で半角数字で入力してください

6.ルータがポートフォワードできるIPアドレスの範囲がVMIPアドレスと違うー

以下によると、転送先のIPアドレスの範囲が限られているらしく、VMのアドレスと異なるので転送できない・・・

f:id:ludwig125:20171103222942p:plain

7.なので、VMIPアドレスをポートフォワードできるものに変更

以下の方法でVMIPアドレスを、ポート開放できる範囲に変える

実践初級ITブログ VMwareのUbuntuを固定IPにする

もともとのUbuntu
IPアドレス 192.168.19.137
ブロードキャストアドレス 192.168.19.255
サブネットマスク 255.255.255.0
デフォルトルート 192.168.19.2
第一DNS 192.168.19.2


VMのUbuntuのIPを以下に変更
192.168.3.11

変える前

f:id:ludwig125:20190306044115p:plain

変えた後

f:id:ludwig125:20190306044131p:plain

f:id:ludwig125:20171103224244p:plain

ifconfigなどでIPアドレスが変わっていることを確認 再度ポート転送を設定すると成功

f:id:ludwig125:20171103224629p:plain f:id:ludwig125:20171028225748p:plain

8.ファイアウォールの設定も変更しよう ⇒ ufwで8181のポートをallowに

utfいじる

  • ここでallowにしたポート以外はファイアウォールが有効なので、ホストOSからは接続できない

第76回 Ubuntuのソフトウェアファイアウォール:UFWの利用(1):Ubuntu Weekly Recipe|gihyo.jp … 技術評論社

 $ sudo ufw status    
状態: 非アクティブ
 $ sudo ufw enable
ファイアウォールはアクティブかつシステムの起動時に有効化されます。
 $ sudo ufw status    
状態: アクティブ
 $
$ sudo ufw default DENY
デフォルトの incoming ポリシーは 'deny' に変更しました
(適用したい内容に基づいて必ずルールを更新してください)

$ sudo ufw allow 8181
ルールを追加しました
ルールを追加しました (v6)


$ sudo ufw status    
状態: アクティブ

To                         Action      From
--                         ------      ----
8181                       ALLOW       Anywhere
8181 (v6)                  ALLOW       Anywhere (v6)

 $  sudo ufw allow http
ルールを追加しました
ルールを追加しました (v6)

9.まだつながらない → 面倒だから 自宅ルータから直接VMをブリッジで接続しよう

以下の方法でブリッジに変更した - VMware上のLinux Webサーバに外部からアクセスできるようにする | あみだがみねのもろもろ備忘録

最初はNATに設定されていた f:id:ludwig125:20171028231018p:plain

ブリッジに変更 f:id:ludwig125:20171028230950p:plain

よくよく考えると、VMのネットワークをブリッジ接続にしたので、ゲートウェイDNS サーバーのIPアドレスはルータのIPアドレスが正しいはず なのに、それらがVMIPアドレスを変える前のままになっている 以下は間違い

f:id:ludwig125:20171103224244p:plain

以下が正しい

f:id:ludwig125:20171103224539p:plain

10.出来た ! iphoneやノートPCのブラウザからUbuntu内のファイルの情報が見られた

f:id:ludwig125:20171103224731p:plain

11.グローバルIPアドレスを設定して見られないか確認

上の接続はあくまで同一ネットワーク内からVMのプライベートIPアドレス 192.168.3.11 に繋いだだけだった

自分のグローバルIPアドレスが何か? - ルータの設定が書かれた以下のWAN側IPアドレスでも見られる - http://172.16.255.254/settei.html

LAN外のネットワークからルータのグローバルIPアドレスを設定して、 グローバルIP:8181で外からも接続できるか確認

  • Ubuntu側でWebサーバを立てる
$python3 -m http.server --cgi 8181 (git)-[master]
Serving HTTP on 0.0.0.0 port 8181 (http://0.0.0.0:8181/) ...
  • ネットワーク外からアクセスする

    • 注意点として、スマホからアクセスする場合はWifiを外して無線LAN外からアクセスする必要がある
    • ネットワーク内からグローバルIPに対してアクセスすることはできない
  • Wifiを切ったスマホから「グローバルIP:8181」でアクセスして、同様の画面が見られることが確認できた

図で書くとこんな感じだと思う(間違っていないはず・・)

f:id:ludwig125:20171103232200p:plain