java.net Drills

Source: Internet
Author: User
Tags flush gettext socket stub window client
The server side of a very simple LAN chat tool

/*



* Created on 2005-3-18
*
* TODO to change the template of this generated file go
* Window-preferences-java-code Style-code Templates
*/
Package netchat;

Import javax.swing.*;
Import java.io.*;
Import java.net.*;
Import java.awt.*;
Import java.awt.event.*;


/**
* @author Appletian
*
* TODO to change the template of this generated type comment go
* Window-preferences-java-code Style-code Templates
*/
public Class Server extends JFrame {
Private JTextField MessageBox;
Private JTextArea Infbox;
private ObjectInputStream input;
Private ObjectOutputStream output;
Private ServerSocket server;
Private Socket ServerSocket;

Public Server () {
Super ("Server");
Container C=getcontentpane ();

Messagebox=new JTextField ("");
Messagebox.setenabled (FALSE);

Messagebox.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
SendData (E.getactioncommand ());
}

});
C.add (Messagebox,borderlayout.north);

Infbox=new JTextArea ();
Infbox.setfont (New Font ("Serif", font.bold,12));
C.add (New JScrollPane (Infbox), borderlayout.center);

SetSize (500,400);
SetVisible (TRUE);

}


/**
* @param Actioncommand
*/
protected void SendData (String actioncommand) {
try{
Output.writeobject (Actioncommand);
Output.flush ();
Infbox.append ("\nsend<<" +actioncommand+ ">> to client...\n");
}
catch (IOException e) {
Infbox.append ("\nexceptionioinsenddatafunction ...");
}
}
public void Runserver () {
try{
Server=new ServerSocket (5000,100);
while (true) {
Waitforconnection ();
Getstreams ();
Processconnection ();
CloseConnection ();
}
}
catch (Eofexception e) {
Infbox.append ("\nerror in Runserverfuction");
}
catch (IOException e) {
E.printstacktrace ();
}

}

/**
*
*/
private void CloseConnection () throws IOException {
Infbox.append ("\nclose connection ...");
Messagebox.setenabled (FALSE);
Output.close ();
Input.close ();
Serversocket.close ();

}


/**
*
*/
private void Processconnection () throws IOException {
String mg= "\nconnection succeful";
Messagebox.setenabled (TRUE);
Output.writeobject (MG);
Output.flush ();

do{
try{
Mg= (String) input.readobject ();
Infbox.append ("\nsend to Client:" +MG);
Infbox.setcaretposition (Infbox.gettext (). Length ());
}
catch (ClassNotFoundException e) {
Infbox.append ("\ n unkown class received ...");

}
}while (mg!= "End");




}


/**
*
*/
private void Getstreams () throws IOException {
Output=new ObjectOutputStream (Serversocket.getoutputstream ());
Output.flush ();

Input=new ObjectInputStream (Serversocket.getinputstream ());

Infbox.append ("\nget Streams ...");


}


/**
*
*/
private void Waitforconnection () throws IOException {
Infbox.append ("Wait for a Client ...");
Serversocket=server.accept ();
Infbox.append ("Connected by" +serversocket.getinetaddress (). GetHostName ());


}


public static void Main (string[] args) {
Server my=new server ();
My.setdefaultcloseoperation (Jframe.exit_on_close);
My.runserver ();
}
}




Client



/*
* Created on 2005-3-18
*
* TODO to change the template of this generated file go
* Window-preferences-java-code Style-code Templates
*/
Package netchat;
Import javax.swing.*;

Import java.io.*;
Import java.net.*;
Import java.awt.*;
Import java.awt.event.*;

/**
* @author Appletian
*
* TODO to change the template of this generated type comment go
* Window-preferences-java-code Style-code Templates
*/
public class Client extends JFrame {

Private JTextField MessageBox;
Private JTextArea Infbox;
private ObjectInputStream input;
Private ObjectOutputStream output;
Private String mg= "";
Private String Chatserver;
Private Socket client;
Public client (String host) {
Super ("Client ...");
Chatserver=host;
Container C=getcontentpane ();

Messagebox=new JTextField ();
Messagebox.setenabled (FALSE);
Messagebox.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
SendData (E.getactioncommand ());
}


});
C.add (Messagebox,borderlayout.north);
Infbox=new JTextArea ();
C.add (Infbox,borderlayout.center);

SetSize (500,400);
SetVisible (TRUE);





}


/**
* @param Actioncommand
*/
protected void SendData (String actioncommand) {
try{
Mg=messagebox.gettext ();
Output.writeobject (MG);
Output.flush ();
Infbox.append ("\nsend to Server:" +MG);
}catch (IOException e) {
Infbox.append ("\nioexception in SendData.");
}

}
public void Runclient () {
try{

Connecttoserver ();
Getstreams ();
Processconnection ();
CloseConnection ();
}catch (IOException e) {
Infbox.append (E.tostring ());
}

}

/**
*
*/
private void CloseConnection () throws IOException {
TODO auto-generated Method Stub
Infbox.append ("\nclose ...");
Output.close ();
Input.close ();
Client.close ();

}


/**
*
*/
private void Processconnection () throws ioexception{
TODO auto-generated Method Stub
Messagebox.setenabled (TRUE);
do{
try{
Mg= (String) input.readobject ();
Infbox.append ("\nfrom server:" +MG);
Infbox.setcaretposition (Infbox.gettext (). Length ());
}catch (ClassNotFoundException e) {
Infbox.append ("classnotfound...\n");
}
}while (mg!= "End");
}


/**
*
*/
private void Getstreams () throws IOException {
TODO auto-generated Method Stub
Output=new ObjectOutputStream (Client.getoutputstream ());
Output.flush ();
Input=new ObjectInputStream (Client.getinputstream ());
Infbox.append ("Get" IO Stream. \ n ");

}


/**
*
*/
private void Connecttoserver () throws IOException {
TODO auto-generated Method Stub
Infbox.append ("Try connecting to server...\n");
Client=new Socket (Inetaddress.getbyname (chatserver), 5000);
Infbox.append ("Connect to Server:" +client.getinetaddress (). GetHostName () + "\ n");

}


public static void Main (string[] args) {
Client p;
if (args.length==0)
P=new Client ("127.0.0.1");
Else
P=new client (args[0]);
P.setdefaultcloseoperation (Jframe.exit_on_close);
P.runclient ();

}
}





Related Article

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.