• Advertisement
Sign in to follow this  

Simple C++ TCP Client

This topic is 3079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
All I need to send is notifications and strings..

So basically just strings.

Also, thanks for your input Antheus.

Share this post


Link to post
Share on other sites
I see, I wasn't sure.

Winsock has just been frusteratingly complex to me, and I wanted a wrapper for it.

Thanks for your help.

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
All of your help was awesome thanks!

I decided to go with Winsock and I'm using this little tutorial from MSDN:
http://msdn.microsoft.com/en-us/library/ms738545(VS.85).aspx

-Michael

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement