AppServer.java
资源名称:AppServer.rar [点击查看]
上传用户:bjlylbj
上传日期:2010-01-31
资源大小:5k
文件大小:4k
源码类别:
Telnet服务器
开发平台:
Java
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- public class AppServer extends Thread implements ActionListener
- {
- static JFrame frm;
- static JPanel pnl;
- JScrollPane slp_chat;
- JTextArea txr_chat;
- JScrollPane slp_sendchat;
- JTextArea txr_sendchat;
- JLabel lbl_chat;
- JLabel lbl_sendchat;
- JButton btn_send;
- JButton btn_exit;
- String str;
- ServerSocket serverSocket;
- public static AppServer p;
- public AppServer()
- {
- Font f=new Font("Roman",Font.BOLD,18);
- frm=new JFrame("online chat on server");
- pnl=new JPanel();
- pnl.setLayout(null);
- frm.getContentPane().add(pnl);
- lbl_chat=new JLabel("online");
- lbl_chat.setBounds(new Rectangle(0,5,50,30));
- pnl.add(lbl_chat);
- txr_chat=new JTextArea();
- txr_chat.setEditable(false);
- slp_chat=new JScrollPane(txr_chat);
- slp_chat.setBounds(new Rectangle(3,50,480,200));
- pnl.add(slp_chat);
- lbl_sendchat=new JLabel("send message");
- lbl_sendchat.setBounds(new Rectangle(0,250,100,30));
- pnl.add(lbl_sendchat);
- txr_sendchat=new JTextArea();
- slp_sendchat=new JScrollPane(txr_sendchat);
- slp_sendchat.setBounds(new Rectangle(3,280,480,80));
- pnl.add(slp_sendchat);
- txr_chat.setFont(f);
- txr_sendchat.setFont(f);
- btn_send=new JButton("send");
- btn_exit=new JButton("exit");
- btn_send.addActionListener(this);
- btn_exit.addActionListener(this);
- btn_send.setFont(f);
- btn_exit.setFont(f);
- txr_chat.setText("");
- btn_exit.setBounds(new Rectangle(5,370,100,30));
- pnl.add(btn_exit);
- btn_send.setBounds(new Rectangle(350,370,100,30));
- pnl.add(btn_send);
- frm.setSize(500,500);
- frm.setVisible(true);
- frm.setResizable(false);
- str="";
- try
- {
- serverSocket=new ServerSocket(1001);
- }
- catch(IOException e)
- {
- fail(e,"could not start server");
- }
- System.out.println("server start ...");
- this.start();
- }
- public static void fail(Exception e,String str)
- {
- System.out.print(str+"."+e);
- }
- public void run()
- {
- try
- {
- while(true)
- {
- Socket client= serverSocket.accept();
- Conn con=new Conn(client);
- }
- }
- catch(IOException e)
- {
- fail(e,"could not start server");
- }
- }
- public static void main(String args[])
- {
- p=new AppServer();
- }
- public void actionPerformed(ActionEvent evt)
- {
- if(evt.getSource()==btn_exit)
- {
- frm=null;
- frm.disable();
- frm.setVisible(false);
- }
- else if(evt.getSource()==btn_send)
- {
- Msg.str=txr_sendchat.getText();
- txr_sendchat.setText("");
- }
- }
- public class Conn extends Thread
- {
- protected Socket netClient ;
- protected BufferedReader fromClient;
- protected PrintStream toClient;
- ;
- public Conn(Socket client)
- {
- netClient=client;
- try
- {
- fromClient=new BufferedReader
- (new InputStreamReader
- (netClient.getInputStream()));
- toClient=new PrintStream(netClient.getOutputStream());
- }
- catch(IOException e)
- {
- try
- {
- netClient.close();
- }
- catch(IOException ex)
- {
- System.err.println(ex.toString());
- }
- }
- this.start();
- }
- public void run()
- {
- String fromclientMessage="";
- try
- {
- while(true)
- {
- fromclientMessage=fromClient.readLine();
- if(txr_chat.getText().equals(""))
- {
- txr_chat.setText("client said: "+fromclientMessage);
- }
- else
- {
- txr_chat.setText(txr_chat.getText()+"n"+"client said:"+fromclientMessage);
- }
- while(Msg.str.equals(""))
- ;
- txr_chat.setText(txr_chat.getText()+"n"+"server said:"+Msg.str);
- toClient.println(Msg.str);
- Msg.str="";
- }
- }
- catch(IOException e)
- {
- }
- finally
- {
- try
- {
- netClient.close();
- }
- catch(IOException ex)
- {
- }
- }
- }
- }
- }