March 5th, 2003, 06:59 PM
Networking and Login Program (windows) in C
When some one connects to a certain port I want to run a program to simulate a unix login. My problem is how to run the program everytime someone connects to the port. I don't know exactly if this is a programming problem.
I only know the basics of C, no network related programming experiece.
Note: I want to use netcat (on windows) to listen for incoming connections.
Last edited by DarkSpy; March 5th, 2003 at 07:11 PM.
March 5th, 2003, 07:24 PM
RTFM. Seriously, it contains an example.
General knowledge: A TCP server application is bound to the port that it is listening to, so any client that connects to that port will connect to that server. While a server is bound to a port, no other application may bind to it as well; that attempt will fail and generate an error.
March 5th, 2003, 07:41 PM
Your saying that I can't do what i want?
What FMs should I read? (post some links)
March 6th, 2003, 12:22 AM
RTFM == "Read the Furnished Manual"
It's the standard response to a question that is answered in the documentation that accompanied the application.
I also used it for good effect on my son when he didn't have a clue how to work a math problem, so I opened his book, showed him the applicable example problem, and told him "RTFM". It only took a few times for him to learn to check the examples before claiming defeat.
README.TXT and HOBBIT.TXT are part of the netcat distribution and they both describe how to use netcat. There is a description in README.TXT that ties together netcat and cmd.exe on Windows NT that enables you to run the command line shell remotely. If I understand correctly, that is similar to what you are trying to do.
March 6th, 2003, 07:00 AM
I know what RTFM means.
I think I know how to solve my problem. But now I got new questions:
1) Where can I find intermediate tutorials in C? Because when you refer to RTFM, I don't know what manuals to read. I don't guess header files and what they do.
2) How can I call an aplication inside my code?
3) Is there a delay function (I think it is sleep() )? What's the header file related to the function?
4) What are the differences between gcc 2 and gcc 3.
(Maybe it is off topic questions but since it's on the following of my first post, I decided to put them here)
March 6th, 2003, 02:25 PM
I guess I was being a bit more cryptic than usual. The general political climate tends to make one a bit more suspicious. And your moniker didn't help matters much. To be honest, I hesitated responding in the first place and nearly aborted my first response a couple times. Even then, I felt hestitant to give any more information than you already had available. netcat, the networking "Swiss Army Knife", has been used by hackers -- in a TechTV demo of an attack, netcat was used to take over the target system. But then I figured that if you were a hacker, then you would/should already know more, and if you were a "script kiddie", then you wouldn't be trying to figure out how to do it but rather find a ready-made tool.
I tend to share my UNIX/Linux instructor's philosophy that it is less important to know how to do a specific task and much more important to know how to find out how to do it -- especially since the specific procedure can change drastically as you go from one system to another (eg, between Red Hat, Solaris, and FreeBSD) or from one version to another. A variation of the old "Giving a man a fish vs teaching him how to fish" saw.
RTFM usually refers directly with the "manual" that came with the software or system. In that sense, I was refering directly to the two text files, named in my last response, that come with netcat. Of course, I'm assuming that you got the distribution and not just a lone executable.
More generally, there are the help files on Windows (some on Win2k are actually helpful, I was pleasantly surprised to discover) and the "man pages" on UNIX/Linux (currently being superceded by info files). And FAQs to be found on the Web; e.g., on the software/system company's/organization's web page.
Now the question I have is, what system are you running this under? You have referenced both Windows and gcc. Are you working both on Windows and Linux or just on Windows with a port of gcc? In the former case, the man pages are available to you under Linux; at the shell prompt type 'man sleep' for the shell command and 'man 3 sleep' for the C function -- some function man pages seem to call for '2', so be flexible until you catch on to the system. In the latter case, you would need to read the man pages on the Web. Just have your favorite search engine search for '"man page" sleep', for example, and you'll get lots of hits.
Another more immediate method would be to grep the header files for the function name. This is also very useful for looking up the fieldnames of the structs that some of the functions use (eg, struct tm for the time functions).
As for recommending a tutorial, I'm not up-to-date. I'm working at a well-experienced journeyman level (though weak in file I/O since I've been doing embedded work for 8 years), so I no longer work with C tutorials, but rather will research through the documentation and look for discussions and code examples on-line. Besides, any beginning or intermediate tutorials that I may have used in the past would most likely be out-of-print or superceded by something much better. Sorry, but someone more recently at your current level would be a better source for references. Otherwise, there are a number of tutorials on the Web; I just don't know of any off the top of my head.
Again, that depends on what system you're using.
On UNIX/Linux, the most common method is to fork a child process and then have the child make an exec*() call. exec*() represents a family of functions that do the same thing but take different parameters; do a 'man execl'. The reason for forking a child process first is because exec*() replaces the process calling it with a new process for the application being called. Of course, if you don't want your original program to continue running, then you don't need to do the fork. There are other behaviors that you will need to consider and research.
In this case, I am at a beginning level in UNIX systems programming. My primary sources now are "Linux Programming by Example" by Kurt Wall and "Beginning Linux Programming, 2ndEd" by Stones and Matthew. Both books discuss forking processes and the exec*() family.
Under Windows, I'm not quite sure. Forking is not native to Windows and would not be supported unless the compiler port tries to simulate it. exec*() is supported, but I don't have experience with it. According to the Visual C++6 help file, the exec*() family also replaces the calling process, just as in UNIX. Whether it behaves the same way in the gcc port, I don't know.
Windows also has a similar family of spawn functions which create a new process but keep the calling process running, similar to forking before exec'ing. I haven't seen reference to it in Linux, but rather I think that it was introduced in DOS versions of C to compensate for the lack of forking. However, it does appear to be in my gcc port, since grep found it in process.h along with the exec*() prototypes.
Plus, there's the system() function, which I completely forgot about but somebody just brought up in the "What the system command returns. Is it safe to use?" thread.
The delay function is sleep(). As described above, read the man page. Here is the information you requested from the man page:
But there's a gotcha that depends on which system you are using and possibly which Windows port of gcc. You see, Win32 also has a sleep() function now, but you pass it milliseconds instead of seconds. I wrote a short test program and compiled and ran it both under Win32 and on Linux. Both cases slept for ten seconds, but I had to use different parameters for sleep().
unsigned int sleep(unsigned int seconds);
sleep() makes the current process sleep until seconds sec-
onds have elapsed or a signal arrives which is not
Zero if the requested time has elapsed, or the number of
seconds left to sleep.
and under Linux
Reading specs from C:/DEV-CPP/BIN/../lib/gcc-lib/mingw32/2.95.3-6/specs
gcc version 2.95.3-6 (mingw special)
// schlaf -- tests the sleep function
Some ports of gcc might "do it right", but you need to test your own compiler to see whether it expects seconds or milliseconds.
[dwise@pc10593 schlaf]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.0)
[dwise@pc10593 schlaf]$ cat schlaf.c
// schlaf -- tests the sleep function
I don't know off-hand, but you can look it up at the GCC page of the GNU Project, http://gcc.gnu.org/ . I had to drill down through a few pages, but I found "GCC 3.0 New Features" at http://gcc.gnu.org/gcc-3.0/features.html . There should be similar "New Features" pages for the other versions as well.
March 6th, 2003, 06:18 PM
You're right.. the reason I do this is only for learning. I haven't got enought knowledge to write a real program.
I want to code in a Linux OS, (Currently waiting for slackware 9. I don't have any Linux distro installed right now), but the program I want to code will run on a Win98 OS, so a will use a gcc port (probably MinGW).
My problem when searching for resources is: "What exactly am I looking for?". I knew about the sleep function, but what happens when I don't know if certain function exists or not? So I won't search for it.
For example when I am writing a program my code could become very long, instead of using a simple function that a didn't know about. (I don't know If I'm making myself clear).
What a really wanted to know was in terms of coding. Like if there is a certain function that does something different in v3, Or a header file missing, etc.
I think I missed something in my post cause I accidently closed the browser when I was writting it.
Thanks a lot for all the information.
March 6th, 2003, 07:32 PM
Most C compilers should support the standard libraries and be consistent with the ANSI standard. That should not change from version to version. What will usually change is bug fixes, optimizations, new compiler options, and support for features that weren't supported before.
Most of what you or I would write should be supported by either version and should behave the same if compiler under either version.
If you're thinking of getting either Linux systems programming book I mentioned, you should know C first. My impression is that you are familiar with the basics, but aren't sure about what the standard libraries have to offer. There are tutorials on-line; I Googled on 'C tutorial' and got pages of hits. And there are books that will give you enough of an overview. They've been around for so long that I'm sure some are in most used book stores.
You could/should also read other people's code for ideas. On Source Forge or freshmeat.net , look for a project or utility that does something you're interested, download the source code, and read through it. If a particular function gets used, then you can research it.
Plus, you can ask here whether a function exists that can ... .
Come to think of it, read through some of the recent threads here about pointers. One member has been posting links to a FAQ or tutorial that explains the question. That might be the online tutorial/reference that you're looking for.
Good luck and have fun.
March 7th, 2003, 11:06 AM
Another source of information for you, especially since you will be going to Linux, is the Linux site at http://www.linux.org/ . The documentation section (see menu buttons on left side of page) includes the HOW-TOs, which include both system setup suggests and how to approach certain projects (eg, serial port comm, which I'll be needing for a near-future project -- before reading that HOW-TO, I didn't realize that termios was what I would need).
You can also FTP the man pages for off-line reading before you get Linux. They're ASCII files, but formatted with nroff (I think), which leaves them still readable.
Then, either by looking at the man-page names or at a reference-book list of standard library functions and header files, you can see the names of what functions are available. After you've gotten a feel for the terse naming conventions of C, you should be able to spot functions that look like they'd be interesting or useful and then read the documentation about them.
March 7th, 2003, 02:01 PM
If you're gravitating towards network programming, I would recommend a short (110 pages) and inexpensive ($15 US) book that gives you the straight skinny and should get you writing sockets programs in a short time.
"The Pocket Guide to TCP/IP Sockets: C Version" by Michael J. Donahoo and Kenneth L. Calvert, Morgan Kaufmann Publishers, 2001, 130 pages, ISBN 1-55860-686-6, US$ 14.95
Web page: http://cs.baylor.edu/~donahoo/PocketSocket/
Second edition has been renamed:
"TCP/IP Sockets in C: Practical Guide for Programmers", ISBN 1-55860-826-5.
Web page: http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/
They also wrote a similar book for Java.
The web sites contain source code from the books, errata (unavoidable), and additional material and tutorials. Check both sites, because not all the additional materials is duplicated.
The book deals only with sockets programming under Unix and they have instructions on their sites about converting those programs to Winsock as well as their converted examples. Winsock uses almost all the same sockets functions, so it only takes a few changes to convert to Winsock.
Before getting this book, I was stuck for a year in "analysis paralysis" studying up on TCP/IP and sockets but not feeling ready to actually code anything. This book got me coding, so I recommend it highly.
March 7th, 2003, 03:28 PM
I know how to work with linux. But thank you once more for the information :).