views:

349

answers:

9

I will have a cloud computing class this semester, and one of the requirements about the course to know about socket programming.

First thing that I came up in my mind for this question was Java. I know Java has a strong socket programming library; but I anyway want to ask your opinion. What do you guys think?

+2  A: 

I taught myself socket programming in C. There are plenty of books and articles online about the subject.

Really, the language is fairly irrelevent. The key is to come up with an idea you can really get behind and implement it. I chose to write a standalone IRC file serve that could run as a service in Linux. It was a lot of fun and something I really wanted, so it was a great learning experience. As an unfortune aside, the college I entered blocked IRC traffic and I couldn't continue. How ironic!

Stuart Branham
A: 

If you want the highest possible abstraction I suggest Tcl. Event-based programming with sockets is trivial using Tcl's fileevent model. If you want to know the nitty-gritty of how sockets work at the lowest level, do it in C.

Here's a good resource on how to do socket programming in Tcl: http://www.beedub.com/book/2nd/socket.doc.html

Bryan Oakley
+5  A: 

For learning socket programming, it's hard to not recommend C. You really get into the nitty gritty with it.

For doing actual work, I'd pick a more modern language with a much better socket abstraction layer (C#, Java, Python, etc).

Alan
This was going to me by answer too. Every other language pretty much builds a lot of helper wrapper layers around the C socket interface. If you actually need to learn that interface, best drop straight down to it.
T.E.D.
+1  A: 

Depends on the OS, for a start.

Beej's Guide to Network Programming using Internet Sockets is probably the definitive guide to sockets, networks and programming for them on the internet.

Java and C# both have really good solid Socket classes and methods, but it's worth noting that a lot of the examples on the net that worked in XP don't work as well in Vista and Win 7 as Microsoft has really clamped down on Raw sockets among other things. They're still possible, it just requires some further preconditions these days.

Good luck, it's a huge but interesting topic.

Mark Mayo
This! I couldn't remember the guide I used, but this is it. Highly recommended.
Stuart Branham
heh-heh, Beej
Alan
A: 

Python socket module is an easy to use wrapper around the BSD socket library.

The Python interface is a straightforward transliteration of the Unix system call and library interface for sockets to Python’s object-oriented style: the socket() function returns a socket object whose methods implement the various socket system calls. Parameter types are somewhat higher-level than in the C interface: as with read() and write() operations on Python files, buffer allocation on receive operations is automatic, and buffer length is implicit on send operations.

gimel
+2  A: 

As always, python makes it easy:

# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()

# Echo client program
import socket

HOST = 'daring.cwi.nl'    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
voyager
A: 

Use the language that you are most comfortable with (that has socket support). But as said before, the concepts are the most important, not the language.

But if you are comfortable with C, use it. And if you do use C, I would suggest getting comfortable with select()/poll() as they will allow you to handle multiple connections without dealing with threads.

miko
A: 

Sockets are a simple concept, which is why they are so useful. Being such a simple concept, I doubt that you can use it as a differentiator of languages. It is like asking "Which language is better for file handling?" There are defiantly languages that make working with them easier, but when programming with them, you almost always write wrappers and interfaces internally and use those instead of littering your code with direct socket calls.

But for learning, use Java. I don't like Java, but you eventually need to get hired, so Java experience won't be a bad thing.

William Crim
A: 

I just want to mention Erlang, surprised nobody has.

Also if you chose python you might want to look into the Twisted Framework (it's event-driven)

CodeJustin.com
Just curious, why Erlang? is it personal preference or socket programming in Erlang is somewhat different from socket programming in other high level languages? cheers
Tumas