Hi,
I'm facing another problem with my network program. I'm writing a basic server
that handles multiple clients. Whenever a client is connected, a new thread is
created to handle it.
The socket descriptor of the client and it's sockaddr_in structure are passed
as arguments to the function that handles the client. Now, the following code is
giving me an error. It's the function that handles the client:
--------------------------------------------------------------------------------
void *
handle_client(v oid *arg)
{
pthread_detach( pthread_self()) ;
int clientfd,temp;
char *mesg;
struct sockaddr_in client;
sock_arg *func_arg = (sock_arg *)arg;
mesg = (char *)malloc(100 * sizeof(mesg));
/*
sock_arg is my data type.
struct
{
int sockfd; //socket descriptor
struct sockaddr_in server; // info. of client connected
}
*/
clientfd = func_arg->sockfd;
client = func_arg->server;
/* printing welcome message to the client - works fine */
mesg = "welcome to network-sort server\n";
write(clientfd, mesg,strlen(mes g));
/* this block of code gives the error */
if( read(clientfd,m esg,1024) < 0 )
{
printf("read() error in %s: %s\n",__FUNCTIO N__,strerror(er rno));
exit(1);
}
temp = atoi(mesg);
if( temp == S_OK )
printf("server said: OK\n");
else if( temp == S_NOK )
printf("server said: NOT OK\n");
else
printf("unrecog nized code - call the sys admin\n");
return;
}
Here's the sample output for a session: ( the above code is in monitor.c )
SERVER:
[project@voors implementation]$ ./monitor
server started on 0.0.0.0 - listening on port 9999
connection from 172.16.14.52:20 67
creating a new thread to handle the client
read() error in handle_client: Bad address
CLIENT:
[project@voors project]$ telnet 172.16.14.52 9999
Trying 172.16.14.52...
Connected to 172.16.14.52.
Escape character is '^]'.
welcome to network-sort server
1 /* input */
Connection closed by foreign host.
--------------------------------------------------------------------------------
I've no idea what's going wrong. This is the first time I'm using threads. Any
help is appreciated.
regards,
GVK
--
Happy Hacking!!! 4 16632
GVK <va******@stude nts.iiit.net> wrote in
news:c2******** **@news.wplus.n et: SERVER: [project@voors implementation]$ ./monitor server started on 0.0.0.0 - listening on port 9999 connection from 172.16.14.52:20 67 creating a new thread to handle the client read() error in handle_client: Bad address
Since networking and read() are not part of C how can we help you? Did you
try comp.unix.progr ammer?
--
- Mark ->
--
GVK wrote: mesg = "welcome to network-sort server\n"; write(clientfd, mesg,strlen(mes g));
/* this block of code gives the error */ if( read(clientfd,m esg,1024) < 0 )
mesg points still to "welcome to network-sort server\n" which
is put by your compiler in READ-ONLY memory. Besides that, there
is not space for 1024 bytes there.
You need something like
mesg = (char *)malloc(1024);
before the read line
{ printf("read() error in %s: %s\n",__FUNCTIO N__,strerror(er rno)); exit(1); }
Regards, Joerg
GVK wrote:
[...] char *mesg;
[...] mesg = "welcome to network-sort server\n"; write(clientfd, mesg,strlen(mes g));
/* this block of code gives the error */ if( read(clientfd,m esg,1024) < 0 ) { printf("read() error in %s: %s\n",__FUNCTIO N__,strerror(er rno)); exit(1); }
[...] read() error in handle_client: Bad address
[...]
Two things:
The pointer mesg is pointing to a buffer of only 32 bytes,
containing "welcome to network-sort server\n\0".
This buffer may be in read-only memory.
Neither one of these allows you to store 1024 bytes into it.
--
+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
Joerg Schoen <Jo************ ************@t-online.de> spoke thus: mesg points still to "welcome to network-sort server\n" which is put by your compiler in READ-ONLY memory.
The compiler is certainly likely to store the string literal in
read-only memory, but is not required to. It doesn't change the fact
that attempting to modify a string literal results in undefined
behavior.
You need something like mesg = (char *)malloc(1024);
^^^^^^^^
Either you've been listening to Trollsdale or are sadly misinformed.
For numerous reasons (rehashed here regularly), casting malloc()'s
return value is a bad idea.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: matt breedlove |
last post by:
(x-posted to microsoft.public.dotnet.general and
microsoft.public.dotnet.languages.vb)
Hello all.
I am currently creating a Windows Service using VB.NET and would like to
return an error if something goes wrong during startup, IE: parsing a
configuration file.
The only way I know of is to throw an exception, and this will prevent the
|
by: Someonekicked |
last post by:
Is it possible to read a memory address with C++;
For example, If I run this code first:
*************
#include <iostream>
using namespace std;
void main()
{
int *zz = new int;
|
by: ambs.tech |
last post by:
Hi all,
Is there any way to read a email address from a text file and send
email to that address using C language. any hint or any code regarding
this will be very helpful.
Thanks,
Ambika
|
by: Øyvind Isaksen |
last post by:
Hello!
Have a site that contains both ASP (3.0) and ASP.NET files.
When I log inn, my login.asp write info to a cookie to use for
authentication in my asp.net files. One of the things I write to the cookie
is the users local IP address. My problem comes when I try to "use" this IP
address in my asp.net files. When I print out the IP adress from the cookie,
it looks like theis:
127%2E0%2E0%2E1
|
by: Bhanu Murthy |
last post by:
Dear Music lovers,
I want to copy one Music CD of my friend who brought from US. I want to burn CD using nero 'copy disk' option. I am not able to copy. Reasons displayed are read error in sector ... so on continuously,.. finally, it never copies. But the original CD, after installing, it working fine, but I am not able to copy/burn CD. Now they are out of sale, even to purchase.
Is there any way to copy.... the CD.
thank u...
| |
by: eureka |
last post by:
Hi All,
I need to store the address-bar content of a web page into a string
variable ,
Is there any way I can achieve this through JavaScript?
|
by: raviva |
last post by:
Hi there,
Can someone please explain me about the follwoing error. When I click a table name in schema browser in TOAD. I got this message.
"Access violation at address 00000364. Read of address 00000364"
Thanks,
Ravi
|
by: neridaj |
last post by:
Hello,
I'm trying to figure out how to get rid of these errors:
Notice: Undefined offset: 1 in output_fns.php on line 315
Notice: getimagesize() : Read error! in
output_fns.php on line 315
function get_imgdim()
|
by: shuisheng |
last post by:
Dear All,
I have a probably simple question but annoying me a lot.
For example:
std::stringstream ss;
int * p = new int(1);
ss << p; // Output the address
void * q;
|
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...
|
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...
| |
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,...
|
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...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |