Sign in to follow this  
Enlighten

Simple C++ TCP Client

Recommended Posts

I need to setup some form of communication between a C# application that will be the server and a C++ app acting as the client. For the moment I have the C# app using System.Net and System.Net.Sockets, and it seems to be working. I'm having a hard time finding a solution for my C++ app that is simple, strictly a client, and compatible with the C# app. If anyone could point me in the right direction, I would greatly appreciate it. Also if it's important, this is for Vista. Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by Enlighten
I'm having a hard time finding a solution for my C++ app that is simple, strictly a client, and compatible with the C# app.


Sockets are platform agnostic, so the compatibility is not a problem.

Conceptually, boost::asio is similar to .Net networking. It may be too verbose for someone not versed in C++.
Winsock obviously works as well.


However, if you rely on anything more, such as .Net serialization, then managed C++ would probably work, and just use the .Net classes.

Share this post


Link to post
Share on other sites
Have you looked at Windows Sockets? While it may seem slightly difficult to grasp at first it is actually rather straight forward once you get the hang of it. There are countless tutorials about making a simple TCP client in Winsock -- Google it!

There isn't really such a thing as a "C# compatible" C++ client. .NET allows you to access the socket classes, which provide you with the tools to send information over the network. The .NET sockets layer doesn't implement any custom protocol on top of TCP/IP -- that is for you to do. As such, there's no "trick" to getting your C++ application talk to the C# application; all you have to do is make up the rules for how they shall communicate.

EDIT: Antheus mentioned Boost.Asio, which is excellent when you need scalability and portability. However, it kind of overkill if all you're looking for is a simple client app. It won't be worth the trouble.

Share this post


Link to post
Share on other sites
Quote:
Original post by Enlighten
All I need to send is notifications and strings..

So basically just strings.

Also, thanks for your input Antheus.


Darn, looks like I just missed you. If you send strings you must take into consideration the fact that C# works with Unicode whereas C++ works with single-byte characters. If you need your protocol to be able to send anything besides ASCII/ANSI characters, you should implement an encoding such as UTF-8 in your C++ application to allow for this. (Note that C++ does have support for multi-byte characters, but the type used for it, wchar_t, is not precisely defined in the standard and should not be relied upon in my opinion).

Share this post


Link to post
Share on other sites
This is not C++, but there is a simple send/recv ascii string via TCP socket C source floating about online.... um, let me dig it up: http://flinflon.brandonu.ca/Dueck/1999/62306/sockets/Default.htm

Its portable, not that you say you needed that. At least it will show you how its handled. Not the best documented though, if you want to play with the innards.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this