Use Java socket to make broadcast Messenger program

Source: Internet
Author: User
Tags socket port number

Brief introduction

Java is a powerful object-oriented development language that supports many functions, such as C/s communication via socket programming, windows-based programming, console based programming, database connectivity, image and sound programming. Java is used in internet-based network programming, to create some applets embedded in HTML pages to achieve the applet.

Before you begin to actually write code, there are several important concepts that need to be mentioned to make the concept clearer. The broadcast Messenger (broadcast Messenger) is to create a server that is responsible for receiving and responding to network messages from the client. This is called broadcast (broadcasting), which means sending packets or messages to all clients.

This is a server/client (c/s) framework, because one computer plays the role of a server to respond to client messages, and all other computers play the role of a client, simply sending requests to the server to perform some of their tasks. Sockets are a logical connection that connects computers to each other. To create a socket, you need to provide a port number and a host IP address/host name.

Multithreading means that multiple threads of a process can run simultaneously on the same processor that is assigned to them, and it feels as if only the process is running. So, with multithreading technology, many clients can connect to the same port on the server. Threads are part of a process or program that occupies resources, such as files, I/O, and so on, and they can run independently.

Java Code Explanation

First, we create a server and a client class, and import the following files:

import java.io.*;
import java.net.*;
import java.awt.*;

Design a interface for the server class so that requests arriving from the client can be displayed in a window form. A simple server window design is as follows:

Figure 1: A server window that displays a log of all input and output, client names, and IP addresses.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.