send array of string over socket programming

Nov 24, 2008 at 6:51am
I am doing a network relay program using socket programming to connect all the nodes.
Basically, the server will connect to one client and will pass an information to it.
The information that i want to send is a .txt format (text file).As far as I know, it is very difficult to send text file over socket.

So, I think it will be better for me to send the data as a string.

This is my partial code to send the array of string.
1
2
3
4
5
6
7
8
9
10

//send data to client
string list[];
for (int n=b-1;n>=0; n--)
{
 list[n]=path2[n];
 sckt::byte data[256];
 strcpy(data,list[n].c_str() );
 sock.Send(data,sizeof(data));
}


in sckt.h, byte define as follows
1
2
typedef unsigned char byte;
M_SCKT_STATIC_ASSERT(sizeof(byte)==1);


and it gives this error:
error:invalid conversion form 'sckt::byte*' to 'char*'
error:initializing argument 1 of 'char* strcpy(char* , const char*)'

can anyone help me please..
and I hope that my question is clear =)

Thanks in advance


Last edited on Nov 24, 2008 at 7:56am
Nov 24, 2008 at 5:32pm
char* (what strcpy expects) is not the same as unsigned char* (the type of sckt::byte).
You have to cast.

strcpy( static_cast<char*>( data ), list[n].c_str() );

Or just make data a char array to begin with.

Some other things to note:
1) Using a variable named list can be dangerous in light of the STL list container.
2) Line 8 is a buffer overflow waiting to happen.

Nov 25, 2008 at 5:39am
Thanks for the suggestion!

But when I run and compile the code. This error occur,

error: invalid static_cast from type ‘sckt::byte [256]’ to type ‘char*’


Nov 25, 2008 at 1:58pm
Sorry, try

 
strcpy( static_cast<char*>( &data[0] ), list[n].c_str() );


Nov 26, 2008 at 4:17am
thanks for the idea..my code run as im using this
strcpy( reinterpret_cast <char*> (data), arraylist2[g].c_str());

=))
Topic archived. No new replies allowed.