[C++] Receive String from Client To Server

09/01/2014 21:47 -Unknow#1
Hello Guys :), I've a problem to receive string from client to server :
In the client i use :
Code:
send(socket,string.c_str(),string.size(),0)
to send string to server;
In the server i use :
Code:
recv(socket,char,sizeof(char),0)
, to receive string, but when i go to write the string that i've receive from the client with :
Code:
std::cout<<char
, the output are a bad characters.
Can you Help me?
Thanks and sorry for my bad english :)
09/02/2014 00:08 Logtetsch#2
I'm really not familiar with the recv or send function in C++ but a view at the msdn description of these functions shows that you are parsing invalid arguments.

Code:
recv(socket,char,sizeof(char),0)
maybe? :
Code:
const unsigned int  BUF_SIZE = 1024;
char BUF[BUF_SIZE];
recv(socket, BUF, BUF_SIZE, 0);
and instead of:
Code:
send(socket,string.c_str(),string.size(),0);
Code:
/*const*/ std::string BUF = "Data\0";
send(socket, BUF.c_str(), BUF.length(), 0);
09/03/2014 12:15 -Unknow#3
Thanks for reply, i Solved with :
Code:
char message[4096];
int byte = recv(socket,message,4096,0);

std::string mess;

for(int i = 0;i<byte;i++) mess+=messagge[i];

std::cout<<mess;
I try to receive string with telnet client and it work, but if i send string by my client it isn't work :

Code:
while(1){

socket = connect(socket_listen,(sockaddr*)&sock,sizeof(sock));
std::string hello = "hello";

send(socket,hello.c_str(),hello.size(),0);

break;

}
Can you help me? << Thanks
09/03/2014 14:30 ​Tension#4
Quote:
Originally Posted by -Unknow View Post
Thanks for reply, i Solved with :
Code:
char message[4096];
int byte = recv(socket,message,4096,0);

std::string mess;

for(int i = 0;i<byte;i++) mess+=messagge[i];

std::cout<<mess;
Byte for byte?
Code:
std::array<char,4096> msg;
std::string mess;
int recv_len;

recv_len = recv(socket, msg.data(), msg.size(), 0);
if(recv_len) {
mess = std::string(msg.begin(), msg.begin()+recv_len);
}

Quote:
Originally Posted by -Unknow View Post
I try to receive string with telnet client and it work, but if i send string by my client it isn't work :

Code:
while(1){

socket = connect(socket_listen,(sockaddr*)&sock,sizeof(sock));
std::string hello = "hello";

send(socket,hello.c_str(),hello.size(),0);

break;

}
Can you help me? << Thanks
Can you post the full code of your connection process?
Have you tested if you are connected to the server?
Maybe you receive an error, you can see the error code with
WSAGetLastError();
09/08/2014 14:03 ƬheGame#5
plese no hardcoded arrays :S

find out how long your string is and save it in -> int length = yoursize;

ptr = calloc(length+1, sizeof(char));

if transmission is finished -> free(ptr);

dont forget to write a '/0' at the end of the char array ;)
09/10/2014 07:46 +Yazzn#6
That doesn't make any sense.
09/10/2014 07:58 Computerfreek#7
Quote:
Originally Posted by ƬheGame View Post
plese no hardcoded arrays :S

find out how long your string is and save it in -> int length = yoursize;

ptr = calloc(length+1, sizeof(char));

if transmission is finished -> free(ptr);

dont forget to write a '/0' at the end of the char array ;)
Sorry but a dynamic allocation makes no sense there.
The array is only a buffer which is fixed size. Your receive the content chunked because you never know how big the full content is. That's how network connections work.
09/10/2014 08:47 ƬheGame#8
Quote:
Originally Posted by Computerfreek View Post
Sorry but a dynamic allocation makes no sense there.
The array is only a buffer which is fixed size. Your receive the content chunked because you never know how big the full content is. That's how network connections work.
Depends on how many clients (threads) you have. If you make a new thread for every client you could just flood the ram by sending 1 char with a lot of clients. Best way would be to send the length of the string first, or make a thread pool so you can only have a certain amount of threads at the time(e.g 10 or 1 if its only for him).