Programming Help Forums
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        



Struct Problems storing Client IP-address Expand / Collapse
Author
Message
Posted 9/22/2011 10:03:18 PM
Forum Newbie



Group: Forum Members
Last Login: 9/22/2011 10:26:15 PM
Posts: 1, Visits: 2
Hi everyone,
I got stuck again, this time with a different problem though. Writing on a little chat server based on “Beej’s Guide to Network Programming” and I tried to save each incoming connection into an array, so that every time a client posts something that it also will echo the clients IP-address.
For example:
127.0.0.1: Hello, anybody out there?

I got everything working but right now I just display following output:
Socket 4: Hello, anybody out there?

So in order to store the IP addresses as soon as a client connects, I created following struct.


struct connection
{
const char *IPv4_addr;
};


And created an array variable of type struct connection;

struct connection client[6]; // client information


Then I try to store the client IP in this array and I use the following command:

client[newfd].IPv4_addr=inet_ntop(remoteaddr.ss_family,get_in_addr((struct sockaddr*)&remoteaddr),remoteIP, INET_ADDRSTRLEN);


Here is the function it is in

//----------------------------------------------------------
// CHECK EXISTING CONNECTIONS FOR DATA
//------------------------------------
for(i = 0; i {
//------------------------------------------------------
// CHECK FOR NEW CONNECTIONS
//--------------------------
if (FD_ISSET(i, &read_fds)) // we got one!!
{
if (i == listener)
{
// handle new connections
addrlen = sizeof remoteaddr;
newfd = accept(listener,(struct sockaddr *)&remoteaddr,&addrlen);
if (newfd == -1) // If connection attempt fails
{
perror("accept");
}
else
{
FD_SET(newfd, &master); // add to master set
if (newfd > fdmax) // keep track of the max
{
fdmax = newfd;
}
client[newfd].IPv4_addr=inet_ntop(remoteaddr.ss_family,get_in_addr((struct sockaddr*)&remoteaddr),remoteIP, INET_ADDRSTRLEN);
printf("selectserver: new connection from %s on socket %d",
/* -> */ inet_ntop(remoteaddr.ss_family,get_in_addr((struct sockaddr*)&remoteaddr),remoteIP, INET6_ADDRSTRLEN),newfd);
}
}


And right in that line where it says:

client[newfd].IPv4_addr=inet_ntop(remoteaddr.ss_family,get_in_addr((struct sockaddr*)&remoteaddr),remoteIP, INET_ADDRSTRLEN);


Is my problem, since I’m using a pointer and it always clear the entire content of that variable as soon as another client connects because it keeps pointing to the same spot in the memory. It works perfectly fine for a single connection (of course…..)
Now I unfortunately can’t change it, since the function I use in that same line requires a pointer at that time I call it.
So what else could I do, to bypass this problem and somehow store each clients IP-address in that client array of type struct connection?

I hope You understand what exactly my problem is.
The entire code is attached in a .zip file.


I really hope you guys can help me out and I’d appreciate each suggestion that leads me the right way, since this is bugging me for a few days now.

Thanks,
~Stud


  Post Attachments 
clean_print.zip (0 views, 2.59 KB)
Post #7782
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.

Permissions Expand / Collapse

All times are GMT -5:00, Time now is 10:50pm

Homework help programming
Execution: 0.718. 12 queries. Compression Disabled.