Ronin
Ronin

Reputation: 2067

How to send data and get data from tcp/ip port in C,C++ both on Windows and Linux

I am to write a cross platform program for windows and linux :: A Java program will send data to a tcp/ip port and there will be a C/C++ program which will continuously listen from that tcp/ip port when it gets some data it will do some works and send the result data to another tcp/ip port from which now the Java program will read the data and do some actions. I am more worried about the C/C++ portion. Can you please help with any detail or useful link or sample code ?

Upvotes: 3

Views: 3709

Answers (2)

user406009
user406009

Reputation:

My suggestion: boost::asio for C++ cross platform networking and google protocol buffers for specifying the cross platform/cross language protocol.

Use boost::asio for sending/receiving bytes of data and google protocol buffers to turn those bytes into something useful(ints, strings, etc).

Upvotes: 2

user405725
user405725

Reputation:

There are tons of networking APIs/libraries and event notifications mechanisms. Since I assume you are new to C++ and don't really want to deal with platform-specific functionality in order to squeeze microseconds from your implementation and/or write different code for different platforms, I think your best bet is to use Boost ASIO - a cross-platform C++ library for network and low-level I/O programming. It has a very good documentation, tons of examples, and is pretty easy to use in general. It will work on many platforms w/o a need to change a single line of code in your application. It has its pitfalls, but they are not very significant unless you are doing something extremely complicated that has a lot of strict requirements for performance, memory usage, latency, throughput or all of the above.

Just in case I am not right in my assumptions, there are a lot of alternatives. Some of them are:

  • libevent - a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached
  • epoll - a scalable I/O event notification mechanism for Linux
  • kqueue - a scalable event notification interface introduced in FreeBSD 4.11, also supported in NetBSD, OpenBSD, DragonflyBSD, and Mac OS X.
  • POSIX
  • Windows API (those guys always re-invent the wheel).

Good luck!

Upvotes: 0

Related Questions