Skip to main content
gamedev.net
🔒 Locked

recv() question

Started by DMINATOR Nov 21, 2004 at 8:12 AM 1 replies 1.8k views
Original Post
DMINATOR
DMINATOR
Hi again, well i am just playing around with recv() winsock command , and found something strange . As i understand recv() receaves a certain number of bytes and puts them to the buffer. Soething like that

#define BUFSIZE 512

//i create buffer
char *buffer = (char*)malloc(sizeof(char) * BUFSIZE);
memset(buffer,0,BUFSIZE);

//now i read the bytes to the buffer
recv(socket,buffer,BUFSIZE, 0);

Well it works fine if overall number of bytes is less than BUFSIZE, but when the buffer is full i get some strange results like (an example from irc server response) :
Quote:
:wineasy2.se.quakenet.org 001 DMINASTAH :Welcome to the Internet Relay Network, DMINASTAH :wineasy2.se.quakenet.org 002 DMINASTAH :Your host is wineasy2.se.quakenet.org, running version u2.10.11.04+asuka(1.0.6a) :wineasy2.se.quakenet.org 003 DMINASTAH :This server was created Thu Apr 29 2004 at 19:13:41 CEST :wineasy2.se.quakenet.org 004 DMINASTAH wineasy2.se.quakenet.org u2.10.11.04+asuka(1.0.6a) dioswkgxXInR biklmnopstvrDcCNu bklov :wineasy2.se.quakenet.org 005 DMINASTAH WHOX WALLCHOPS WALLVOICES USERýýýýÝÝÝÝÝÝÝÝÝÝÝÝ'IP CPRIVMSG CNOTICE SILENCE=15 MODES=6 MAXCHANNELS=20 MAXBANS=45 NICKLEN=15 TOPICLEN=250 AWAYLEN=160 KICKLEN=250 :are supported by this server :wineasy2.se.quakenet.org 005 DMINASTAH CHANTYPES=#& PREFIX=(ov)@+ CHANMODES=b,k,l,imnpstrDcCNu CASEMAPPING=rfc1459 NETWORK=QuakeNet :are supported by this server :wineasy2.se.quakenet.org 251 DMINASTAH :There are 63061 users and 141157 invisible on 41 servers :wineasy2.se.quakenet.org 252 DMINASTAH 76 :operator(s) online :wineasy2.se.quakenet.org 253 DMINASTAH 6ýýýýÝÝÝÝÝÝÝÝÝÝÝÝ'5 :unknown connection(s) :wineasy2.se.quakenet.org 254 DMINASTAH 190954 :channels formed :wineasy2.se.quakenet.org 255 DMINASTAH :I have 8809 clients and 1 servers :wineasy2.se.quakenet.org NOTICE DMINASTAH :Highest connection count: 9005 (9004 clients) :wineasy2.se.quakenet.org 375 DMINASTAH :- wineasy2.se.quakenet.org Message of the Day - :wineasy2.se.quakenet.org 372 DMINASTAH :- 2004-10-16 16:45 :wineasy2.se.quakenet.org 372 DMINASTAH :- ** [ wineasy.se.quakenet.org ] **************************************** :wineasy2.seýýýýÝÝÝÝÝÝÝÝÝÝÝÝ'.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- Welcome to the QuakeNet IRC Network! :wineasy2.se.quakenet.org 372 DMINASTAH :- _ :wineasy2.se.quakenet.org 372 DMINASTAH :- _ __(_)___ ___ ____ ________ __ :wineasy2.se.quakenet.org 372 DMINASTAH :- | | /| / / / __ \/ _ \/ __ `/ ___/ / / / :wineasy2.se.quakenet.org 372 DMINASTAH :- | |/ |/ / / / / / __/ /_/ (__ ) /_/ / :wineasy2.se.quakenet.org 372 DMINASTAH :- |__/|_ýýýýÝÝÝÝÝÝÝÝÝÝÝÝ'_/_/_/ /_/\___/\__,_/____/\__, / :wineasy2.se.quakenet.org 372 DMINASTAH :- s e . q u a k e n e t . o r g /____/ :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- You are connected to wineasy.se.quakenet.org. :wineasy2.se.quakenet.org 372 DMINASTAH :- Available ports are 6667, 6668, 6669, 6670 and 7000. :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- This server is located at WinEasy AB, Stockholm. :wineasy2.se.quakýýýýÝÝÝÝÝÝÝÝÝÝÝÝ'enet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- ** [Local IRC operators ] ********************************************* :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- froo froo@quakenet.org (Swedish & English) :wineasy2.se.quakenet.org 372 DMINASTAH :- stevoo stevoo@quakenet.org (Swedish & English) :wineasy2.se.quakenet.org 372 DMINASTAH :- softis softis@quakenet.org (Swedish & English) :wineasy2.se.quakenet.oýýýýÝÝÝÝÝÝÝÝÝÝÝÝ'rg 372 DMINASTAH :- vendis vendis@quakenet.org (Swedish & English) :wineasy2.se.quakenet.org 372 DMINASTAH :- quas quas@quakenet.org (Swedish & English) :wineasy2.se.quakenet.org 372 DMINASTAH :- j-dog j-dog@quakenet.org (Swedish, English & French) :wineasy2.se.quakenet.org 372 DMINASTAH :- ammis ammis@quakenet.org (Swedish & English) :wineasy2.se.quakenet.org 372 DMINASTAH :- Deckard deckard@quakenet.org (English) :wineasy2.se.quakenet.org 372 DMINASTýýýýÝÝÝÝÝÝÝÝÝÝÝÝ'AH :- Paul paul@quakenet.org (English) :wineasy2.se.quakenet.org 372 DMINASTAH :- meeb meeb@quakenet.org (English) :wineasy2.se.quakenet.org 372 DMINASTAH :- magpie magpie@quakenet.org (English) :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- ** [ Rules ] **********************************************************
Note this ýýýýÝÝÝÝÝÝÝÝÝÝÝÝ' line ? Well i already found a solution, but it's not really efficient i think:

