473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

warning: the `gets' function is dangerous and should not be used.

5
Hello everybody

im just creating an client that is going to communicated with Iterative server.
and i get this error:
warning: the `gets' function is dangerous and should not be used.

and i think this is related to this code:

printf("Enter message to be sent:\n");
gets(send_buf);


and this is my code for the client could anyone could check it to see if am on the right track:
Expand|Select|Wrap|Line Numbers
  1. /**************************************************************************/
  2. /* Generic client example is used with connection-oriented server designs */
  3. /**************************************************************************/
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <string.h>
  9.  
  10. #define EOB "$$"
  11. #define  KB_size 1024     
  12. int ClientNo = 0;
  13.  
  14. main (int argc, char *argv[])
  15. {
  16.    int    len, rc;
  17.    int    sockfd;
  18.    char   send_buf[80];
  19.    char   recv_buf[80];
  20.    char   KB_buff[KB_size];
  21.    int      intPort;
  22.    struct sockaddr_in   addr;
  23.  
  24.    /*************************************************/
  25.    /* Create an AF_INET stream socket               */
  26.    /*************************************************/
  27.    sockfd = socket(AF_INET, SOCK_STREAM, 0);
  28.    if (sockfd < 0)
  29.    {
  30.       perror("socket");
  31.       exit(-1);
  32.    }
  33.  
  34.    /*************************************************/
  35.    /* Initialize the socket address structure       */
  36.    /*************************************************/
  37.    memset(&addr, 0, sizeof(addr));
  38.    addr.sin_family      = AF_INET;
  39.    addr.sin_addr.s_addr = htonl(INADDR_ANY);
  40.    addr.sin_port        = htons(intPort);
  41.  
  42.    /*************************************************/
  43.    /* Connect to the server                         */
  44.    /*************************************************/
  45.    rc = connect(sockfd,(struct sockaddr *)&addr, sizeof(struct sockaddr_in));
  46.    if (rc < 0)
  47.    {
  48.       perror("connect");
  49.       close(sockfd);
  50.       exit(-1);
  51.    }
  52.    printf("Connect completed.\n");
  53.  
  54.    /*************************************************/
  55.    /* Enter data buffer that is to be sent          */
  56.    /*************************************************/
  57.    printf("Enter message to be sent:\n");
  58.    gets(send_buf);
  59.  
  60.    /*************************************************/
  61.    /* Send data buffer to the worker job            */
  62.    /*************************************************/
  63.    len = send(sockfd, send_buf, strlen(send_buf) + 1, 0);
  64.    if (len != strlen(send_buf) + 1)
  65.    {
  66.       perror("send");
  67.       close(sockfd);
  68.       exit(-1);
  69.    }
  70.    printf("%d bytes sent\n", len);
  71.  
  72.    /*************************************************/
  73.    /* Receive data buffer from the worker job       */
  74.    /*************************************************/
  75.    len = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
  76.    if (len != strlen(send_buf) + 1)
  77.    {
  78.       perror("recv");
  79.       close(sockfd);
  80.       exit(-1);
  81.    }
  82.    printf("%d bytes received\n", len);
  83.  
  84.    /*************************************************/
  85.    /* Close down the socket                         */
  86.    /*************************************************/
  87.    close(sockfd);
  88. }
Sep 19 '07 #1
2 29988
gets() is deprecated and unsafe.

Use fgets() instead.

Look at man :

This is a _dangerous_ function, as it has no way of checking the
amount of space available in BUF. One of the attacks used by the Inter-
net Worm of 1988 used this to overrun a buffer allocated on the stack
of the finger daemon and overwrite the return address, causing the dae-
mon to execute code downloaded into it over the connection.
Sep 22 '07 #2
Warning: The gets function is very dangerous because it provides no protection against overflowing the string s. The GNU library includes it for compatibility only. You should always use fgets or getline instead. To remind you of this, the linker (if using GNU ld) will issue a warning whenever you use gets.
Jan 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Stuart Anderson | last post by:
I am looking to use Ken Stephenson's CirclePack program (http://www.math.utk.edu/~kens/) with some tiling programs written by Cannon, Floyd, Parry. The programs I want to use are subdivide.c...
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
6
by: Brendan | last post by:
Hi, I'm trying to mimic the IPC/messaging system of an specific OS in a portable way by using GCC's library. The IPC system uses buffered asynchronous messages, where any thread can send a...
10
by: paytam | last post by:
hi all can you tell me what's the wrong with this code? I use gcc compiler,but when I wanted to use gets() function in my code but it takes a dangerous warning(the gets function is dangerous...
4
by: metalinc | last post by:
hi...im new to C programming...need help...tried to run this code but got this error fork.c: In function ‘parse’: fork.c:44: warning: comparison between pointer and integer fork.c:51: warning:...
28
by: Dave Stafford | last post by:
I have a macro that I use across the board for freeing ram. I'd like to clean up my code so I don't get these warnings. #define sfree(x) _internal_sfree((void **)&x) #define _internal_sfree(x)...
1
by: Rajesh V | last post by:
When I use gets() function, the gcc compiler gives the warning - gets function is dangerous and should not be used. What is the problem in using it and how to avoid it ?
16
by: pereges | last post by:
I'm getting a warning on my linux gcc compiler: /tmp/ccXgHa9s.o(.text+0x48): In function `main': : warning: the `gets' function is dangerous and should not be used. And here's where I used...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.