Make a radio trolley program with Java Socket
Translated by caiyi0903 (Willpower), 2004.3.1
Introduction
Java is a powerful object-oriented development language that supports many features, such as C / S communications reached by Socket, WINDOW programming, based on Console programming, and database connection, image, and sound programming. Java is used to create some Applet applets embedded in the HTML page on an Internet-based network programming. Before you start the actual writing code, in order to make the concept more clear, you need to mention several important concepts. Broadcast Messenger is to create a server to receive and respond to network messages from the client. This is called Broadcasting, which means sending packets or messages to all clients. The server / client (C / S) framework is used here, because there is a computer-playing server role to respond to the client's message, all other computers play the role of the client, just just send a request to the server to perform them. Some tasks. Socket is a logical connection to each other of the computer. To create a socket, you need to provide a port number and a host IP address / hostname. Multi-threaded means a plurality of threads that can run on the same processor assigned simultaneously at the same time, it feels like only processes are running. So, through multi-threading technology, many clients can connect to the same port of the server. Threads are part of the process or program of resources, such as files, I / O, and so on, they can run independently. Java code explanation First, we create a server, create a Server and Client class, and IMPORT below: import java.io. *; Import java.Net. *; Import java.awt. *; Design for the Server class design Interface enables requests from clients to be displayed in a Window Form. A simple server window is designed as follows: Figure 1: A server window, display all the inputs, the client name, and the IP address log. We created a menu at the top of the window and created a TextArea and a Help dialog in the middle. We set the window's container layout manager as a flow layout (FlowLayout). This streamline manager places a component with rows. When a line is full, the components are automatically changed to the next line. There are other layout managers available in Java, such as Border Layout Manager, Grid Layout Manager, Card Layout Manager, and Gridbag Layout Manager.
The code is given below: public class ChatServer extends JFrame {public ChatServer (String title) // CONSTRUCTOR TO INITIALIZE THE // ChatServer CLASS {output = new TextArea (15,40); // output IS A TextArea COMPONENT // OF THE ChatServer ClassoutPut.setedITable (false); Output.SetFont (f); Output.setForeground (color.blue); settitle (title); // to set the title of the client windowsetjmenubar; // to Initialize the menu bar on THE WINDOWJMENU FileMenu = New Jmenu ("File"); JMenu Colormenu = New Jmenu ("Color"); JMenu HelpMenu = New Jmenu ("Help"); // Main Menu Shortcuts: FileMenu.SetmNemonic ('f'); Colormenu .SETMNEMONIC ('c'); HelpMenu.SetmNemonic ('h'); // About Dialog Init: AboutItem = New Jmenuitem ("About"); // AboutItem.com This; HelpMenu.Add (AboutInu.Add (AboutItem AddMenuItem (HelpMenu, AboutAction = New AboutAction ("About")); // Initialize Menu Items: MenuBar.Add (FileMenu); MenuBar.Add (HelpMenu); enableEvents (AWTEvent.WINDOW_EVENT_MASK); class AboutAction extends AbstractAction // CREATES AN ABSTRACT // INTERNAL CLASS FOR // About {JOptionPane opt; String name; public AboutAction (String Name) {this.name = Name;} // About menu event : public void actionPerformed (ActionEvent ae) {// if (ae.getSource () == aboutAction) {JOptionPane.showMessageDialog (opt, "ChitChat_Broadcast_Messenger / nCopyright Fatima_Ahmed", "About_ChitChat_Broadcast_Messenger", J OptionPane.INFORMATION_MESSAGE);
}}} Figure 2: ABOUT dialog public static void main (String args []) throws IOException {ChatServer ServerWindow = new ChatServer ( "ChitChat Broadcast Messenger: Server Window"); // CREATES AN OBJECT OF SERVERToolkit theKit = ServerWindow.getToolkit (); // TO CREATE AN OBJECT // OF ToolKitDimension wndSize = theKit.getScreenSize (); ServerWindow.setBounds (wndSize.width / 4, wndSize.height / 4, wndSize.width / 2, wndSize.height / 2); ServerWindow.setVisible (true);. ServerWindow.getContentPane () add ( "North", output);. // TO ADD THE TextArea (output) AT THE NORTH OF THE WINDOWServerWindow.getContentPane () setLayout (new FlowLayout (FlowLayout.CENTER ))); // to set the layout as centrally flowserverWindow.Pack (); // to pack the server window with Above // Initialize Components if (args.length! = 1) throw new iLlegaAraGumeTexce Ption ("Syntax: Chat Server
Handler.Start (); // the Broadcasting of Messages IS // Started by Start () Method Output.Append ("/ n Accepted from" Client.getinetaddress () "}} Socket is through another A class "chathandler" is created, which is included in the Demo Project file (the translator's note: downloaded). Now, we design a Client class: Figure 3: Client Letter window Ask each connection initialized user login name Figure 4: A client window, contains some fonts, color pick boxes, and a menu to control the window. Import the following files in the Client class. We have created another class "SketchFrame", which is used to define the Interface of some client windows. We describe some of the basic features of the Socket class in Java and implements START (), Run (), and STOP () methods on the client thread.
This class has the following imported files: import java.io. *; Import java.Net. *; Import java.awt. *; Import java.awt.Image. *; Import java.awt.event. *; Import javax. swing *;. import javax.swing.event *;. public class ChatClient implements Runnable, WindowListener, ActionListener, ListSelectionListener {protected String host; protected int port; public TextArea output; protected TextField input; String yourname; SketchFrame window; public ChatClient ( String host, int port, SketchFrame window) {// CONSTRUCTOR INITIALIZING THE ChatClient CLASS this.host = host; // host AND port WILL BE USED TO OPEN THE // SOCKET this.port = port; this.yourname = JOptionPane.showInputDialog ("Enter login name:"); // to create an input dialog box window.setsize (100,100); // to set the size of the client // window window.getContentPane (). Add (output, borderlayout.center) ; // to add textarea (output) at the center of the window window.getContentPane (). Add (Input t, BorderLayout.SOUTH); // TO ADD THE Textbox (input) AT THE BOTTOM (SOUTH) protected DataInputStream dataIn; protected DataOutputStream dataOut; protected Thread listener; public synchronized void start () throws IOException {// THREAD SYNCHRONIZATION METHOD FOR STARTING BROADCAST if (listener == null) {Socket socket = new Socket (host, port); // TO INITIALIZE // THE SOCKET try {dataIn = new DataInputStream (new BufferedInputStream (socket.getInputStream ())); dataOut = new DataOutputStream (New BufferedOutputStream (socket.getputstream ()); DataOut.writeutf (YourName "