473,326 Members | 2,134 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,326 software developers and data experts.

EFAULT error in accept socket call

Hi,

I was writing a simple program using sockets. The program is supposed to be
a TCP server that receives a string sent by the client. For this purpose I
defined a char array called readString which was initially defined as
readString[6]. The program worked fine. I then increased the array size to
readString[100] and the accept socket call started giving an error
(errno=14;EFAULT). This gave me the impression that the clientAddr structure
was causing this problem, so I declared it as a global variable(see
below).Now the code works fine. Can someone tell me why I am getting this
error? Your help would be greatly appreciated. Thanks. The following is a
brief outline of the code:

struct sockaddr_in clientAddr; //Declaring this here works when I increase
readString from readString[6] to readString[100]
int main()
{
char readString[100],*tempPtr;
struct sockaddr_in serverAddr;//,clientAddr; /*Declaring clientAddr here
does not work when I increase readString from readString[6] to
readString[100]. It gives an EFAULT error for accept. This works fine for
readString[6]*/
..
..
..
if((serverSockFd=socket(AF_INET,SOCK_STREAM,0))<0)
..
..
..
if(bind(serverSockFd,(struct sockaddr*)&serverAddr, sizeof(serverAddr))<0)
..
..
..

if(listen(serverSockFd,5)<0)
..
..
..

if((clientSockFd=accept(serverSockFd,(struct
sockaddr*)&clientAddr,&clientAddrSize))<0)
..
..
..
}

Here is the full text of the code, in case the above does not suffice:

//struct sockaddr_in clientAddr;
int main()
{
int serverSockFd,clientSockFd,noOfBytesReadTotal,noOfB ytesReadOneRead;
char readString[100],*tempPtr;
socklen_t clientAddrSize;
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;

strcpy(readString,"Hello World!");
printf("Before receiving from client readString = %s\n",readString);

if((serverSockFd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("Socket failed\n");
exit(1);
}
else
printf("Socket succeeded.\n");

serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(5002);
serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);
if(memset(serverAddr.sin_zero,'\0',8)<0)
{
printf("Error in memset\n");
close(serverSockFd);
exit(1);
}

if(bind(serverSockFd,(struct sockaddr*)&serverAddr, sizeof(serverAddr))<0)
{
printf("Bind failed\n");
close(serverSockFd);
exit(1);
}
else
{
printf("Bind succeeded.\n");
}

if(listen(serverSockFd,5)<0)
{
printf("Listen failed\n");
close(serverSockFd);
exit(1);
}
else
printf("Listen succeeded.\n");

if((clientSockFd=accept(serverSockFd,(struct
sockaddr*)&clientAddr,&clientAddrSize))<0)
{
printf("Accept failed\n");
close(serverSockFd);
extern int errno;
printf("errno=%d\n",errno);
exit(1);
}
else
printf("Accept succeeded.\n");

noOfBytesReadTotal=noOfBytesReadOneRead=0;
tempPtr=readString;
while(noOfBytesReadTotal<47)
{
noOfBytesReadOneRead=read(clientSockFd,tempPtr,47-noOfBytesReadTotal);
if(noOfBytesReadOneRead<0)
{
printf("Error occurred in Read after reading %d
bytes\n",noOfBytesReadTotal);
close(serverSockFd);
close(clientSockFd);
extern int errno;
printf("Error no=%d\n",errno);
exit(1);
}
noOfBytesReadTotal++;
tempPtr++;
}
printf("After receiving from client string is %s\n",readString);

close(serverSockFd);
close(clientSockFd);
}
Nov 14 '05 #1
2 4978

In article <cj**********@gist.usc.edu>, "Rookie" <do***********@rediffmail.com> writes:

I was writing a simple program using sockets. The program is supposed to be
a TCP server that receives a string sent by the client. For this purpose I
defined a char array called readString which was initially defined as
readString[6]. The program worked fine. I then increased the array size to
readString[100] and the accept socket call started giving an error
(errno=14;EFAULT).


This is because your program calls the accept function incorrectly,
and the code within the accept function is then invoking undefined
behavior. After that anything can happen. In this case, "anything"
happens to depend on how you have arranged things in memory, but you
are merely changing the symptoms. The underlying problem remains.

Everything else is off-topic for comp.lang.c. accept is not a C
library function. It is a POSIX / SUS function, so this would be
on-topic in comp.unix.programmer, for example. It would also be
on-topic in comp.protocols.tcp-ip, where the sockets API is
frequently discussed.

OT: Your code does not initialize the third parameter to accept
before making the call. Per the definition of accept (see SUSv3 or
an equivalent source), the "addrlen" parameter (for which you are
passing the address of the clientAddrSize variable) must be set to
the size of the area pointed to by the "addr" parameter. In the
case of your program, that would be sizeof clientAddr.

There may be other errors in your code; I didn't bother checking
further.

--
Michael Wojcik mi************@microfocus.com

The surface of the word "profession" is hard and rough, the inside mixed with
poison. It's this that prevents me crossing over. And what is there on the
other side? Only what people longingly refer to as "the other side".
-- Tawada Yoko (trans. Margaret Mitsutani)
Nov 14 '05 #2
On Sun, 26 Sep 2004 19:49:27 -0700, in comp.lang.c , "Rookie"
<do***********@rediffmail.com> wrote:
Hi,

I was writing a simple program using sockets.


Sockets is offtopic in CLC, You need to ask again in comp.unix.programmer
where sockets are topical.

(and where you are more likely to get the right answer)

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rune | last post by:
Hi, I've written a very simple 'kill-server' to help me shut down processes through Telnet or HTTP. The kill-server is a function and is launched as a thread. I use the module socket.py on Python...
2
by: Christopher J. Bottaro | last post by:
Hello, I'm trying to write a fairly simple network program. The main thread spawns a thread which creates a listener socket and then calls socket.accept on it. socket.accept blocks indefinantly....
5
by: Blatwurst | last post by:
I'm trying to implement a simple server in C#. I want to do the classic thing of spinning off a thread that just blocks in a Socket.Accept() call until a request comes in. At that point, the...
1
by: Rookie | last post by:
Hi, I was writing a simple program using sockets. The program is supposed to be a TCP server that receives a string sent by the client. For this purpose I defined a char array called readString...
1
by: Joe | last post by:
Hi, I browsed the news and a few seem to have this problem too, but no solution ! I have a client server application where in case the connection gets bad (crashes or whatever) client or...
9
by: Henrik | last post by:
I'm developing an application that uses two threads to communicate with two different devices on my network. One of the devices is reached through an API from the supplier of that device. The API...
2
by: ppuniversal | last post by:
hello, my program snippet: /*****************************/ /******runThread Function*****/ /*to accept other client connections*/ /*****************************/ DWORD WINAPI...
3
by: ollii | last post by:
im creating a iterrative server and i get this error error: syntax error before numeric constant and this errors occurs near this line: int listen(sock,5); need help
13
by: 7stud | last post by:
I have the following two identical clients #test1.py:----------- import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = 'localhost' port = 5052 #server port
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.