Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to show only those that are not equal to 192.168.1.103 but it's not compiling.

printf("\n         source port      : %ld", htons(tcp_header->source_port)<>192.168.1.103);
Posted

printf("\n         source port      : %ld", htons(tcp_header->source_port)<>192.168.1.103);

This statement makes no sense, no wonder it does not compile. You are also confusing a port number with an IP address by the look of your code.
You need to convert both your IP addresses to integer values via inet_addr()[^] and then make a simple numeric comparison. Then use something like:
long IPaddress1 = inet_adr("192.168.1.103");
long IPaddress2 = inet_adr(source_address); // string
if (IPaddress1 != IPaddress2)
    printf("IP address: %s\n", source_address);
 
Share this answer
 
Comments
Member 7766180 23-Jul-11 10:19am    
I did this and got the message. Cannot convert from 'const char[14]' to long'.

#define inet_adr
long IPaddress1 = inet_adr("192.168.1.103");
long IPaddress2 = inet_adr(ip_header->source_ip); // string
if (IPaddress1 != IPaddress2)
printf("IP address: %s\n", ip_header->source_ip);
Richard MacCutchan 23-Jul-11 10:38am    
I'm not surprised, why have you added the line #define inet_adr? Yet another reason why your code won't compile.
Member 7766180 23-Jul-11 10:21am    
Also if they are not equal what will it return?
Richard MacCutchan 23-Jul-11 10:39am    
TRUE
Are you sure you understand the C/C++ language?
Member 7766180 23-Jul-11 10:41am    
It ws aded because it says that inet_adr was not defined. What should I do?
You're going to want something like
int *addressValue = new int();
char *address = "192.168.1.103"; 
inet_pton(AF_INET, address, addressValue);
if (htons(tcp_header->source_port) != *addressValue)
  printf("\n         source port      : %d", htons(tcp_header->source_port));


That should convert the string representation of the IP address to a number, then compare it to the other address you're getting.
 
Share this answer
 
Comments
Member 7766180 22-Jul-11 15:57pm    
I'm receiving these errors.
Error 1 error C3861: 'inet_pton': identifier not found
Error 2 error C2065: 'tcp_header' : undeclared identifier
Error 3 error C2227: left of '->source_port' must point to
class/struct/union/generic type
Error 4 error C2065: 'tcp_header' : undeclared identifier
Error 5 error C2227: left of '->source_port' must point to class/struct/union/generic type
6 IntelliSense: identifier "inet_pton" is undefined
7 IntelliSense: identifier "tcp_header" is undefined

This is my TCP Header
typedef struct _TCP_HEADER_
Member 7766180 22-Jul-11 16:13pm    
I added this header #include <ws2tcpip.h> this took care of the inet_pton.
Now it's just the tcp_header
Member 7766180 22-Jul-11 16:59pm    
This is changed a little from the deleted post, simpler but it doesn't work. 192.168.1.103 still appears?

if (htons(u_short(ip_header->source_ip)) == *addressValue)
{
printf("\n Source IP: %s", "0.0.0.0");
}
else
{
printf("\n Source IP: %s", ipSrc);
}
Philippe Mori 22-Jul-11 19:11pm    
@lewax00: You are comparing only the first char when using *addressValue. To compare the whole string, either uses std::string or a function like strcmp
@Member 7766180: You cast to u_short probably truncated you address from 4 bytes to 2 bytes (assuming that source_ip is an int). By the way, casting is bad. Uses the same type on both side! It does not make sence to call new on a int in C++ as it is not a garbage collected language.
lewax00 19-Oct-12 10:03am    
I know this is late, but it does not compare only the first character, you should look up what inet_pton does: http://www.kernel.org/doc/man-pages/online/pages/man3/inet_pton.3.html
I believe you must use !=.
 
Share this answer
 
Comments
Member 7766180 22-Jul-11 15:20pm    
Doesn't work. Says operator has no effect. Expected operator with side effect.
printf("\n Source IP: %s", ipSrc)!=atoi("192.168.1.103");
[no name] 22-Jul-11 15:32pm    
It's because your syntax seems to be wrong. Your original code and the code in the comments are different.
Member 7766180 22-Jul-11 15:38pm    
I know, posted the wrong one, second one is correct. I added the atoi so that the data types match. However, I still don't have a solution. <> doesn't work and != gives me that error I described above. What is the correct synatx. (second example)
Member 7766180 22-Jul-11 15:59pm    
Did this because its comparing ipScr to 192.168.1.103
Almost worked, then program froze....
printf("\n Source IP: %s", ipSrc != "192.168.1.103");
[no name] 23-Jul-11 1:21am    
What are you trying to print? if you just want to print the IP,
printf("\n Source IP: %s", ipSrc); should work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900