Sockets - Java Programming Sockets in Java Manuel Oriol 1...

This preview shows page 1 - 3 out of 8 pages.

Java Programming: Sockets in Java Manuel Oriol May 10, 2007 1 Introduction Network programming is probably one of the features that is most used in the current world. As soon as people want to send or receive data over a network in a program, you need to use sockets. Java is a language born after the advent of the Internet and hence has very good integration of sockets in the standard API. Sockets-related classes are located in the java.net package. In this chapter, we explain how sockets work and show examples that make use of sockets. Section 2 gives some background on networking. Section 3 shows how to use and build TCP sockets. Section 4 shows how to use and build UDP sockets. 2 Networking Background The traditional approach of networking is the client-server approach. In this model there is one server talks to arbitrarily many clients to answer their re- quests. There are two fundamental ways to communicate over a network: Connected Mode: The connected mode is the most natural way of handling network communications. It correspond closely to how people are used to communicate say over a phone line. In this mode the two parties share a (virtual) connection that they send data over. The connection first needs to be opened, then data can be sent and eventually the connection is closed. Data sent in such a way has a low chance of getting lost and will always arrive in the order it was sent. Both the traditional telephone network and the TCP for the Internet work in this way. Disconnected Mode: The disconnected mode is a way of communication in which no connection needs to be established between the parties communi- cating. In this mode information is broadcast. Since there is no notion of connection it cannot be detected if information has not been picked up by a receiver or information might arrive out of order. Radio and television as well as the UDP for the Internet work in this mode. Traditionally, the code required to make use of sockets has been cumbersome to write and was quite low level. The following gives an example of how a socket is created and bound in C 1 : 1 Code borrowed from 1
Image of page 1

Subscribe to view the full document.

int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); }
Image of page 2
Image of page 3

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern