Posted By: Shining Arcanine | Feb 26th @ 10:43 PM
page 1 of 1
Comments: 8 | Views: 668
I am taking a Computer Networking class and for my first homework assignment, my professor wants each of us to write a multithreaded web server for Unix in Java.

I am no fan of Java and I have learned to hate it very much. I pointed out a line ending problem involving Java's handling of the HTTP Headers across different systems to my professor and asked if it was okay if I did the assignment is another language, say C, to avoid the problem entirely. At the time I did not realize the significance of what I had pointed out (my professor spent a few minutes on it making me realize its scope) and jumped on the opportunity to ask if I could do the assignment in C. My professor was not very enthusiastic about that idea, likening it to (I don't remember the example so I am fabricating one) replacing a car because it needs an oil change.

I find Java's libraries to be so extensive that I have little clue as to what it is actually doing at any point in time and I am not willing to dig through Sun Microsystems' millions (billions?) of lines of published source code to find out. Because of this, I have decided to go ahead with writing my assignment in C to learn what is actually happening under the hood despite my professor's opinion of writing it in C and then rewrite it in Java after I am happy with how it works in C so I can submit my assignment without having to deal with any drama from my professor.

My problem is that since my university's Computer Science department has largely abandoned C in favor of Java, I have not had much practice to be able to learn about the various system libraries and I have no clue what library I am supposed to open a socket to be able to listen to ports, accept incoming connections, communicate over those connections, etcetera. I pulled out my copy of The Unix Programming Environment (which I hate to admit that I never found time to read until today) expecting to find a description of how to do networking in it, but there was none to be found. I visited the Single Unix Specification at unix.org, but there is so much there I don't know which library I should be examining.

Could someone point me in the right direction as to what library to use in C to do networking on Unix?
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
You will have to use Berkeley Sockets, unless you want to use some external third party library. Berkeley Sockets are available on every Unix variant, and even on Windows as WinSock (socket code is largely portable, except for some WinSock specific initialization functions that you have to call on Windows, and different headers of course).

Most of the functions you will need are in <sys/socket.h>, your starting point will be the socket function which creates a socket. Depending on if you have the proper stuff installed, you can just type "man socket" to get the documentation. Or you can look here.

Note that this isn't a HTTP library. It's just bare sending bytes across the network, it'll be up to you to do all the work. You'll also need a threading library, such as pthread or simply use fork, since C has no built-in notion of threads. In any case, you can expect this to be a lot more work in C than it would've been in Java. And having been a student assistant for several programming courses (so I had to grade assignments), I can tell you that the graders will hate you and almost automatically be extra hard on you because you are causing them extra work. Also note that in the real world, you will rarely have the opportunity to always use your preferred environment, so better get used to it.

I'm curious what you were doing in Java that caused this problem. Was this some HTTP library or were you using sockets?
DCMonkey
DCMonkey
Monkey see, monkey do, monkey will destroy you!
socket
bind
listen
connect
accept
recv
send

http://edn.embarcadero.com/article/26022

You're going to need a threading library too, unless you're going to just fork processes (but that would be multiprocess).

I would have stuck with Java. 

vesuvius
vesuvius
Das Glasperlenspiel
In the real world, you will be just the type of developer I will not want to employ - however much of a brainbox you might be

My reasons are:

Your Professor has already dissuaded you from choosing the path you are taking, and you have basically ignored him. This indicates insolence, and is a poisonous trait when you have a software project with a team of developers.

You are frightened of leaving your comfort zone i.e. C/C++ and in this day and age, I could achieve what you need to do in 30 seconds using WCF. I know the object is for you to learn the why rather than the how, but the lesson you are failing to learn is to use the right tools for the job, time-wise and cost effective wise.

Through the years I have countenanced a great many learned friends, that if I'm honest are intellectually superior than I, but they lacked the key decision making abilities hence remain coding, and are flummoxed why they are never allowed to become system architects.

Your stubbornness is the type that drives a $500 000 software project to $5 000 000, and even more in maintenance because you have failed to use the correct tools for the job.

Obviously I mean no offence, as you are very smart, but I only wish to precipitate a train of thought that you may choose to ignore. If you continue down the path you are choosing, then you have already lost 50% in your coursework, and Java really is not that hard. Like .NET it is laden with Library after Library and I would say that your should be able to research a multi-threaded Java web service very easily, as surely there must be oodles and oodles of people that have run into the same problem.

Good luck with your assignment.
In addition to the rather wise advice everyone else has already given, which I'd strongly recommend you take. I'd also point out that writing it in C and expecting a port to Java to be a trivial task is setting yourself up for a rather big fall.
figuerres
figuerres
???
what port?  that would be a whole different program.... other than at a very abstract level.

hey I'm no fan of Java but if thats what they are asking for....
Why not just write it in Java first? Get yourself a working solution for your homework assignment, and then, in your own spare time, write your C version that goes deep under the hood to learn how the low level details all work?

Porting from C to Java isn't going to be a trivial task, and you are just creating more work for yourself which is not generally a good idea when under the pressure of a deadline (eg the assignment due date).

As for Java's line ending problem across different systems, all that matters for the purpose of the assignment, is that it works on the university's lab computers that they will be grading/testing your program on. Issues like that don't matter until there is actually a need to run it across the different systems. If the problem actually does occur on the university's computers, then talk to your professor about how to work around that.
figuerres
figuerres
???
as for
"In a previous class when presented with 2 assignments in C (I was in heaven), I was flabbergasted at peers' inability to think in terms of simple concepts such as stack allocations, memory management and pointer arithmetic. If you were to quiz them now, you would likely find them to still be unable to think in terms of such basic concepts. I am afraid that writing code exclusively in Java (like they do) will cause me to lose my abilities in such concepts and I view programming in C/C++ as a method to prevent that from occurring.

In other words, what I am attempting to do is to prevent the onset of a geek form of Alzheimer Syndrome that I view multilingualism in Java to cause."

i can relate....   I know that I started with C and assembly and still today It serves me well to be able to think of how to do very "basic" stuff like push and pop a stack or watch the IP move as code executes...
and on one or two occasions i had to know that stuff to debug an app with VS -- some strange crash left me in native code and hex dumps.... but I was ablle to work out what happend and fix the bug....
I had to read some memory to see what was trashed... it had to do with pinvoke and a C style structure that .net had to parse.

it's good to spend some time in the guts of the machine....
I even have some bbooks at home that show how the 286 system boards were built, ic diagrams and gates and interupt lines
good stuff if you want to hack a custom board for something...
page 1 of 1
Comments: 8 | Views: 668
Microsoft Communities