Giter Site home page Giter Site logo

sazameki / cpp_tcp-communication-sample Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 74 KB

C++によるTCP通信の実装サンプル(Xcode / Visual Studio対応)

C++ 93.70% C 6.30%
cpp bsd-sockets winsock tcp client server xcode visual-studio tcp-server tcp-client

cpp_tcp-communication-sample's Introduction

cpp_tcp-communication-sample

C++でTCP/IPの通信を行うサーバ実装とクライアント実装のサンプルプログラムです。macOSのXcode上で使用可能なBSDソケットベースで作成し、Visual Studio上でもコンパイルできるようにWinSockへの対応を加えています。

実装について

マルチスレッド環境で、適切な排他処理が行われた上でキューなどを利用してメッセージの処理を行うことを想定して、同期的な送受信を行うメソッドのみをサポートしています。

開発環境

以下の環境で動作を確認しています。

  • macOS 10.12上のXcode 8
  • Windows 7 (SP1)上のVisual Studio 2017

クライアントについて

以下の2つのファイルを追加すれば使用できます。

  • TCPClient.hpp
  • TCPClient.cpp
TCPClient *client = new TCPClient("127.0.0.1", 12345);

// データの送信
client->Send("Hello!!\r\n");

// データの受信
std::string reply = client->ReceiveString();
...        
delete client;

※ Windows環境では、アプリケーションの起動時と終了時に、WinSockの初期化とクリーンアップのための関数呼び出しが必要になることに注意してください。また、[プロジェクト]-[<プロジェクト名>のプロパティ]から、[構成プロパティ]-[C/C++]-[詳細設定]の「必ず使用されるインクルード ファイル」に「stdafx.h」を指定する必要があります。

サーバについて

以下の4つのファイルを追加すれば使用できます。

  • TCPClient.hpp
  • TCPClient.cpp
  • TCPServer.hpp
  • TCPServer.cpp
TCPServer *server = new TCPServer(12345);
        
bool isRunning = true;
while (isRunning) {
     TCPClient *client = server->Accept();
     /* clientで表されるクライアント用のスレッドをここで作成する。通信方法は上のクライアントと同様。 */
}

delete server;

※ Windows環境では、アプリケーションの起動時と終了時に、WinSockの初期化とクリーンアップのための関数呼び出しが必要になることに注意してください。また、[プロジェクト]-[<プロジェクト名>のプロパティ]から、[構成プロパティ]-[C/C++]-[詳細設定]の「必ず使用されるインクルード ファイル」に「stdafx.h」を指定する必要があります。

cpp_tcp-communication-sample's People

Contributors

sazameki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cpp_tcp-communication-sample's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.