一个简单的server和client程序

来源:互联网 发布:80端口开启 编辑:程序博客网 时间:2024/05/21 07:58

import java.io.*;//服务器端程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
import java.text.*;

public class ChatServer{
       class B implements Runnable{
         public void run(){
           while(true){
             try{
               t1.append(in.readUTF()+"\n");
               t1.setCaretPosition(t1.getText().length());
                }catch(Exception e){e.printStackTrace();}
             }
           }
        }
           
       JTextArea t1;
       JTextArea t2;
       private JFrame f;
       private JOptionPane jop;
       private ServerSocket ss;
       private Socket socket;
       private DataInputStream in;
       private DataOutputStream out;
       SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                          //设置日期格式
       public ChatServer(){
          try{
            ss=new ServerSocket(9999);
            socket=ss.accept();
            in=new DataInputStream(socket.getInputStream());
            out=new DataOutputStream(socket.getOutputStream());
             }catch(Exception e){e.printStackTrace();}
          f=new JFrame("服务器端程序");
          ImageIcon icon=new ImageIcon("信纸背景1.png");
          JLabel label=new JLabel(icon);//给标签加上图片
          label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
              //设置标签大小即为图片大小
          JPanel p1=(JPanel)f.getContentPane();
              //把内容窗格转化为JPanel
          p1.setLayout(null);
            //设置p1的布局为空
          p1.setOpaque(false);
            //把p1设置透明,以显示图片
          f.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
            //把图片添加到分层窗口的最底层
          t1=new JTextArea(60,40);
          t1.setLineWrap(true);
          t2=new JTextArea(10,20);
          t2.setLineWrap(true);
          JScrollPane jsp1=new JScrollPane(t1);
         
          JScrollPane jsp2=new JScrollPane(t2);
         
          JButton send=new JButton("发 送");
          JButton clear=new JButton("清 空");
          t1.setOpaque(false);
          t2.setOpaque(false);//设置文本域透明
          jsp1.setOpaque(false);
          jsp1.getViewport().setOpaque(false);
          jsp2.setOpaque(false);
          jsp2.getViewport().setOpaque(false);
           //把滚动条设置为透明
          jsp1.setBounds(0,0,453,465);
          jsp2.setBounds(0,465,453,115);
          send.setBounds(150,580,100,40);
          clear.setBounds(250,580,100,40);
            //通过绝对布局给组件布局
          Font font=new Font("楷体",Font.PLAIN,16);
          t1.setFont(font);
          t2.setFont(font);//设置字体
          send.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                try{
                  String s1=null;
                  s1=t2.getText();
                  t2.setText(" ");
                  System.out.println(s1);
                  //t1.append(in.readUTF()+"\n");
                  t1.append(df.format(new Date())+" Server:"+"\n"+s1+"\n");
                  out.writeUTF(df.format(new Date())+" Server:"+"\n"+s1);
                  t1.setCaretPosition(t1.getText().length());
                 }catch(Exception ce){ce.printStackTrace();}
               }
             });
          p1.add(jsp1);
          p1.add(jsp2);
          p1.add(send);
          p1.add(clear);
          f.setSize(460,656);
          f.setLocation(500,400);
          f.setVisible(true);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
               B b=new B();
               Thread k=new Thread(b);
               k.start();
              // k.sleep(2000);
        
        }
     
     public static void main(String[] args){
        new ChatServer();
       }
   }
                     

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
import java.text.*;


public class ChatClient{//客户端程序
       class A implements Runnable{
           public void run(){
              while(true){
                try{
                 
                   t1.append(in.readUTF()+"\n");
                   t1.setCaretPosition(t1.getText().length());
       //通过改变文本插入符,把每次聊天的最新内容显示出来
                   }catch(Exception e){e.printStackTrace();}
              }
           }
        }
        
       private JTextArea t1;
       private JTextArea t2;
       private JFrame f;
       private JOptionPane jop;
       private Socket socket;
       private DataInputStream in;
       private DataOutputStream out;
       SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                          //设置日期格式
    String s=null;
       public ChatClient(){
       try{
            socket=new Socket("192.168.24.44",9999);
            in=new DataInputStream(socket.getInputStream());
            out=new DataOutputStream(socket.getOutputStream());
             }catch(Exception e){e.printStackTrace();}
          f=new JFrame("客户端程序");
          ImageIcon icon=new ImageIcon("信纸背景1.png");
          JLabel label=new JLabel(icon);//给标签加上图片
          label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
              //设置标签大小即为图片大小
          JPanel p1=(JPanel)f.getContentPane();
              //把内容窗格转化为JPanel
          p1.setLayout(null);
            //设置p1的布局为空
          p1.setOpaque(false);
            //把p1设置透明,以显示图片
          f.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
            //把图片添加到分层窗口的最底层
          t1=new JTextArea(60,40);
          t1.setLineWrap(true);
          t2=new JTextArea(10,20);
          t2.setLineWrap(true);
          JScrollPane jsp1=new JScrollPane(t1);
         
          JScrollPane jsp2=new JScrollPane(t2);
         
          JButton send=new JButton("发 送");
          JButton clear=new JButton("清 空");
          t1.setOpaque(false);
          t2.setOpaque(false);//设置文本域透明
          jsp1.setOpaque(false);
          jsp1.getViewport().setOpaque(false);
          jsp2.setOpaque(false);
          jsp2.getViewport().setOpaque(false);
           //把滚动条设置为透明
          jsp1.setBounds(0,0,453,465);
          jsp2.setBounds(0,465,453,115);
          send.setBounds(150,580,100,40);
          clear.setBounds(250,580,100,40);
            //通过绝对布局给组件布局
          Font font=new Font("楷体",Font.PLAIN,16);
          t1.setFont(font);
          t2.setFont(font);//设置字体
        
          send.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                 try{
                   
                    s=t2.getText();
                    t2.setText(" ");
                    //t1.append(in.readUTF()+"\n");
                    System.out.println(s);
                    t1.append("Client "+df.format(new Date())+"\n"+s+"\n");
                    out.writeUTF(df.format(new Date())+" Client:"+"\n"+s);
                    t1.setCaretPosition(t1.getText().length());
                    }catch(Exception ce){ce.printStackTrace();}
                  }
               });
    t2.addKeyListener(new KeyAdapter(){
     //文本域键盘监听,当按下Enter时,触发事件
     public void keyPressed(KeyEvent e){
      if(e.getKeyCode()==KeyEvent.VK_ENTER){
       try{
       s=t2.getText();
       t2.setText(" ");
          t1.append("Client "+df.format(new Date())+"\n"+s+"\n");
                         out.writeUTF(df.format(new Date())+" Client:"+"\n"+s);
                         t1.setCaretPosition(t1.getText().length());
       }catch(Exception ce){ce.printStackTrace();}
      }
     }
    });

          p1.add(jsp1);
          p1.add(jsp2);
          p1.add(send);
          p1.add(clear);
          f.setSize(460,656);
          f.setLocation(500,400);
          f.setVisible(true);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
               A a=new A();
               Thread q=new Thread(a);
               q.start();
               //q.sleep(2000);
          
              
       
         }
      public static void main(String[] args){
           new ChatClient();
          }
      }

ps:加了背景图片,所以程序比较长大笑

原创粉丝点击