Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 18:20

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C++] Receive String from Client To Server

Discussion on [C++] Receive String from Client To Server within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2014
Posts: 15
Received Thanks: 0
[C++] Receive String from Client To Server

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
-Unknow is offline  
Old 09/02/2014, 00:08   #2
 
Logtetsch's Avatar
 
elite*gold: 192
Join Date: May 2009
Posts: 2,227
Received Thanks: 3,262
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);
Logtetsch is offline  
Old 09/03/2014, 12:15   #3
 
elite*gold: 0
Join Date: May 2014
Posts: 15
Received Thanks: 0
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
-Unknow is offline  
Old 09/03/2014, 14:30   #4
 
​Tension's Avatar
 
elite*gold: 110
Join Date: Jun 2013
Posts: 599
Received Thanks: 510
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();
​Tension is offline  
Old 09/08/2014, 14:03   #5
 
elite*gold: 198
Join Date: Mar 2011
Posts: 835
Received Thanks: 263
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
ƬheGame is offline  
Old 09/10/2014, 07:46   #6



 
+Yazzn's Avatar
 
elite*gold: 420
Join Date: Jan 2012
Posts: 1,082
Received Thanks: 1,000
That doesn't make any sense.
+Yazzn is offline  
Old 09/10/2014, 07:58   #7

 
elite*gold: 0
Join Date: Feb 2008
Posts: 2,754
Received Thanks: 1,748
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.
Computerfreek is offline  
Old 09/10/2014, 08:47   #8
 
elite*gold: 198
Join Date: Mar 2011
Posts: 835
Received Thanks: 263
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).
ƬheGame is offline  
Reply


Similar Threads Similar Threads
Working with sockets - Server & client [Send - Receive Packets]
11/12/2013 - CO2 Private Server - 9 Replies
Working with sockets - Server & client Hello Epvps Members .. as title says .. i am working on client and server deployment on C# i used Conquer server socket " V 5518+ " and i created a client that connect to it . i can send and receive bytes and convert it to message like here :
[Visual Basic] [Problem] String auslesen/String zufällig wählen
05/06/2012 - General Coding - 4 Replies
Code: #include <GUIConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Name Generator", 236, 299, 815, 246) $Input1 = GUICtrlCreateInput("Username", 24, 72, 185, 21) $Input2 = GUICtrlCreateInput("Username", 24, 104, 185, 21) $Input3 = GUICtrlCreateInput("Username", 24, 136, 185, 21) $Input4 = GUICtrlCreateInput("Username", 24, 168, 185, 21) $Input5 = GUICtrlCreateInput("Username", 24, 200, 185, 21)
WPE Doesn't receive any packets (reason: server or wud? @.@)
09/09/2010 - Ragnarok Online - 1 Replies
Ok. I am a newbie and am new to WPE. I searched, I read, I followed; still, i don't see any packets on my WPE. I already targeted my ro program, and started logging -> Packets : 0 >.<"" (i moved, dropped, picked things, ran around ig tho). The server i tried on was DreamerRo Nightmare Low rate (CS-Arena.com - professionelles Game-, Rootserver- & Housingbusiness). Sooo yea... I really appreciate every piece of advice from everyone. *-* Thankss:confused:
[HELP] client loding stop(string= Skillname/skilldesc)
10/28/2009 - Dekaron Private Server - 5 Replies
May i ask some questions. I have a little problem. These two files, skillname.csv and skill desc.csv, in the string folder, makes the client stop and 69%, when the index number is over 255. Do i have to fix the dekaron.exe file in the bin folder? or is there another way to figure this out? plz help. I really need some exact answers.



All times are GMT +1. The time now is 18:21.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.