I need to implement a TCP Server which should listen on port 1234 and should have ip=192.168.1.1,,,,
It should be able to accept connections of same subnets clients i.e.
When it gets desired client should say to him Hello otherwise others should be welcomed "Good Bye"..
Any suggestions ??
Why those IP addresses specifically?
The IP address of the server program will depend on which computer you run it on. The IP address is technically assigned per network interface (e.g. ethernet card in your computer or wifi connection). So if the computer you are running this on is 192.168.1.1, great. Otherwise you need to reconfigure it's network settings or run it on another device.
Network programming is not the same for all OSes. Linux, Windows and MacOS all do things a little differently. The core concepts are the same, and even the APIs are very similar, but they are not 100% compatible.
Then, you need to study up on network programming. I like Beej's Guide. It's Linux oriented, but it's a great tutorial, and all of the concepts carry over to MacOS or Windows quite well. Google will turn up many more, for whatever OS you use.
Working through a good guide (no rushing or skipping steps) should give you some ideas of how to do your filter.
Originally Posted by anduril462
Actually i had been through beejs guide already and had practiced on linux and yes i agree that ipadress cant be forced...but i want to do like this on linux OS but it seems imposible on localhost
Filtering a class of ipaddresses is what i want to learn........
A specific class....
Had already done practice of network programming using linux....
but in the current scenario is lacking vision a little bit
you'd do well to study up on the linux firewall, iptables, and learn how to filter TCP traffic with it.
Originally Posted by Javed Iqbal
All this stuff is in Beej's guide. The section on bind() discusses listening on specific network interfaces (it uses getaddrinfo to get a list of local interfaces you can listen on). The section on accept shows you how to get the address of the person connecting to you. Also, he has man pages for bind and accept. Read them.
I'm being intentionally vague with my help for two reasons:
The first is that, it's good for you. If I do all the thinking and spew out an answer you don't learn much. Also, learning to dig up the documentation, and be able to read and understand it, are crucial skills for any programmer, even the "hobbyist".
Second, this forum is not a place for free code. It's actually against our forum guidelines. You will need make an attempt at this, and post your code here, and ask specific questions about what you don't understand. Make sure you do at least the 3 following things, to make it easy for us to help you:
- Post your code as plain text in [code][/code] tags, so it's easy for us to read.
- Post properly formatted/indented code (make sure it looks good in this forum, not just your editor), again, so it's easy for us to read.
- Make sure you check all your socket-related functions for errors. If an error occurs, print a useful message (e.g. using perror) and act appropriately (e.g. try again, continue anyway, or exit).
I need to know that if i will be able to programme so..how to check on linux host i.e i will be provided with localhost only...how to check for other ant ip addresses for testing purpose using single machine..
Originally Posted by anduril462
Okay, here's some code to get you started:
Seriously, you said you've done network programming before, so why can't you at least attempt this, and post something? Beej's guide has code you could literally copy-paste, that would take care of 90% of what you need. I already explained, I wont write code for you.
// you fill in this part
My comment #6 explains the 3 functions you need to do what you want. That's all the help you're getting until I see some real effort on your part.