#define BUFSIZE 512

//number actually read
int bread;

//used to output messages receaved from server
buffer = (char*)malloc(sizeof(char) * BUFSIZE);
memset(buffer,0,BUFSIZE);

//just used for temp values
temp = (char*)malloc(sizeof(char) * 255);
memset(temp,0,255);

//we remember howmuch bytes we read
bread = recv(wParam,temp,255, 0);

//now we insert bread number of bytes to the buffer
sprintf(buffer,"%.*s",bread,temp);


Here is correct output from irc server :
Quote:
:wineasy2.se.quakenet.org 001 DMINASTAH :Welcome to the Internet Relay Network, DMINASTAH :wineasy2.se.quakenet.org 002 DMINASTAH :Your host is wineasy2.se.quakenet.org, running version u2.10.11.04+asuka(1.0.6a) :wineasy2.se.quakenet.org 003 DMINASTAH :This server was created Thu Apr 29 2004 at 19:13:41 CEST :wineasy2.se.quakenet.org 004 DMINASTAH wineasy2.se.quakenet.org u2.10.11.04+asuka(1.0.6a) dioswkgxXInR biklmnopstvrDcCNu bklov :wineasy2.se.quakenet.org 005 DMINASTAH WHOX WALLCHOPS WALLVOICES USERIP CPRIVMSG CNOTICE SILENCE=15 MODES=6 MAXCHANNELS=20 MAXBANS=45 NICKLEN=15 TOPICLEN=250 AWAYLEN=160 KICKLEN=250 :are supported by this server :wineasy2.se.quakenet.org 005 DMINASTAH CHANTYPES=#& PREFIX=(ov)@+ CHANMODES=b,k,l,imnpstrDcCNu CASEMAPPING=rfc1459 NETWORK=QuakeNet :are supported by this server :wineasy2.se.quakenet.org 251 DMINASTAH :There are 62837 users and 140995 invisible on 41 servers :wineasy2.se.quakenet.org 252 DMINASTAH 77 :operator(s) online :wineasy2.se.quakenet.org 253 DMINASTAH 71 :unknown connection(s) :wineasy2.se.quakenet.org 254 DMINASTAH 190970 :channels formed :wineasy2.se.quakenet.org 255 DMINASTAH :I have 8736 clients and 1 servers :wineasy2.se.quakenet.org NOTICE DMINASTAH :Highest connection count: 9005 (9004 clients) :wineasy2.se.quakenet.org 375 DMINASTAH :- wineasy2.se.quakenet.org Message of the Day - :wineasy2.se.quakenet.org 372 DMINASTAH :- 2004-10-16 16:45 :wineasy2.se.quakenet.org 372 DMINASTAH :- ** [ wineasy.se.quakenet.org ] **************************************** :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- Welcome to the QuakeNet IRC Network! :wineasy2.se.quakenet.org 372 DMINASTAH :- _ :wineasy2.se.quakenet.org 372 DMINASTAH :- _ __(_)___ ___ ____ ________ __ :wineasy2.se.quakenet.org 372 DMINASTAH :- | | /| / / / __ \/ _ \/ __ `/ ___/ / / / :wineasy2.se.quakenet.org 372 DMINASTAH :- | |/ |/ / / / / / __/ /_/ (__ ) /_/ / :wineasy2.se.quakenet.org 372 DMINASTAH :- |__/|__/_/_/ /_/\___/\__,_/____/\__, / :wineasy2.se.quakenet.org 372 DMINASTAH :- s e . q u a k e n e t . o r g /____/ :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- You are connected to wineasy.se.quakenet.org. :wineasy2.se.quakenet.org 372 DMINASTAH :- Available ports are 6667, 6668, 6669, 6670 and 7000. :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- This server is located at WinEasy AB, Stockholm. :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- ** [Local IRC operators ] *********************************************
So anyone know what is wrong , and how to do it correctly ?
SiCrane
SiCrane
Off the top of my head it looks like you aren't reserving room for a null terminator at the end of your buffer in the first version.

Try doing a recv(socket,buffer,BUFSIZE - 1, 0); instead and see if that helps.
DMINATOR
DMINATOR
Quote:
Original post by SiCrane
Off the top of my head it looks like you aren't reserving room for a null terminator at the end of your buffer in the first version.

Try doing a recv(socket,buffer,BUFSIZE - 1, 0); instead and see if that helps.


Yep you were right, Silly me , tnx again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.