Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I develop C/C++ programs for UNIX 3

Status
Not open for further replies.

qednick

Programmer
Jul 26, 2002
516
0
0
US
Hi all,

I've been working in C/C++ on Windows and Mac for some time now - I'm no stranger to the language. However, I'm currently working on Windows project which is meant to be peer-to-peer networking over the internet using stream sockets. I've discovered that this is not very feasable considering that some clients may be operating behind a firewall.

I can't implement a Windows based server program to process and dispatch socket messages because I would have to have a windows machine permanently hooked up to the internet which is why I wanted a peer-to-peer approach in the first place.

What I would like to do is program a small C/C++ server which will sit on my UNIX web server and process and dispatch incoming and outgoing sockets. In other words, some kind of "inbetween" to collect and forward the streams between peers.

I envisioned a small program on my UINX server with which Windows client programs would instantiate a connection by using the IP address. Then, information would be passed around to clients via the small server program.

Here's my prob - and probably a very simple one at that! - I've never programmed on UNIX before and don't even know where to start. I don't have a UNIX machine/operating system nor do I have a UNIX C/C++ compiler.
I understand that there are some options I can take here such as using a UNIX emulator and/or using GNU stuff. However, I would like to hear any suggestions/idea on this subject before I start.

Also, any info on using UNIX sockets would probably come in handy!

[rockband]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
find yourself a copy of GNU/Linux preferably a stable RedHat, Mandrake or Debaine. GNU/Linux comes with a c++ compiler (g++) and is called from the commandline.
Code:
g++ -Wall this.cc
The -Wall switch turns all warnings on... -c for compile only -o for linking only:
Code:
g++ -c Repository.cpp Duration.cpp Mud.cpp
g++ -o Repository.o Duration.o Mud.o
And you can stack the commands in a shell script or in a Makefile....
 
Once you have your linux machine up and running, and you can compile programs, the way to start with the sockets idea is this:

First of all you need to include <sys/types.h>, <sys/socket.h>, <netinet/in.h> and <arpa/inet.h>


fd=socket(proto,type,?)
creates a socket and returns a file descriptor. The first argument selects the protocol, usually internet: AF_INET, the second is either SOCK_STREAM, SOCK_DGRAM (ie. TCP or UDP) or SOCK_RAW. The ``raw'' socket gives access to the lower IP level.

bind(fd,struct sockaddr *ptr,len)
associates a host and port number with a socket. It is used by a process to inform the operating system it will deal with any connections to a port and provide the service. (Usually the host address is part is the wild-card INADDR_ANY). The last argument is the length of the address record. The actual format of a struct sockaddr containing the address and port is discussed later.

listen(fd,conn_q)
used by a process to indicate that it is prepared to receive connections. The second argument indicates the number of incoming connections to queue up. NB It does no wait for connections, accept does that.
fd2=accept(fd1,struct sockaddr *sender,len)
this causes a process to wait for a connection. When if arrives the connecting process's address is returned in the sockaddr address structure. Also a new file descriptor is created so that the process can fork a sub-process (or start a thread) to service the connection and the parent process can go back to await another request on the original file descriptor; a process doing this is called a concurrent server.

connect(fd,struct sockaddr,len)
this is used by a process to make a connection on a socket to an address contained in the sockaddr structure.


Hope all of this makes sense and helps - let me know if you need more help.
 
Guys, thanks for all your help - I'm used to making posts in the VC++ forum and not getting replies! This is great!!

I had actually downloaded a PDF doc from the web which gave me some info on sockets. Taking into account my lack of experience with UNIX and my lack of a copy of UNIX/Linux I tried to write my first small program. I called the source &quot;test.cpp&quot; and uploaded it to my UNIX web server. I then used Telnet to compile it on the server which created a file called &quot;a.out&quot;. I ensured the file was executable (I just chmod'ed it to 0777 for the hell of it).

However, I do have a couple more silly questions now which have arisen from all this:

1. How the heck do I get this program to actually execute and print something out?

2. Can it be executed remotely by using the URL web address (just like a cgi script)?

3. How long does the program remain in existence once it has been executed? (ie. what I mean is, if I incoporate some kind of loop in there, will the program keep running on the server and, if so, until when?)

4. Suppose several clients link (and execute) the program remotely. Are they all running entirely separate instances of the program or can they share information?

I told you they were silly questions but you all get a star for putting up with me!! :)

