Network programming...(Sockets)

Status
Not open for further replies.
Hello All...

I have a quick question about socket programming on a Linux box. I know it is more efficient to use C as opposed to Java as the programming language. I don't know if I have a brain freeze or what...but the question is why? :p

This is for a report and I have to justify everything I do...for me its just intuition...network=C

From what I can recall: the advantages are in efficiency and speed, which are the most important aspects of network programming. Again why is C faster/more efficient? Is it because it is more low-level than Java?

I apologise for the stupid sounding questions...but, like I said, Brain Freeze :eek: :D

Thanks a million for you help.

Regards,
Hellfire

:D Constantly working on "it" :D

[This message was edited by hellfire on January 09, 2002 at 08:57.]
 
D

Deleted member 5324

Guest
C is lower level than almost every other language out there. Compiler technology being what it is, that also makes it more efficient at almost everything than every other (non-assembler) language out there. Operating systems (especially UNIX/POSIX) being what they are, networking programming in C is more efficient than every other language (because all the other languages have to go through C to get to the network.)

That said, C is oodles more efficient than Java :)

Jeremy
 

Sans

Wise, Aged Ars Veteran
144
C may be faster than Java but I don't know about more efficeint. I'd much rather write all my networking code in Java since:
1) Java is more than fast enough for most network services you would write. Obvious exceptions would be games, real time apps, etc.
2) In my opinion, it's much quicker to write socket code in java so it may be more efficient from a development standpoint (though this may depend on how comfortable you are doing sockets in C...for me *shudder*)
3) NO BUFFER OVERFLOWS (and other possible exploits) meaning, even if by default, more secure code. No need to be sending out 'Security Advisories' every week and then spending more time fixing your code.

.Sans
 

limbs

Ars Scholae Palatinae
1,058
Well, as has been pointed out, C is lower level and a lot more efficient with system resources. Due to todays compiler technology C code tends to produce very fast compiled machine code. Java is, for all intents and purposes, intepreted.

Granted on today's modern machines yuo won't notice much speed difference between the two unless you start doing some heavy work. It therefore comes down to your personal preference in programming languages - personally I prefer C.

3) NO BUFFER OVERFLOWS (and other possible exploits) meaning, even if by default, more secure code. No need to be sending out 'Security Advisories' every week and then spending more time fixing your code.
Errr - if you ever write socket code using static buffers you deserve exactly what you get, which is code that will likely crash long before anyrthing useful happens with it. If you're sensible you should never ever get buffer overflow exploits.
 
Also keep in mind that programmers are much more expensive than hardware, most of the time. The only exception is when the program is used by lots and lots of people.

It depends what you mean by "network programming". If you mean simple tasks that requires high performance, C is the answer (ex.: http server, IRC server, etc). If the tasks are more complex, and you have less users, Java might be a better choice. It also depends how much of the application is really going to be "network code". Is it very network-oriented? Or is the network just a simple transport for data? Because an application has to communicate over a network doesn't mean that the network aspect should govern what you use to develop it.
 
I highly disagree. The differences between the two are minimal. The almost non-existant learning curve for Java, in my opinion, outways any single digit increase in speed.

I have just finished reading UNIX Network Programming (by Stevens) and I'd have to disagree. Stevens details the magnitude of speed differences between various implementations of the same servers. Implementations such as pre-threaded servers individually mutex locking on accept() appear to be the way to go for performace in the C world (Based on the echo server Stevens develops).

I also have Core Java (volume 1 & 2) and have no where in this book did I come close to finding the same in depth discussion of how to create well performing large-scale Java networking programs. The answer for Java seems to be spawn a new thread for each connection (if you want to be complex) and go from there. While this is good, I don't think it offers the breadth of flexibility (and efficiency) as well-written (note, I DO have to justify myself with well written because Java is much easier to get written well quicker) C code.

In terms of modern computers and networking, however, discussions about speed and need tend to become blurred. For example, I implemented the beginnings of a distributed FTP implementation last year in Java and it was by no means slow. But yet, if I were to implement it this year in C after reading UNP (by Stevens), I'm sure I could make the networking part of the system an order of magnitude faster...
 

slok

Ars Scholae Palatinae
619
you might want to expore socket programming with Tcl... here..
http://groups.google.com/groups?threadm=e6jZ7.281%245Y4.6325%40news.cpqcorp.net&rnum=1&prev=/groups%3Fselm%3De6jZ7.281%25245Y4.6325%2540news.cpqcorp.net

and for something network oriented.. I would also look into using
something like REBOL which has build-in support for standard protocol...
 
If you are going to develop network apps I think a good idea is to first prototype in a language such as Java and then optimise in C/C++.

Java is in my experience very nice to write small test for networking. It's both simple to do and you don't have to fall into a lot of small traps. (Memory managment and all that.) So you can consentrate on the things you want to test.

Then when you know what you want you do it in C.

