Socket bad Address error resolution

Source: Internet
Author: User
Tags define function fread
Article reproduced from: http://www.isongzi.com/2009/03/26/socket-bad-address/

I don't understand why I have bad address errors when I read the socket buffer. The send-side write return value is normal, and the receiving end read returns 0 and prints a bad address error message.

Receive-side code: RECEIVE.C

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include < arpa/inet.h>
#include <stdio.h>
#include <unistd.h>

/** initialized to get sock_fd**/

int Receive_init (int *sock_fd,int port);

int main ()
{
int server_fd;
int client_fd;
int port=8000;
Char *buff= "";
Client_fd=receive_init (&server_fd,port);
if (client_fd<0) {
printf ("Receive init failed!\n");
return-1;
}

while (1)
{   read (client_fd,buff,1);
printf ("Receive:%s\n", buff);

Sleep (2);
}

return 0;

}

Send-side code: SEND.C

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include < arpa/inet.h>
#include <stdio.h>
#include <unistd.h>

/** initialize send, return client_fd**/
int Send_init (int *sock_fd,char *host_addr,int port);

int main ()
{
int err;
int port=8000;
Char *buff= "ABCD";
Char *host_addr= "127.0.0.1″;
int sock_fd;
Err=send_init (&sock_fd,host_addr,port);

if (err<0) {
printf ("Send init failed!\n");
return-1;
}
while (1)
{
write (sock_fd,buff,2);
printf ("Send:%s\n", buff);
Sleep (2);
}
return 0;

}

When debugging, note that when the receive.c in the buff to char, that is, each write, read is 1 characters, read normal. Why, then? The reason for the memory address error. Because there is no space allocated.

                                                                                                                                                                                                        

There are explanations on the Internet as follows:

In Linux can be used Strerror (errno) to get the error code description, when it comes to memory operations, inadvertently strerror (errno) to return "bad address" error, Of course, more often than not, killed and segmentation fault. How to solve it. Most of the cases must be the memory address has been wrong, I have encountered in the work of the following two kinds of "badaddress":

First, file reading and writing:

FILE *FP = NULL;

size_t file_length = 0;

.../open file and find file length

size_t bytes_read = 0;

unsigned char *ptemp = NULL;

.../forget to assign value to ptemp or not allocate memory to it.

while (0 = feof (FP))

{

Byres_read = Fread (ptemp, 1, file_length, FP);

if (0!= ferror (FP))

{

printf ("Fread error[%s]\n", Strerror (errno));

......

}

Ptemp + = Byres_read;

}

The error returned at this time is: fread Error[bad address]

Reason: Ptemp is null (NULL)

Problem:

1. If I am in ptemp + = Byres_read, then add a file_length-=byres_read; The program will fall into a dead loop and ask your brother to look up the reason for the Fread document with Microsoft MSDN.


Second, Recv,send,sendto,recvfrom

Sometimes call the above four functions to send and receive data is also prone to "bad address" error, that 99% may be your function has a pointer to inaccessible memory space. Be careful, brother. We have already met in the actual work.

Recvfrom Possible error code:

1. EBADF parameter S non-legal socket processing code
2. The Efault parameter has a pointer to an inaccessible memory space.
3. Enotsock parameter S is a file descriptor, not a socket.
4. The eintr was interrupted by the signal.
5. Eagain This action will block the process, but the socket of the parameter S is not blocked.
6. Insufficient buffer memory for ENOBUFS system
7. Enomem Core memory is low
8. The parameters passed to the system call by EINVAL are incorrect.

Note:
Send (data transmitted via socket)
Related functions: Sendto,sendmsg,recv,recvfrom,socket
#include <sys/types.h>
#include <sys/socket.h>
define function int send (int s,const void * msg,int len,unsigned int falgs);
Parameter flags are generally set to 0, and other values are defined as follows:
The data transmitted by Msg_oob is sent out Out-of-band.
Msg_dontroute Cancel Routing Table query
Msg_dontwait is set to not block operation
Msg_nosignal This action does not want to be interrupted by a sigpipe signal.

Note: Sure enough, there is no reason to allocate space for pointers. Change Char *buff to a char array, char buff[max_size], or simply memset (buff,0,max_size). The reason for bad address is because the pointer points to inaccessible space.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.