[rockband]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
As far as not having a box to work on, you could always dual-boot, and like jstreich said g++ is the c++ compiler for unix/linux and it comes with the OS.
If you don't want to dual-boot unix/linux, just find a friend who has a box hooked up to the net, and get them to set an account up for you. Then you can use telnet or putty to work from your windows box on their unix/linux box.
If neither of those choices are an option. Just get yourself a cheapo 350Mhz box or so with minimal specs (preferably for free :p). You dont need a monitor or keyboard for it (except when you install the OS), just a box with a nic - hook it up to your network and telnet to it from your windows box. It wont be uber-fast, but its cheap and it works.


I am in a class right now where we are learning sockets for Unix


 
hmmmm, guess you posted while I was reading -- :p, maybe I should learn to type faster.
 
> 1. How the heck do I get this program to actually execute and print something out?

Just type a.out<Enter> in your telnet window. What happens?

> 3. How long does the program remain in existence once it has been executed? (ie. what I mean is, if I incoporate some kind of loop in there, will the program keep running on the server and, if so, until when?)

Normally running programm occupyes your terminal connection and stops when you press Ctrl/C or telnet session is broken.
You can start looped program in the background, adding &quot;&&quot; to the end of the command line:
a.out&
It will be running forever, even after you leave your telnet session.
 
Dan,
-c is compile into object code, which in and of itself is useless.... It must be linked and assembed.

-o is to link object files, the arg after -o can be either the first object file or the name you want to give the executable. The -o switch links the .o (object code) and assembes the object code into binary that the machine will be able to excute.
When I said -o for linking only, I meant to say it doesn't actually compile anything... It assembles and links.

-----
1.) type a.out on the cammond line... Or if the program is saved as a cgisrcipt write it's location into a webbrowser.

2.) As above, yes. It must, however, be inside of the cgi-bin directory for the webserver, called correctly and output in HTML form to the STDOUT stream.... cgi is easies t to do in Perl.

3.)The program will continue to excute until a terminating signal is sent or the main returns (the final time if it has recursive main function). A term signal can be created by a kill command, a kill -9 or by closing the running terminal window... the ampersand will execute the cproccess in it's own terminal (weather or not that terminal opens depends on if the program has a display). Most termination signals can be caught and ignored, the only one that can't is kill -9 ... Also, powerloss or other unforseen crash can also end a programs termination.

4.) Server programs and sockets... wee... Actually, even without a server, prcessess can share information using semiphores and memory mapping. In Unix C/C++ pipes and filters (and Memory mapped files and semiphores) are easier to use than client-server operations. Slaving terminals and sharing a proccess also works... but it gets slow...

So, here are the differnt models for multi user programs...
Shared resources - memory mapping and semiphores
Several peer process (several copies of the same program) politly follow conventions for notifiny the others of the state of the resource. This is how older DB programs worked. Cgi scripts are often still written this way.
Slaving terminals
One process, that writes to several user's displays. Since it is the only proccess using the resource it doesn't have to worry about the fact that it has multipule users, because it is the sole user of the resource. Slower and only really popular with telnet muds and old-style Bulitian Boards.
Cleint-server
One process that controlls the resource getting infromation from smaller programs which there are multipule copies of. Has the benifits of the Slavig Terminals approach, but the speed of equal clients (given that the server is kept slim). Most internet applications today are this type.

Just a taste of the different way to do this.
 
jstreich,
-o is to link object files, the arg after -o can be either the first object file or the name you want to give the executable. The -o switch links the .o (object code) and assembes the object code into binary that the machine will be able to excute.
When I said -o for linking only, I meant to say it doesn't actually compile anything... It assembles and links.

The -o is used to put the output of the compilation/link/whatever in a file. For instance, most Makefiles use something similar to this:
Code:
g++ -osourcefile.o -c -O2 sourcefile.c
If your theory would be correct, this obviously wouldn't work. But it does, and is, as I said, what most Makefiles use. //Daniel
 
I do indeed stand corrected. I suppose the reason for my confussion is that -o only makes sence (well is praticle) when linking and assembling (using -o to rename object files makes little sence).....

I guess in short, I am trying to say, I was wrong.

qednick:
You need only compile the objects
Code:
g++ -c this.cpp that.cpp theother.cpp
and then link & assmeble
Code:
g++ this.o that.o theother.o

-o switch to save the excutable (or objects) under a different name

-O switch to optimize code

-g to generate code that has built in debugging information.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top