vcodeword
vcodeword

Reputation: 21

Send To Function Error :

string s = "100:T1:R1:170.168.0.9:55555:11111";
stringstream ss;
ss<<s;
string Temp=    ss.str();
ss.str(string());
char *buff= const_cast<char *>(Temp.c_str());
I have string in this buff.
int len;
len=sendto(sd, buff,sizeof(buff), 0, (struct sockaddr*)&addr, sizeof(addr));
cout<<"\n"<<len<<endl;

When I am writing this code,I am getting length of 8,that is 8 character of the string that I want to send,but I want to send the whole string . So,Please Help me out.

Upvotes: 0

Views: 44

Answers (1)

paulsm4
paulsm4

Reputation: 121649

buff is declared as char*, and sizeof(char*) is the size of the pointer itself, not the size of the data being pointed at. On your system, a pointer is 8 bytes (i.e. your app is compiled for a 64-bit OS).

Don't use sizeof(buff), use Temp.length() or strlen(buff) instead (add +1 if you need to send the null terminator as well).

Also, your stringstream is useless in your example.

string s = "100:T1:R1:170.168.0.9:55555:11111";
int len = sendto(sd, s.c_str(), s.length(), 0, (struct sockaddr*)&addr, sizeof(addr));
cout << "\n" << len << endl;

Upvotes: 5

Related Questions