Hey it's a nice development in theory at least, perhaps not so if you have to get it done yesterday. ;-) (But don't dismiss it just because you have to do some work twice. Doing it right two time can be faster than doing it wrong once.)

Cobalt: That programs look kinda' like somthing I've been thinking about doing. Do you have any document on what it is you have done? (Or should I just go read the source? ;-)
 

wb

Ars Legatus Legionis
25,208
Subscriptor
It depends what you mean by "network programming". If you mean simple tasks that requires high performance, C is the answer (ex.: http server, IRC server, etc).

I have vast experience (8+ years) developing commercial clients and servers using both Java and C on UNIX and Windows. My company sells a high-performance server product written entirely in Java. We have clients for this server written in Java, C, C++, and VB. With the exception of the VB code, everything has been written to run on both UNIX and Windows.

Without a doubt, it is at least 100 times easier to implement and maintain stable, high-performance networking code using Java than C++. We still have occasional problems with some of the C++ implementations that have now been in use for about 5 years, and are continually tweaking the code to make it work better (sometimes we do a complete rewrite as newer operating system deficiencies come to light). I honestly don't know how the Java native code has been implemented, but they did an outstanding job on all of the platforms I've used (Windows all versions except XP, HPUX, IRIX, Linux, AIX, etc). I'm more than willing to sacrifice a small decrease in overall performance for the total ease with which we've implemented all of our Java networking code.
 
D

Deleted member 5324

Guest
Originally posted by Jackass JoeJoe:
I highly disagree. The differences between the two are minimal. The almost non-existant learning curve for Java, in my opinion, outways any single digit increase in speed.

The differences are rather large when it comes to network programming. Up through 1.2, Java doesn't even provide non-blocking sockets.

And the speed difference is far more than just a single digit :)

Jeremy
 
D

Deleted member 5324

Guest
Originally posted by limbs:
Errr - if you ever write socket code using static buffers you deserve exactly what you get, which is code that will likely crash long before anyrthing useful happens with it. If you're sensible you should never ever get buffer overflow exploits.

If you're sensible and perfect. The point is that a Java program won't ever have a security flaw due to a buffer overflow. The same can't be said of C programs.

Even the best programmers make mistakes. In Java, mistakes that overflow a buffer won't be a security risk (other than DoS.) In C, that's not true.

Jeremy
 
D

Deleted member 5324

Guest
Originally posted by Cobalt:
I have just finished reading UNIX Network Programming (by Stevens) and I'd have to disagree. Stevens details the magnitude of speed differences between various implementations of the same servers. Implementations such as pre-threaded servers individually mutex locking on accept() appear to be the way to go for performace in the C world (Based on the echo server Stevens develops).

I've also read UNP, but don't remember the exact details of the echo server. Either way, if you're going for performance in any network application, you'll find that using select(2) and nonblocking sockets is always faster on a single processor than a multithreaded model. On multiple processors, you simple use as many processes as there are processors to listen to the socket.

Jeremy
 

ssmith

Ars Scholae Palatinae
928
Don't forget that if you are writing a server, then the thread library in Java is easily accessible and powerful.
The answer for Java seems to be spawn a new thread for each connection (if you want to be complex) and go from there.
A simplistic solution may do this (and it will scale, depending on the VM, OS and memory available to between ~2000 and ~10000 simultaneous connections).
A more efficient solution (particularly if your clients are not hammering away) is to use one thread to service n connections. This essentially requires maintaining a thread pool to service IO tasks when they arrive. Concurrent Programming in Java by Doug Lea can introduce you to these type of design patterns, but be warned, it is a serious book.
The JDK1.4 does include non blocking I/O, but it is still in beta.
The essential question is, how important is performance to you, when balanced against managing the OS from a low level language. IMO, a simple Java solution will be easier to develop, and hence be more robust and simpler to debug.
 
Thanks a lot for all the :cool: excellent :cool: info guys and gals. Just a couple of points on the sort of program that I am writing...it is not something spectacular that will handle a few thousand connections or anything along those lines.. This is for my design project / thesis at my University. So the number of connections it will be able to handle in the single digits :eek:

As far as programming complexity is concerned, I have not been exposed to network programming in Java, it has been a totally C experience, and I have written quite a few programs (at this level) such as http proxy, your basic client server programs, a chat program(just an extension) and so on... I hope I will be doing what you all suggested when (if) I graduate :p this summer, but this was essentially a slanted post meaning that I wanted reasons to say C is better than Java in this aspect because....
1...
2...etc
I dont have anything against Java, I like it quite a bit, but like I stated in my original post, I have to account for every little decision I make (I guess choice of a programming language is far from a "little" decision, but you get the point...right? ;) )

And once again, Ars fails to disappoint... :cool:

Thanks very much guys...keep em coming

Regards,
Hellfire

:D Constantly working on "it" :D
 
Either way, if you're going for performance in any network application, you'll find that using select(2) and nonblocking sockets is always faster on a single processor than a multithreaded model.

