Client/Server Ip Address Array

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 8328 Views - Last Post: 19 November 2010 - 09:12 AM Rate Topic: -----

#1 junior?  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-March 09

Client/Server Ip Address Array

Posted 19 November 2010 - 02:31 AM

Hello
I am having difficulties with my program. It's a client server connection, that server broadcasts to the network so the computers in network can reply back to the server with their ip, but when i store the inet_ntoa(SenderInfo.sin_addr) in my iparray[] in the next ip address all positions in array are replaced by the current one. I cant get them all in the array. As i checked around its because of the inet_ntoa being static. Any help would be appreciated.
ps:Everyone says strcpy inet_ntoa to my array but i dont quite get it

I am using Visual Studio 2010 C

Is This A Good Question/Topic? 0
  • +

Replies To: Client/Server Ip Address Array

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 02:37 AM

Is iparray[] a 1D character array or a 2D array or an array of pointers or what?

If it is a 1D character array then it can only hold a single string which seems to match what you are describing.

Basically, as usual, the help we can give without seeing your code is very limited.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6549
  • View blog
  • Posts: 30,679
  • Joined: 10-May 07

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 02:44 AM

View Postjunior?, on 19 November 2010 - 03:31 AM, said:

when i store the inet_ntoa(SenderInfo.sin_addr) in my iparray[] in the next ip address all positions in array are replaced by the current one


Can you give a little more detail on the above? I'm not sure why using two variables wouldn't solve this... how many ip addresses are you getting?
Was This Post Helpful? 0
  • +
  • -

#4 junior?  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-March 09

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 03:47 AM

well i'm using a 1d array, and when stores the first ip address received goes on to connect to the next one and when it stores the next one's ip, it replaces both spaces in array, and not only the second one.

		NewConnection = accept(s, NULL, NULL);
						printf("\nServer: accept() is OK...\n");
						printf("Server: New client got connected, ready to receive and send data...\n");
						connection++;
						// At this point you can do two things with these sockets
						// 1. Wait for more connections by calling accept again on ListeningSocket (loop)
						// 2. Start sending or receiving data on NewConnection.
						ByteReceived = recv(NewConnection, recvbuf, sizeof(recvbuf), 0);
						

						// When there is data
						if ( ByteReceived > 0 )
						{
							printf("Server: recv() looks fine....\n");
							// Some info on the sender side
							// Allocate the required resources
							memset(&SenderInfo, 0, sizeof(SenderInfo));
							nlen = sizeof(SenderInfo);
							
							
							iparray[ip] =inet_ntoa(SenderInfo.sin_addr);
							ip++;
							
							getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
							printf("Server: Sending IP used: %s\n", inet_ntoa(SenderInfo.sin_addr));
							printf("Server: Sending port used: %d\n", htons(SenderInfo.sin_port));
							// Print the received bytes. Take note that this is the total
							// byte received, it is not the size of the declared buffer
							printf("Server: Bytes received: %d\n", ByteReceived);
							// Print what those bytes represent
							printf("Server: Those bytes are: \"");
							// Print the string only, discard other
							// remaining 'rubbish' in the 1024 buffer size
							for(i=0;i < ByteReceived;i++)
								printf("%c", recvbuf[i]);
							printf("\"");
						}
						// No data
						else if ( ByteReceived == 0 )
							printf("Server: Connection closed!\n");
						// Others
						else
							printf("Server: recv() failed with error code: %d\n", WSAGetLastError());
					}
					
					// Clean up all the send/recv communication, get ready for new one
					if( shutdown(NewConnection, SD_SEND) != 0)
						printf("\nServer: Well, there is something wrong with the shutdown(). The error code: %ld\n", WSAGetLastError());
					else
						printf("\nServer: shutdown() looks OK...\n");
					
					// Well, if there is no more connection in 15 seconds,
					// just exit this listening loop...
					if( recvTimeOutTCP(s, 0, 0) == 0)
						break;
				}


