一个很简单的局域网聊天工具的服务器端
/*
* created on 2005-3-18
*
* todo to change the template for this generated file go to
* 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 for this generated type comment go to
* 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();
}
}
客户端
/*
* created on 2005-3-18
*
* todo to change the template for this generated file go to
* 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 for this generated type comment go to
* 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 the 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();
}
}