First build two Java Project project, a write client, a write server side program, the program roughly for a server side set up listening to a port, more than one client can connect, the implementation of the following:
1. Server-side
importjava.io.*;
importjava.net.*;
importjava.util.*;
Publicclass Simplechatserver {
arraylist<printwriter>clientoutputstreams;
public static void Main (String[]args) {
New Simplechatserver (). Go ();
}
public class ClientHandler implementsrunnable{
BufferedReader reader;
Socket sock;
Public ClientHandler (Socketclientsocket) {
try{
sock = Clientsocket;
InputStreamReader isreader =new InputStreamReader (Sock.getinputstream ());
Reader = Newbufferedreader (Isreader);
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
public void Run () {
String message;
try{
while (Message =reader.readline ())! = null) {
System.out.println ("read" + message);
Telleveryone (message);
}
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
}
public void Telleveryone (stringmessage) {
Iterator<printwriter> it =clientoutputstreams.iterator ();
while (It.hasnext ()) {
try{
PrintWriter writer = (PrintWriter) it.next ();
WRITER.PRINTLN (message);
Writer.flush ();
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
}
public void Go () {
Clientoutputstreams = newarraylist<printwriter> ();
try{
ServerSocket Serversock = Newserversocket (2310);
while (true) {
Socket clientsocket =serversock.accept ();
PrintWriter writer = Newprintwriter (Clientsocket.getoutputstream ());
Clientoutputstreams.add (writer);
Thread t = new Thread (Newclienthandler (clientsocket));
T.start ();
System.out.println ("Got Aconnection");
}
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
}
2. Client Side
importjava.io.*;
importjava.net.*;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
Importjavax.swing.JOptionPane;
Publicclass Simplechatclient {
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
Static String ClientName;
public static void Main (string[] args) {
Simplechatclient client = Newsimplechatclient ();
ClientName = Joptionpane.showinputdialog ("Pleaseinput the client Name:");
Client.go ();
}
public void Go () {
Build GUI
JFrame frame = new JFrame (clientName + "s Chat Client");
JPanel Mainpanel = new JPanel ();
incoming = new JTextArea (15,20);
Incoming.setlinewrap (TRUE);
Incoming.setwrapstyleword (TRUE);
Incoming.seteditable (FALSE);
JScrollPane Qscroller = Newjscrollpane (incoming);
Qscroller.setverticalscrollbarpolicy (scrollpaneconstants.vertical_scrollbar_always);
Qscroller.sethorizontalscrollbarpolicy (Scrollpaneconstants.horizontal_scrollbar_never);
outgoing = new JTextField (20);
JButton Sendbutton = Newjbutton ("Send");
Sendbutton.addactionlistener (Newsendbuttonlistener ());
Mainpanel.add (Qscroller);
Mainpanel.add (outgoing);
Mainpanel.add (Sendbutton);
Setupnetworking ();
Thread readerthread = new Thread (Newincomingreader ());
Readerthread.start ();
Frame.getcontentpane (). Add (Borderlayout.center, Mainpanel);
Frame.setsize (400,500);
Frame.setvisible (TRUE);
}//Close Go
private void setupnetworking () {
try {
Sock = Newsocket ("127.0.0.1", 2310);
InputStreamReader streamReader =new InputStreamReader (Sock.getinputstream ());
Reader = Newbufferedreader (StreamReader);
writer = Newprintwriter (Sock.getoutputstream ());
System.out.println ("networkingestablished");
} catch (IOException ex) {
Ex.printstacktrace ();
}
}//Close setupnetworking
public class Sendbuttonlistener Implementsactionlistener {
public void actionperformed (Actioneventev) {
try {
Writer.println (clientname+ "says:\n" +outgoing.gettext ());
Writer.flush ();
} catch (Exception ex) {
Ex.printstacktrace ();
}
Outgoing.settext ("");
Outgoing.requestfocus ();
}
}//close Sendbuttonlistener Inner class
public class Incomingreader Implementsrunnable {
public void Run () {
String message = NULL;
try {
while (Message =reader.readline ())! = null) {
System.out.println ("read" + message);
Incoming.append (message + "\ n");
}//Close while
} catch (Exception ex) {ex.printstacktrace ();}
}//Close run
}//Close inner class
}
The following is an example of server-side generation EXE:
1. Export the jar file
Select your project---then right-click on the pop-up options list---Select Export---Java---jarfile---next---browser (choose the storage path for the jar)---next---next (whichever is too much to proceed to the next step)--- Browser (choose your program's main class--OK)---Finish
2. Installing exe4j
Use exe4j this software to convert jar files to exe files, exe4j:http://www.ej-technologies.com/download/exe4j/files.html.
3.EXE4J after installation is complete
1. Click Next directly after running.
2. Select the second "JAR in EXE mode" and click Next.
3. In the general---the short name of your application box, enter the name of your application (whatever you want), enter the save path after the EXE is generated in the directories---outputdirectory box. Then next.
4. In the Excutable Name box, enter the name of the EXE that will be generated, and "icon File" is the small icon that the application displays, not the same. Continue next.
5. You must first configure the Class-path, click the + sign to add the file, that is, the jar file, if useful to other resources need to add resources together.
6.General---Main class is selected.
7.Java Version---Minimum version (the low version must be filled in), Maximumversion (the higher version can be empty), if all are filled in then the value of the higher version must be greater than the lower version of the value.
8. Tick enable JREs with a beta version number as well as the only allow JDKs Andno JREs. Next. You can finish it in this step. This will convert the jar to an EXE file. You can click Here to Start the application button to test the generated file.
The client side generates EXE steps as above.
Client/server program Generation EXE in eclipse