This post has been edited by no2pencil: 19 November 2010 - 03:51 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 04:09 AM

From Beej:

Quote

inet_ntoa() returns the dots-and-numbers string in a static buffer that is overwritten with each call to the function.


Given that info, it is obvious you need to make a COPY of the return from that function before saving it in your array.
Was This Post Helpful? 0
  • +
  • -

#6 junior?  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-March 09

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 04:13 AM

can you be a bit more specific on the point of COPY?
u mean crate another function like that in my code?
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 05:18 AM

Show how you're defining ipArray.
Was This Post Helpful? 0
  • +
  • -

#8 junior?  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-March 09

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 05:34 AM

char* iparray[10];
like this
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 05:55 AM

OK, so this:
char *iparray[10];

creates an array of 10 char *s. They don't point to anywhere, or more likely they point to random areas in memory to which you cannot write. I recommend declaring this as:

char *iparray[10] = { 0 };


which makes them point nowhere at all.

You will need to allocate memory for each of those pointers before you can do anything with them. So what you need to do is the following:

Get the ip address into a variable:
char *thisIPAddress = inet_ntoa(SenderInfo.sin_addr);


As stated above, this points to a static area in memory that contains the IP address, so you need to make a copy of this data so that you will be able to get the next address on the next call to inet_ntoa. You first create the memory in your iparray entry by determining how much memory the address needs. You need the length of the current IP address PLUS ONE for the required NULL terminator. Like so:

iparray[ip] = malloc(strlen(thisIPAddress) + 1);


Now you have memory YOU own that you can write to. It is here that you use strcpy to copy the bytes in memory from thisIPAddress to iparray:

strcpy(iparray[ip], thisIPAddress);


Now that you've created (allocated) this memory, you will later on -- after you are done with it -- need to deallocate (free) the memory or you will end up with a memory leak. You will do that like this:

int i = 0;
while (i < sizeof(iparray))
{
    free(iparray[i]);
}


Here is an excellent reference page on pointers.
Was This Post Helpful? 2
  • +
  • -

#10 junior?  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-March 09

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 06:20 AM

Your reply was very clear and thanxs but it seems that here
iparray[ip] = malloc(strlen(thisIPAddress) + 1);
void* cannot be assigned to type char*
any ideas?
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 06:22 AM

It seems you're compiling your code as C++. C++ requires that you cast the return value of malloc:

iparray[ip] = (char *)malloc(strlen(thisIPAddress) + 1);

Was This Post Helpful? 1
  • +
  • -

#12 junior?  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-March 09

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 06:37 AM

You are my savior thanks a lot
all working great now getting all ip address of lab, and i can flash all computers in lab :D
thank you very very much
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,600
  • Joined: 16-October 07

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 06:42 AM

View PostJackOfAllTrades, on 19 November 2010 - 07:22 AM, said:

It seems you're compiling your code as C++. C++ requires that you cast the return value of malloc:

iparray[ip] = (char *)malloc(strlen(thisIPAddress) + 1);


Bonk! If it's C++...

iparray[ip] = new char[strlen(thisIPAddress) + 1];


Though, honestly, it's C++:
string iparray[10];
iparray[ip] = thisIPAddress;



Better yet:
vector<string> ipList;

Was This Post Helpful? 1
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 08:19 AM

baavgai, there's no actual C++ in the code posted; it uses C I/O so I just went with the assumption that the code is probably C but being compiled as C++. But on the whole, you are correct.

EDIT: The other alternative, junior? is to actually compile the code as C, if it really IS C. There is a project setting you can change, Project->Properties->Configuration Settings->C/C++->Advanced->Compile As. I also think if you name the file as filename.c, rather than filename.cpp, this will occur automatically.
Was This Post Helpful? 1
  • +
  • -

#15 junior?  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-March 09

Re: Client/Server Ip Address Array

Posted 19 November 2010 - 08:45 AM

Ye the whole code is C but it still needed the char* at the front to work
dont know the reason but anw it works like a charm now :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2