Stevens also wrote that for the effort needed to make a properly functioning non-blocking implementation of a system, it was many times more complex and often not worth the effort over a threaded blocking system. But yes, where speed over speed & ease is concerned, nonblocking I/O is the way to go. I believe Stevens pointed it out to be ~25% faster or so (of course his implementation of nonblocking I/O sockets ran at 150 lines for readline() whereas his threaded one was something like 30)...

The problem here is that there is no definate answer. It depends on what you know, and what the rest of your project needs. If you need lots of other Java-centric power, then it is silly to do your networking in C just to get the speed. But if the most you need is basic STL functionality, and the ability to hammer out code (that, or you have previous experience with BSD sockets in C), then C/C++ would be the way to go. To an extent, one could probably justify using either by saying it was what they knew and were comfortable implementing. Don't know if that reasoning works in thesises but hey, if the implementation works it's what matters to the people who will be paying you in the future.

Hast: All the documentation that I have worked up for FTreeP is there on the link I posted earlier. It was for a Network Programming class, and the requirements for documentation were slim. I can tell you how it works, and what everything does if you like. Gimme an email to cobalt@f2s.com if you're interested.
 
D

Deleted member 5324

Guest
Originally posted by Cobalt:
Stevens also wrote that for the effort needed to make a properly functioning non-blocking implementation of a system, it was many times more complex and often not worth the effort over a threaded blocking system.

But that cost is easily amortized by writing an asynchronous framework that facilitates code reuse (see http://www.nightmare.com/medusa/ for an example.)

I do agree that a thread-per-connection model is simpler to program at first, though.

But yes, where speed over speed & ease is concerned, nonblocking I/O is the way to go. I believe Stevens pointed it out to be ~25% faster or so

I'd be surprised if it's that low -- from looking here, it appears that asynchronous webservers are usually at least twice as fast as threaded or forking servers.

Jeremy
 
Totally agree jem...

Just for some quote fodder...

"We can time our nonblocking version using the same 2000-line file and the same server (a 175-ms RTT from the client) as in Section 6.7. The clock time is now 6.9 seconds, compared to 12.3 seconds for the version in 6.7 [a plain select() system w/ no threads]. Therefore nonblocking I/O reduces the overall time for this example [an echo server] that sends a file to the server.
UNIX Network Programming, Stevens, p. 407

He then states that the nonblocking I/O length of source and complexity are not trivial, and that it would be a better idea to use a threaded model. Stevens, in 23.4 states that a threaded version of the same client "took 8.5 seconds, slightly faster than the version using fork (which we expect) but slower than the nonblocking I/O version." For some numbers, then, the threaded version is ~23.2% slower than the nonblocking I/O system.

If you're writing a solid framework, then yes, you would be best to use nonblocking I/O for highest performance, but not necessarily easy to write code...

Both would still be faster than Java I believe.

EDIT: Of course the numbers I cite do not reflect any numbers that do not pertain to echo servers and clients... Web servers, being more real-world, are probably more illustrative of the benefits and deficiency of any given implementation... :)

[This message was edited by Cobalt on January 10, 2002 at 16:57.]
 

limbs

Ars Scholae Palatinae
1,058
If you're sensible and perfect. The point is that a Java program won't ever have a security flaw due to a buffer overflow. The same can't be said of C programs.

Even the best programmers make mistakes. In Java, mistakes that overflow a buffer won't be a security risk (other than DoS.) In C, that's not true.
Sort of. By static buffers I meant something along the lines of ....
char buffer[256];

which when used in anything that dynamically reads data is a recipe for disaster. Whereas if you use
char *buffer;
You at least are forced to deal with the size of data coming in.

Also, (unless I've been getting this wrong for years!!!) buffer overflow exploits can only happen of memory allocated of the stack (i.e. buffer[256]) whereas *buffer memory is allocated of the heap which grows down in memory and therefore you can't keep running up the address space.
 
D

Deleted member 5324

Guest
Originally posted by limbs:
Sort of. By static buffers I meant something along the lines of ....
char buffer[256];

which when used in anything that dynamically reads data is a recipe for disaster. Whereas if you use
char *buffer;
You at least are forced to deal with the size of data coming in.

You don't seem to know the C networking APIs. They read into a buffer -- they don't return a buffer. Thus, you have to have an already-allocated-fixed-size buffer in which to read the data, and that's where mistakes can very easily occur.

A plain char * would obviously be very much worse for this than an stack or heap allocated buffer :)

Also, (unless I've been getting this wrong for years!!!) buffer overflow exploits can only happen of memory allocated of the stack (i.e. buffer[256]) whereas *buffer memory is allocated of the heap which grows down in memory and therefore you can't keep running up the address space.


Buffer overflows happen more easily on the stack than on the heap, but buffer overflows of heap-allocated are]http://shinra.soldierx.com:8080/~blake/public/documents/security/w00w00-heap-overflows.txt]are definitely possible[/url].

Jeremy
 
Status
Not open for further replies.