0

is there any way to send commands from server to client with c++? i use send() function, but i have to convert numbers(like 0x100) to strings then convert strings to numbers in the client side... Basically the function that send directly numbers to client...

server:

ZeroMemory(msg,sizeof(msg));
wsprintf( msg , "142");
send(connect_sock ,(char*) msg , sizeof(msg) , 0);

client:

ZeroMemory(msg,sizeof(msg));    
recv (connect_sock, msg, sizeof(msg)-1, 0);
int i = atoi(msg);
if ( i == 142 )
...
Charles
  • 50,943
  • 13
  • 104
  • 142
Ped.NJ
  • 5
  • 4
  • It all depends on the application protocol you're using. – Barmar Nov 12 '13 at 10:02
  • You're receiving a string, because you send a string - you could send any block of data (e.g. a message struct) or encode a more verbose message as xml and send that as a string – benjymous Nov 12 '13 at 10:06
  • well, so how change message struct to send integer instead? – Ped.NJ Nov 12 '13 at 10:16
  • instead of `char msg[MESSAGE_LENGTH];` create your own struct, and use `MyStruct msg;` Note that this will cause you endian / size issues if your client and server are on different platforms – benjymous Nov 12 '13 at 10:36
  • A better example is given here: http://stackoverflow.com/questions/1577161/passing-a-structure-through-sockets-in-c – benjymous Nov 12 '13 at 10:36

2 Answers2

1

Simple - you can't, but you can write your own, or you can find libraries where everything is done for you.

1) boost::asio (c++) has serialization example

2) libcli (c) for telnet protocol (https://github.com/dparrish/libcli)

3) libtpl (c) serialization (http://troydhanson.github.io/tpl/)

many more...

Example for raw descriptors (packing argv using libtpl):

connecting and creating serverFd on your own

tpl_node *tn;
char* bufferArgv;

/** @todo pass array or string? */
tn = tpl_map("iA(s)", &argc, &bufferArgv);
tpl_pack(tn, 0); 

char** _argv;
for (_argv = argv; *_argv != 0; _argv++)
{
    bufferArgv = *_argv;
    tpl_pack(tn, 1);
}

tpl_dump(tn, TPL_FD, serverFd);
tpl_free(tn);

Unpack

tpl_node *tn;
char* bufferArgv;
int argc;

tn = tpl_map("iA(s)", &argc, &bufferArgv);
tpl_load(tn, TPL_FD, serverFd);
tpl_unpack(tn, 0);

if you brave enough replace tpl_dump and tpl_load and send raw tn.data

Maquefel
  • 480
  • 4
  • 16
0

you can try like this , using the system function Server :

wsprintf( msg , "142"); send(connect_sock ,(char*) msg , sizeof(msg) , 0);

Client :

recv (connect_sock, msg, sizeof(msg)-1, 0); string result = system( "./ %s", msg) ;

i am not sure abt the functionality . please check this and let me know .

  • it doesn't works... really there is not source code for remote desktop app , network games, and others. to let me know about architecture of net apps? – Ped.NJ Nov 12 '13 at 11:09