不是自己没动手,而是已经弄好了个,想看看大家的方法跟我的方法哪个好而已,先发个服务器的程序大家看看,不然都以为所有问问题的人都是懒汉。
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Calendar;
import javax.swing.*;public class Server extends Frame implements ActionListener{
private String fileName = "new.txt"; //设置默认的文件名
private boolean serving = false;
private Color backGround = new Color(200,200,200);
private String rightNow;
private InetAddress serverAddress;
private ServerSocket server;
private ServerThread st;
private Hashtable userHashTable;
private Hashtable connectHashTable; private Label serverIP = new Label("本服务器IP地址:");
private TextField printServerIP = new TextField();
private Label serverPort = new Label("端口号:");
private TextField getServerPort = new TextField("8080");
private Button start = new Button(" 启动服务器 ");
private Button end = new Button(" 停止服务器 ");
private Label outputTitle = new Label("聊天记录:");
private TextArea output = new TextArea();
private Label user = new Label("在线成员(0)");
private List list = new List();
private MenuBar menuBar = new MenuBar();//菜单栏
// 文件菜单
private Menu menuFile = new Menu("文件");
private MenuItem menuFileOpen = new MenuItem();
private MenuItem menuFileSave = new MenuItem();
private MenuItem menuFileSaveAs = new MenuItem();
private MenuItem menuFileDelete = new MenuItem();
// 关于菜单
private Menu menuAbout = new Menu("关于");
private MenuItem menuAboutAuthor = new MenuItem();
// 打开文件对话框&保存文件对话框
private FileDialog openDialog = new FileDialog(this, "打开文件", FileDialog.LOAD);
private FileDialog saveAsDialog = new FileDialog(this,"另存为文件", FileDialog.SAVE);
// 响应关闭按钮的内部类
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
// 窗口设计
private void openWindow()
{
Font fb = new Font("Helvetica", Font.BOLD, 17);
Font fp = new Font("Courier", Font.PLAIN, 14);
menuFileOpen.setLabel("打开");
menuFileSave.setLabel("保存");
menuFileSaveAs.setLabel("另存为");
menuFileDelete.setLabel("删除");
menuFile.add(menuFileOpen);
menuFile.add(menuFileSave);
menuFile.add(menuFileSaveAs);
menuFile.addSeparator();
menuFile.add(menuFileDelete);
menuBar.add(menuFile); menuAboutAuthor.setLabel("作者");
menuAbout.add(menuAboutAuthor);
menuBar.add(menuAbout);
setMenuBar(menuBar);
add(output);
menuFileOpen.addActionListener(this);
menuFileSave.addActionListener(this);
menuFileSaveAs.addActionListener(this);
menuFileDelete.addActionListener(this);
menuAboutAuthor.addActionListener(this);
Panel north = new Panel();
north.setLayout(new FlowLayout());
north.add(serverIP);
serverIP.setBackground(backGround);
serverIP.setFont(fp);
north.add(printServerIP);
printServerIP.setBackground(backGround);
north.add(serverPort);
serverPort.setBackground(backGround);
serverPort.setFont(fp);
north.add(getServerPort);
getServerPort.setBackground(backGround);
Panel east = new Panel();
east.setLayout(new BorderLayout());
east.add("North", user);
user.setBackground(backGround);
user.setFont(fp);
east.add("Center", list);
list.setBackground(backGround);
Panel center = new Panel();
center.setLayout(new BorderLayout());
center.add("North", outputTitle);
outputTitle.setBackground(backGround);
outputTitle.setFont(fp);
center.add("Center", output);
output.setBackground(backGround);
Panel south = new Panel();
south.setLayout(new FlowLayout());
south.add(start);
start.setBackground(new Color(192, 192, 192));
start.setFont(fb);
south.add(end);
end.setBackground(new Color(192, 192, 192));
end.setFont(fb);
menuFile.add(menuFileOpen);
menuBar.add(menuFile);
setMenuBar(menuBar);
setLayout(new BorderLayout());
add("North", north);
north.setBackground(backGround);
add("East", east);
east.setBackground(backGround);
add("Center", center);
add("South", south);
setBackground(backGround);
}
// 构造方法
public Server()throws UnknownHostException, IOException
{
super("Server");
serving = false;
serverAddress = InetAddress.getLocalHost();
byte[] IP = serverAddress.getAddress();
printServerIP.setText((IP[0]&0xFF)+"."+(IP[1]&0xFF)+"."
+(IP[2]&0xFF)+"."+(IP[3]&0xFF)); printServerIP.setEditable(false);
output.setEditable(false);
start.addActionListener(this);
end.addActionListener(this);
end.setEnabled(false);
addWindowListener(new WindowCloser());
openWindow();
pack();
setSize(400,300);
show();
}
// 用于接收客户消息的线程类
private class ServerThreadSingle extends Thread{
public Socket connection;
private DataInputStream in;
private DataOutputStream out;
private boolean login;
private String userName;
public ServerThreadSingle(Socket newConnection){
try{
connection = newConnection;
in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(connection.getOutputStream());
login = false;
userName = new String("");
start();
}catch(IOException ioe){
output.append("Error: "+ioe);
}
}
public void run(){
try{
String line = new String("Q");
while(!login){
if(!serving) break;
line = in.readUTF();
if(line.charAt(0) == 'L'){
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Calendar;
import javax.swing.*;public class Server extends Frame implements ActionListener{
private String fileName = "new.txt"; //设置默认的文件名
private boolean serving = false;
private Color backGround = new Color(200,200,200);
private String rightNow;
private InetAddress serverAddress;
private ServerSocket server;
private ServerThread st;
private Hashtable userHashTable;
private Hashtable connectHashTable; private Label serverIP = new Label("本服务器IP地址:");
private TextField printServerIP = new TextField();
private Label serverPort = new Label("端口号:");
private TextField getServerPort = new TextField("8080");
private Button start = new Button(" 启动服务器 ");
private Button end = new Button(" 停止服务器 ");
private Label outputTitle = new Label("聊天记录:");
private TextArea output = new TextArea();
private Label user = new Label("在线成员(0)");
private List list = new List();
private MenuBar menuBar = new MenuBar();//菜单栏
// 文件菜单
private Menu menuFile = new Menu("文件");
private MenuItem menuFileOpen = new MenuItem();
private MenuItem menuFileSave = new MenuItem();
private MenuItem menuFileSaveAs = new MenuItem();
private MenuItem menuFileDelete = new MenuItem();
// 关于菜单
private Menu menuAbout = new Menu("关于");
private MenuItem menuAboutAuthor = new MenuItem();
// 打开文件对话框&保存文件对话框
private FileDialog openDialog = new FileDialog(this, "打开文件", FileDialog.LOAD);
private FileDialog saveAsDialog = new FileDialog(this,"另存为文件", FileDialog.SAVE);
// 响应关闭按钮的内部类
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
// 窗口设计
private void openWindow()
{
Font fb = new Font("Helvetica", Font.BOLD, 17);
Font fp = new Font("Courier", Font.PLAIN, 14);
menuFileOpen.setLabel("打开");
menuFileSave.setLabel("保存");
menuFileSaveAs.setLabel("另存为");
menuFileDelete.setLabel("删除");
menuFile.add(menuFileOpen);
menuFile.add(menuFileSave);
menuFile.add(menuFileSaveAs);
menuFile.addSeparator();
menuFile.add(menuFileDelete);
menuBar.add(menuFile); menuAboutAuthor.setLabel("作者");
menuAbout.add(menuAboutAuthor);
menuBar.add(menuAbout);
setMenuBar(menuBar);
add(output);
menuFileOpen.addActionListener(this);
menuFileSave.addActionListener(this);
menuFileSaveAs.addActionListener(this);
menuFileDelete.addActionListener(this);
menuAboutAuthor.addActionListener(this);
Panel north = new Panel();
north.setLayout(new FlowLayout());
north.add(serverIP);
serverIP.setBackground(backGround);
serverIP.setFont(fp);
north.add(printServerIP);
printServerIP.setBackground(backGround);
north.add(serverPort);
serverPort.setBackground(backGround);
serverPort.setFont(fp);
north.add(getServerPort);
getServerPort.setBackground(backGround);
Panel east = new Panel();
east.setLayout(new BorderLayout());
east.add("North", user);
user.setBackground(backGround);
user.setFont(fp);
east.add("Center", list);
list.setBackground(backGround);
Panel center = new Panel();
center.setLayout(new BorderLayout());
center.add("North", outputTitle);
outputTitle.setBackground(backGround);
outputTitle.setFont(fp);
center.add("Center", output);
output.setBackground(backGround);
Panel south = new Panel();
south.setLayout(new FlowLayout());
south.add(start);
start.setBackground(new Color(192, 192, 192));
start.setFont(fb);
south.add(end);
end.setBackground(new Color(192, 192, 192));
end.setFont(fb);
menuFile.add(menuFileOpen);
menuBar.add(menuFile);
setMenuBar(menuBar);
setLayout(new BorderLayout());
add("North", north);
north.setBackground(backGround);
add("East", east);
east.setBackground(backGround);
add("Center", center);
add("South", south);
setBackground(backGround);
}
// 构造方法
public Server()throws UnknownHostException, IOException
{
super("Server");
serving = false;
serverAddress = InetAddress.getLocalHost();
byte[] IP = serverAddress.getAddress();
printServerIP.setText((IP[0]&0xFF)+"."+(IP[1]&0xFF)+"."
+(IP[2]&0xFF)+"."+(IP[3]&0xFF)); printServerIP.setEditable(false);
output.setEditable(false);
start.addActionListener(this);
end.addActionListener(this);
end.setEnabled(false);
addWindowListener(new WindowCloser());
openWindow();
pack();
setSize(400,300);
show();
}
// 用于接收客户消息的线程类
private class ServerThreadSingle extends Thread{
public Socket connection;
private DataInputStream in;
private DataOutputStream out;
private boolean login;
private String userName;
public ServerThreadSingle(Socket newConnection){
try{
connection = newConnection;
in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(connection.getOutputStream());
login = false;
userName = new String("");
start();
}catch(IOException ioe){
output.append("Error: "+ioe);
}
}
public void run(){
try{
String line = new String("Q");
while(!login){
if(!serving) break;
line = in.readUTF();
if(line.charAt(0) == 'L'){
解决方案 »
- 跪求孙鑫的那本《struts2深入详解》电子书
- SSH项目中myeclipse不编译JSP,Java文件,不加载xml文件怎么办??
- org.apache.commons.net.ftp.FTPClient下载文件当文件路径出现中文乱码如何处理?
- 元素 动态name 如何取值
- [求助]一句话概括spring,dwr是什么!
- 跪求jsp页面上table里的数据导入到excle中的简便方法。。。急急急。。
- 求 Middlegen-Hibernate 中的两个配置文件!(build.xml , mssql.xml)
- 请问,用java怎样根据曲线的方程将曲线画出来,又没有类似与jfreechart的东西可以做?
- WebSphere Studio Application Developer 5.0
- JDBC问题
- 一个有关JAVA MSN LIBRARY(jml)的问题(100分)
- 我确实没有办法!!搞不明白!
if(userName.length() == 0 ||userName.equalsIgnoreCase("登录名:")){
out.writeUTF("R"); //refused
break;
}
// 判断是否已存在该用户名
for(Enumeration e =userHashTable.keys(); e.hasMoreElements();){
String str = (String)(e.nextElement());
if(str.equalsIgnoreCase(userName)){
out.writeUTF("R"); //refused
return;
}
}
// 向登陆的用户发送当前在线用户列表
login = true;
list.add(userName);
user.setText("在线成员 ("+list.getItemCount()+")");
String[] str = list.getItems();
line = "A "; //accepted
for (int i = 0; i < str.length; i ++){
line += (str [i] + " ");
}
out.writeUTF(line);
line = "L "+userName;
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\n!!!"+userName+" 上线.\n\n");
// 向在线用户(不包括当前客户)发送消息
for(Enumeration e =userHashTable.elements(); e.hasMoreElements();){
DataOutputStream out = (DataOutputStream)(e.nextElement());
out.writeUTF(line);
}
userHashTable.put(userName, out); // 将登录的客户加入名单
connectHashTable.put(userName, connection);
}
}
//当用户退出时,该用户从名单中剔除
while(login){
line = in.readUTF();
if(!serving) break;
if(line.charAt(0) == 'Q'){
out.writeUTF("Q");
login = false;
userHashTable.remove(userName);
connectHashTable.remove(userName);
list.remove(userName);
user.setText("在线成员 ("+list.getItemCount()+")"); line = "Q "+userName;
}
else{
line = userName+":\n"+line.substring(2);
}
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\n"+(line.charAt(0)=='Q'?"!!!"+userName+"刚刚下线.":line)+"\n\n");
// 向所有客户发送消息
for(Enumeration e =userHashTable.elements(); e.hasMoreElements();){
DataOutputStream out = (DataOutputStream)(e.nextElement());
out.writeUTF(line);
}
}
in.close();
out.close();
connection.close();
}catch(SocketException se){
userHashTable.remove(userName);
connectHashTable.remove(connection);
list.remove(userName);
output.append("!!!与 "+userName+" 断开连接\n");
}catch(IOException ioe){
if(!(ioe instanceof EOFException)){
output.append("Error: "+ioe+"\n\n");
}
}
}
}
// 用于接受客户连接请求的线程类
private class ServerThread extends Thread{
private boolean running;
public ServerThread()
{
start();
}
public void run()
{
try{
while(serving){
Socket connection = server.accept();
ServerThreadSingle handler
= new ServerThreadSingle(connection);
}
}catch(SocketException se){
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\n服务器停止.\n\n");
}catch(IOException ioe){
output.append("Error: "+ioe+"\n\n");
}
}
}
// 消息处理方法
public void actionPerformed(ActionEvent event){
if(event.getSource() == end && serving) {
try{
for(Enumeration e =userHashTable.elements(); e.hasMoreElements();){
DataOutputStream out = (DataOutputStream)(e.nextElement());
out.writeUTF("Q");
}
serving = false;
userHashTable.clear();
connectHashTable.clear();
list.clear();
user.setText("在线成员 (0)");
server.close();
end.setEnabled(false);
start.setEnabled(true);
}catch(SocketException se){
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\n服务器停止运行.\n\n");
}catch(IOException ioe){
output.append("Error: "+ioe+"\n\n");
}
}
else if(event.getSource() == menuFileOpen) {
openDialog.show();
fileName = openDialog.getDirectory()+openDialog.getFile();
if(fileName != null)
readFile(fileName);
}
else if(event.getSource() == menuFileSaveAs) {
saveAsDialog.show();
fileName = saveAsDialog.getDirectory()+saveAsDialog.getFile();
if(fileName != null)
writeFile(fileName);
}
else if(event.getSource() == menuFileSave) {
if(fileName != null)
writeFile(fileName);
}
else if(event.getSource() == menuFileDelete){
output.setText("");
}
else if(event.getSource() == menuAboutAuthor){
JOptionPane.showMessageDialog(null,"专业:05计本");
}
else if(event.getSource() == start && !serving){
try{
server = new ServerSocket(Integer.parseInt(getServerPort.getText()));
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\n服务器启动.\n\n");
start.setEnabled(false);
end.setEnabled(true);
userHashTable = new Hashtable();
connectHashTable = new Hashtable();
st = new ServerThread();
serving = true;
}catch(IOException ioe){
output.append("Error: "+ioe+"\n\n");
}
}
}
public void readFile(String fileName){
try{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content, charsRead, size-charsRead);
readIn.close();
output.setText(new String(content,0,charsRead));
}catch(IOException e){
System.out.println("Error opening File");
}
} public void writeFile(String fileName){
try{
File file = new File(fileName);
FileWriter writeOut = new FileWriter(file);
writeOut.write(output.getText());
writeOut.close();
}catch(IOException e){
System.out.println("Error writing file");
}
}
// 程序入口
public static void main(String args [])throws UnknownHostException, IOException
{
Server s = new Server();
}}
还有一个客户端的程序就不发了,这是课程设计,照我这样做的,感觉浪费,最少是精而少的那种,
新的要求:在客户断把数据编辑了,按保存就把他保存到服务器,如果保存成功,服务器返回保存成功的信息即可