Programming sockets in Java
Programming sockets in Java and writing simple SMTP client.
Programming sockets in Java
In this section we will answer the most frequently asked questions about programming sockets in Java. Then we will show some examples of how to write client and server applications.
Note: In this tutorial we will show how to program sockets in Java using the TCP/IP protocol only since it is more widely used than UDP/IP. Also: All the classes related to sockets are in the java.net package, so make sure to import that package when you program sockets.
How do I open a socket?
If you are programming a client, then you would open a socket like this:
Socket MyClient;
MyClient = new Socket("Machine name", PortNumber);
Where Machine name is the machine you are trying to open a connection to, and PortNumber is the port (a number) on which the server you are trying to connect to is running. When selecting a port number, you should note that port numbers between 0 and 1,023 are reserved for privileged users (that is, super user or root). These port numbers are reserved for standard services, such as email, FTP, and HTTP. When selecting a port number for your server, select one that is greater than 1,023!
In the example above, we didn't make use of exception handling, however, it is a good idea to handle exceptions. (From now on, all our code will handle exceptions!) The above can be written as:
Socket MyClient;
try {
MyClient = new Socket("Machine name", PortNumber);
}
catch (IOException e) {
System.out.println(e);
}
If you are programming a server, then this is how you open a socket:
ServerSocket MyService;
try {
MyServerice = new ServerSocket(PortNumber);
}
catch (IOException e) {
System.out.println(e);
}
When implementing a server you also need to create a socket object...
More Programming Languages
essays:
NPuzzle Problem In Java, Lisp & Prolog
... given application. Rather then running a program to obtain a solution, the user asks a question. When asked a question, the ... the most widely available language in the logic programming paradigm ...
A simple ad rotation program written in php, which uses a simple text file to revolve your ads. No need to get any databaseor CGI scripting.
A simple ad rotation program written in php, which uses a simple text file to revolve your ads. No need to get any databaseor CGI scripting. You canalso use this concept to revolve contents on your site.
Compares and Contrasts JavaScript and Java
... a client-side, scripting language the script is ran run directly from in the browser. The user retrieves a web page from the web server with the program already ...
A comprehensive look at the development of the computer programming language, focusing partially on C++.
... in programming. Its most common uses now days are in simulation environments, and in PC applications. The most common ... low-level access to IBM's scientific computing. FORTRAN calculated numbers well; however, it lacked a sufficient input and output directive ...
Analysis Of The C++ Programming Language
... a very scalable and robust tool to develop object oriented applications. Indeed, from my experience, C++ does indeed make writing good programs easier and more pleasant. References Bray, Mike. (2001) Carnegie Mellon ...
Java
... system best suited to their needs. And because Java technology programs can run on just about any type of computer and many devices, a user's applications and data are accessible from network computers on the factory ...
Programming Languages: How were they created, which language was their compilers created in, and the hierarchy followed in their creation.
... enables a programmer to program the system for a specific task. The language to provide the least level of abstraction is the assembly language. It has a very high resemblance to the binary instruction set. In fact, most of what is ...
Report on Fortran as a programming language. Kreitzberg, C & Shnediderman, B. (1982). FORTRAN Programming. New York: Harcourt.
... a programming language that would be: simple to learn, suitable for a wide variety of applications, machine ... author specifies it is a device that computes with digits (numbers). This is in some sense a misnomer, because digital computers ...