Question about NULL Character & fgets()


 
Thread Tools Search this Thread
Top Forums Programming Question about NULL Character & fgets()
# 1  
Old 11-03-2008
Lightbulb Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as

Sent mesg: hello
Bytes Sent to Client: 6


Code:
bytes_received = recv(clientSockD, data, MAX_DATA, 0);
if(bytes_received)
{
                        send(clientSockD, data, bytes_received, 0);
                        data[bytes_received] = '\0';
                        printf("Sent mesg: %s", data);
                        cout<<"Bytes Sent to Client: "<<bytes_received<<endl;
}

clients send that data as follows:

Code:
cout<<"you: ";
fgets(clientInput, MAX_DATA, stdin); 
send(sockfd, clientInput, strlen(clientInput), 0);

Why?

bytes_received should be 5, not 6 ..... Any idea?

**************************************************************

Lets say we have:

Code:
cout<<"Enter a port: ";
cin>>portNum;

fgets(clientInput, MAX_DATA, stdin);

Does fgets() takes a "\0" from the previous input, as you know cin leaves \0 there? getline() does...

Last edited by f.ben.isaac; 11-03-2008 at 09:02 AM..
# 2  
Old 11-03-2008
Hi,

send() and recv() won't distinguish with null character and space. It just send the number of bytes that is defined in the argument. Just do strlen() on sent and received bytes and you will get the answer.

Venky
# 3  
Old 11-04-2008
Thanks!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Housekeeping null 2>&1 in /dev

Hello, Does anyone know how to housekeeping the null 2>&1 file in /dev? its fill up my system, please help. Thanks :b: (3 Replies)
Discussion started by: only
3 Replies

2. Shell Programming and Scripting

Remove first NULL Character in Flat File

We have a flat file with below data : ^@^@^@^@00000305^@^@^@^@^@^@430^@430^@^@^@^@^@^@^@^@^@09079989530As we can see ^@ is Null character in this file I want to remove only the first few null characters before string 00000305 How can we do that, any idea. I want a new file without first few... (5 Replies)
Discussion started by: simpltyansh
5 Replies

3. UNIX for Advanced & Expert Users

Insertion of Null Character while writing into file

I have a huge file with record length around 5000 characters. There is an ETL tool datastage which is writing this data to the file(AIX server). At position 4095 i have seen NULL Character(^@). when i am using this command "head -1 file_nm | sed 's/\000//g'" --- the output is displaying... (3 Replies)
Discussion started by: issaq84mohd
3 Replies

4. Shell Programming and Scripting

NULL Search String & File Name

Hi All, I am writting a Sell Script, that takes Search String & File Name from the terminal and check for Null Status. If either is NULL then pgm should quit. I wrote the following: bash-3.2$ cat null_status_of_parameters.sh #!/bin/sh #WASS that takes Search String & File Name from... (2 Replies)
Discussion started by: manishdivs
2 Replies

5. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

6. Shell Programming and Scripting

Null Character Handling

Hi All, I have a problem with Null values while reading line by line from a text file. I wrote a shell script to read set of file names from a text file line by line, and zipping the each individual file and copying those zip files into some separate directory, and removing the original file... (3 Replies)
Discussion started by: npk2210
3 Replies

7. UNIX for Dummies Questions & Answers

Find files which contain a null character

Hi, I would like to use grep to find files which contain NULL characters. I'm not sure how to represent the null character in the grep statement. Could anyone help please? Thanks! Helen :confused: (5 Replies)
Discussion started by: Bab00shka
5 Replies

8. UNIX for Advanced & Expert Users

sending a null character to a terminal

I'm testing out some ESMTP AUTH stuff, and it requires that the username and password be on the same line separated by a null character. Does anyone know how to echo the ASCII null character? Thanks, Alex (3 Replies)
Discussion started by: vertigo23
3 Replies

9. UNIX for Dummies Questions & Answers

/dev/null 2>&1 question

Hi, suppose you have the following line at your crontab : 5 * * * * /usr/mine/script > /dev/null 2>&1 now i understood that the " > /dev/null 2>&1 outputs both Standard outpout and Standard Error messages to the /dev/null device or file... the first part , " > /dev/null " transfers... (1 Reply)
Discussion started by: BAM
1 Replies

10. UNIX for Dummies Questions & Answers

GREP a string with NULL Character

Does anyone know how to use grep/egrep to find a string that contains a null character? i.e.: the string looks like this: null0001nullN well I want to be able to : grep '0001N' is there a wildcard character or something that I can put in the grep to include the nulls? (3 Replies)
Discussion started by: weerich
3 Replies
Login or Register to Ask a Question