Connecting Tech Pros Worldwide Forums | Help | Site Map

Sockets, C++, Sending...

Newbie
 
Join Date: Jun 2007
Posts: 2
#1: Jun 21 '07
Hello,
I'm new here :) So please be gentle with me :)

My question: I want to write a Server and a Client with Sockets. It's not the problem but I would want to send not "text-based". And I should be able to send commands like this:

CommandName "Para1" "Para2"...

But of course there exist Commands withouth Parameter, too.

How should I do that in C++? Yes, of course C++ doesnt support that. And I'm under windows. So I use winsock. But I would be very happy to do it on the most portable way ;)

So how would be the best way? I'm frightend because of to small buffers and so on. Especially because of recv()... And later I want to send files, too.

How should I design it at all? I'm thinking how to solve this problems for long time. But.... :(

I hope my problem is understandable.

Thanks :)

Dummie

Familiar Sight
 
Join Date: Apr 2007
Posts: 191
#2: Jun 22 '07

re: Sockets, C++, Sending...


You can send and receive any data you want. You just need to set up a system such that the sender and receiver know what is coming through the socket.

So if you send an int the receiver needs to know that the thing he just received is an int and not 4 chars (for example)

Does this help? I'm not sure how much help you need and I don't want to type for 20 minutes if you only need 3 minutes worth of help (laziness factor!)
Newbie
 
Join Date: Jun 2007
Posts: 2
#3: Jun 22 '07

re: Sockets, C++, Sending...


Quote:

Originally Posted by mac11

You can send and receive any data you want. You just need to set up a system such that the sender and receiver know what is coming through the socket.

So if you send an int the receiver needs to know that the thing he just received is an int and not 4 chars (for example)

Does this help? I'm not sure how much help you need and I don't want to type for 20 minutes if you only need 3 minutes worth of help (laziness factor!)

This answer help me a little bit. But I still have a (small?) problem. For example I tried this:

Client:

Expand|Select|Wrap|Line Numbers
  1. struct CommandData
  2. {
  3.     int Test1;
  4.     int Test2;
  5.     char *Para;
  6. };
  7.  
  8. CommandData Test;
  9. Test.Test1     =     10;
  10. Test.Test2     =     33924823;
  11. Test.Para      =     "Hello World!";
  12.  
  13. send( Socket, (char *) &Test, sizeof( CommandData ), 0 );
  14.  
Server:

Expand|Select|Wrap|Line Numbers
  1. struct CommandData
  2. {
  3.     int Test1;
  4.     int Test2;
  5.     char *Para;
  6. };
  7.  
  8. CommandData Test;
  9. recv( connectedSocket,(char * ) &Test, 256,0 ); // Whats about the Number 256? What I need to put there?
  10.  
  11. printf( "Received:%s-%i-%i\n", Test.Para, Test.Test1, Test.Test2 ); // Output "Received:-10-33924823"
  12.  
Firstly, is this the right way to do this? I'm very frigthend that I maybe receive not all data. And the struct can't be used.
Another problem is that I don't receive the "char *Para". How I send it on the right way? And how portable is this?

Example:

Server Windows - Client Linux System

What will happen? Does it still work?

Thanks for your help :)
Reply