Sockets

Born at the University of California, Berkeley, in the 1980s, sockets first appeared in the Unix world as the Berkeley Sockets Interface, a programming device designed to help far-flung networked computers exchange information.

By the mid-1990s, Microsoft Corp. had created its own sockets variation, Windows Sockets (more widely known as WinSock), so Windows applications could talk across network links.

As software engineer Jim Frost explains in his Web treatise "Windows Sockets: A Quick and Dirty Primer," sockets are the computer programming world's equivalent to a telephone, where devices that have their own numerical addresses become the vehicles for two-way communication.

Programmers use the labels "client" and "server" to distinguish between the computer doing the calling and the one receiving the call. Computers with server sockets keep a communications port open, ready for any unscheduled, incoming call. Clients typically determine the port number of the desired server by finding it in a Domain Naming System database.

 
 
 More on Sockets
The Web is a ready resource for detailed information about building sockets on a variety of platforms. The following are some helpful sites:

  • Sockets: WinSock Development Information
    A guide for users of WinSock 1.1 and 2.0, including sample source code and links to other pages.

  • Lesson: All About Sockets
    A tutorial for Java developers.

  • Programming UNIX Sockets in C - FAQ
    A guide for C programmers interested in creating Unix sockets.

  • UNIX Socket FAQ
    A list of frequently asked questions devoted to Unix sockets

  • “Beej’s Guide to Network Programming: Using Internet Sockets,”
    by Brian “Beej” Jørgensen.

  • “Windows Sockets: A Quick and Dirty Primer,”
    by Jim Frost.
  • A little tap dance happens on the server side when the connection is made: Rather than continuing the conversation using the original port, the server switches the dialogue over to a related port to free up the main line in case additional clients try to get the attention of the server.

    With the rise of the Web, sockets continue to play important, if somewhat more behind-the-scenes, roles in keeping communications flowing over the Internet.

    "Every application you use that touches the Internet is written on top of sockets," Frost explains in his primer. For example, sockets are integral parts of Web site addresses, Web browsers and the security technology called Secure Sockets Layer.

    However, today's Web programmers are almost always shielded from having to manually create their own sockets. "Sockets aren't really necessary for Web-based application development," says Brian "Beej" Jorgensen, author of the Web publication "Beej's Guide to Network Programming: Using Internet Sockets."

    "Whether you're using Java, servlets, [Common Gateway Interface], PHP or whatever, you probably will never have to explicitly open a socket," he says. "The socket is still there, mind you, making the connection between the user and the Web application, but the nitty-gritty details of the socket tend to be hidden under higher layers so that most people don't have to deal with them. That being said, all Web browsers, [file transfer protocol] clients, e-mail clients and their respective servers use sockets to get their work done."

    Programmers get a break from the heavy-lifting aspects of socket creation, thanks to new generations of class libraries. Examples include the Microsoft Foundation Class Csocket and CsocketFile, says Frost. Unix programmers can use Socket++.

    Java programmers have their own set of sockets tools.

    Java.net.Socket is a widely used class for creating a platform-independent client socket, while java.net.ServerSocket can build a socket ready to receive requests from clients. With these, developers can create sockets quickly without getting bogged down in programming details.

    Frost says he wrote the sample code in the "Coding a Simple Socket" example at left in 15 seconds. How much time and effort would have been required to do that in 1995? "I could probably have done it in about 10 or 15 minutes, but not entirely out of my head," he explains. "There are too many little details to remember - like the names of the fields you have to populate in the address structure [and] the names of the constants you have to use to create a TCP socket."

     
    Coding a Simple Socket
    A simple threaded server framework sockets application in Java by Jim Frost:
     public void startServing()
    {
      Socket serverSocket = new
    ServerSocket(PORT_NUMBER);
      for (;;) {
       try {
        Socket clientSocket =serverSocket.accept();
        new Thread() {
         public void run() {
          doSomething(clientSocket);
         }
        }
       }
       catch (IOException e) {
        System.out.println("Failure: " + e.toString());
        System.exit(1);
       }
      }
      }

    Join the newsletter!

    Error: Please check your email address.

    More about GatewayMicrosoft

    Show Comments

    Market Place