473,909 Members | 4,189 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;EFAUL T). 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((serverSockF d=socket(AF_INE T,SOCK_STREAM,0 ))<0)
..
..
..
if(bind(serverS ockFd,(struct sockaddr*)&serv erAddr, sizeof(serverAd dr))<0)
..
..
..

if(listen(serve rSockFd,5)<0)
..
..
..

if((clientSockF d=accept(server SockFd,(struct
sockaddr*)&clie ntAddr,&clientA ddrSize))<0)
..
..
..
}

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

//struct sockaddr_in clientAddr;
int main()
{
int serverSockFd,cl ientSockFd,noOf BytesReadTotal, noOfBytesReadOn eRead;
char readString[100],*tempPtr;
socklen_t clientAddrSize;
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;

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

if((serverSockF d=socket(AF_INE T,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=hto nl(INADDR_ANY);
if(memset(serve rAddr.sin_zero, '\0',8)<0)
{
printf("Error in memset\n");
close(serverSoc kFd);
exit(1);
}

if(bind(serverS ockFd,(struct sockaddr*)&serv erAddr, sizeof(serverAd dr))<0)
{
printf("Bind failed\n");
close(serverSoc kFd);
exit(1);
}
else
{
printf("Bind succeeded.\n");
}

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

if((clientSockF d=accept(server SockFd,(struct
sockaddr*)&clie ntAddr,&clientA ddrSize))<0)
{
printf("Accept failed\n");
close(serverSoc kFd);
extern int errno;
printf("errno=% d\n",errno);
exit(1);
}
else
printf("Accept succeeded.\n");

noOfBytesReadTo tal=noOfBytesRe adOneRead=0;
tempPtr=readStr ing;
while(noOfBytes ReadTotal<47)
{
noOfBytesReadOn eRead=read(clie ntSockFd,tempPt r,47-noOfBytesReadTo tal);
if(noOfBytesRea dOneRead<0)
{
printf("Error occurred in Read after reading %d
bytes\n",noOfBy tesReadTotal);
close(serverSoc kFd);
close(clientSoc kFd);
extern int errno;
printf("Error no=%d\n",errno) ;
exit(1);
}
noOfBytesReadTo tal++;
tempPtr++;
}
printf("After receiving from client string is %s\n",readStrin g);

close(serverSoc kFd);
close(clientSoc kFd);
}
Nov 14 '05 #1
2 5032

In article <cj**********@g ist.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;EFAUL T).


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.progr ammer, 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.progr ammer
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
5493
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 v2.3 (Windows) I can use telnet host:port and enter the secret killword or use a broser with http://host:port/secret_killword The 'kill-server' validates the secret_killword and writes a file
2
2386
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. My problem is that when the main thread determines that it is time to quit, how do I get the spawned thread to exit? Preferably I'd like the spawned thread to return from socket.accept when the main thread tells it to and then check a shared...
5
10953
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 Accept() returns, the thread spins off another thread to handle the request, and then calls Accept() again. This all works fine except that I can find no way to kill the thread that is blocked in the Accept() call when I want to shut down the server. ...
1
387
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 which was initially defined as readString. The program worked fine. I then increased the array size to readString and the accept socket call started giving an error (errno=14;EFAULT). This gave me the impression that the clientAddr structure was...
1
6148
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 server (who knows it first) closes the socket and than wants to establish the SAME connection again ( due to firewall reasons it has to be the SAME IP AND PORT).
9
6823
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 has support for detecting when the connection to the device is lost and signal back to my application. When I use this whitout starting the other thread everything works fine. The problem occurs when I start the other thread where the following...
2
2264
by: ppuniversal | last post by:
hello, my program snippet: /*****************************/ /******runThread Function*****/ /*to accept other client connections*/ /*****************************/ DWORD WINAPI runThread(LPVOID param)
3
17103
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
3939
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
10035
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10919
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10538
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7248
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5938
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.