473,574 Members | 3,130 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 5004

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
5476
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...
2
2377
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...
5
10917
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...
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...
1
6121
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...
9
6802
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...
2
2249
by: ppuniversal | last post by:
hello, my program snippet: /*****************************/ /******runThread Function*****/ /*to accept other client connections*/ /*****************************/ DWORD WINAPI runThread(LPVOID param)
3
17085
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
3915
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
7842
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...
0
7764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8110
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. ...
0
8138
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...
1
5658
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3775
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...
0
3794
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.