首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 用netbeans做一个单线程聊天器的步骤,事件处理代码。
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • lajisw521
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 揭贴率:
    发表于:2008-05-22 21:49:40 楼主
    主要讲讲 连接  服务器和客户端怎样通信,具体讲讲怎么实现,我用netbeans做的,讲讲事件处理怎么=实现,聊天只要实现一对一聊天,单线程就可以。我的QQ;79325635
    50  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • stevech
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-05-24 02:11:431楼 得分:0
    java.net

    Socket ServerSocket
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • javagxc
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-06-04 19:58:532楼 得分:0

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

    public class gxc extends JFrame implements ActionListener
    {
        Calendar calendar=Calendar.getInstance();

        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        int minute=calendar.get(Calendar.MINUTE);
        int second=calendar.get(Calendar.SECOND);
        String time=""+hour+":"+minute+":"+second;
        String str=null;
      public static void main(String[] args)
      {
        gxc frame=new gxc();
      }
        JButton command,command2,command3;
        JRadioButton rb[]=new JRadioButton[2];
        JTextArea ta1;
        JTextField tf1,tf2,tf3;
        ServerSocket socket1;
        Socket insocket1,socket2;
        String inbuf;
        BufferedReader in1;
        PrintWriter out1;
        mt625_server t1;
        mt625_client t2;
      public gxc()
      {
        super("聊天室");
        Container c=getContentPane();
        c.setLayout(null);
        JLabel lb=new JLabel("TCP通信");
        lb.setFont(new Font("楷体",Font.BOLD,16));
        lb.setForeground(Color.green);
        lb.setSize(2000,20);
        lb.setLocation(10,2);
        c.add(lb);
        String str1[]={"服务端","客户端"};
        ButtonGroup bg1=new ButtonGroup();
      for(int i=0;i <2;i++)
      {
        rb[i]=new JRadioButton(str1[i]);
        rb[i].setFont(new Font("楷体",Font.BOLD,14));
        rb[i].setForeground(Color.black);
        rb[i].setSize(80,20);
        rb[i].setLocation(10+i*80,27);
        c.add(rb[i]);
        bg1.add(rb[i]);
      }
     
        rb[0].setSelected(true);
        JLabel lb1=new JLabel("连接主机IP");
        lb1.setFont(new Font("楷体",Font.BOLD,16));
        lb1.setForeground(Color.black);
        lb1.setSize(80,25);
        lb1.setLocation(16,55);
        c.add(lb1);
        tf1=new JTextField("10.255.11.199");
        tf1.setForeground(Color.black);
        tf1.setSize(250,25);
        tf1.setLocation(120,55);
        c.add(tf1);
        command=new JButton("连接");
        command.setFont(new Font("楷体",Font.BOLD,16));
        command.setSize(110,20);
        command.setLocation(380,55);
        command.addActionListener(this);
        c.add(command);
        ta1=new JTextArea();
        ta1.setForeground(Color.black);
        ta1.setSize(250,200);
        ta1.setLocation(120,85);
        c.add(ta1);
        JLabel lb3=new JLabel("发送");
        lb3.setFont(new Font("楷体",Font.BOLD,16));
        lb3.setForeground(Color.black);
        lb3.setSize(120,25);
        lb3.setLocation(10,300);
        c.add(lb3);
        tf2=new JTextField();
        tf2.setForeground(Color.black);
        tf2.setSize(250,25);
        tf2.setLocation(120,300);
        c.add(tf2);
        command2=new JButton("发送(Enter)");
        command2.setFont(new Font("楷体",Font.BOLD,16));
        command2.setSize(110,25);
        command2.setLocation(380,300);
        command2.addActionListener(this);
        command2.setEnabled(false);
        c.add(command2);
        command2.setMnemonic(KeyEvent.VK_ENTER);//添加Alt+enter组合键
        JLabel lb4=new JLabel("连接状态: ");
        lb4.setFont(new Font("楷体",Font.BOLD,14));
        lb4.setForeground(Color.black);
        lb4.setSize(120,25);
        lb4.setLocation(180,27);
        c.add(lb4);
        tf3=new JTextField("未登录!");
        tf3.setForeground(Color.black);
        tf3.setSize(120,25);
        tf3.setLocation(270,27);
        c.add(tf3);
        command3=new JButton("下线");
        command3.setFont(new Font("楷体",Font.BOLD,16));
        command3.setSize(110,20);
        command3.setLocation(380,85);
        command3.addActionListener(this);
        command3.setEnabled(false);
        c.add(command3);
        t1=new mt625_server();
        t2=new mt625_client();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,400);
        setVisible(true);
        setLocation(300,300);
    }
      public void actionPerformed(ActionEvent e)
        {
          if(e.getSource()==command)
          {
          try{
            if(rb[0].isSelected()==true)
            {
                str="服务器端:";
            inbuf="";
            tf2.setText("");
            t1.start();
            }
            else{
                str="客户端:";
                inbuf="";
                tf2.setText("");
                t2.start();
                }
          }
            catch (Exception e2) {tf3.setText("错误");}
          }
          if(e.getSource()==command2)
          {
            out1.write(tf2.getText()+"\n");
            out1.flush();
            ta1.setText(ta1.getText()+"\n"+str+"  "+time+"\n"+tf2.getText()+"\n");
            tf2.setText("");
          }
        if(e.getSource()==command3)
          {
          try{
              if(rb[0].isSelected()==true)
              {
                insocket1.close();
                tf3.setText("离线!");
       
                command2.setEnabled(false);
                command3.setEnabled(false);
                System.exit(-1);
       
            }else
              {
                socket2.close();
                tf3.setText("离线!");

                command2.setEnabled(false);
                command3.setEnabled(false);
                System.exit(-1);
              }
            }catch (Exception e2) {tf3.setText("错误");}
        }
        }
         
      class mt625_server extends Thread
      {

        public mt625_server(){}
        public void run()
        {
          try{
            command.setEnabled(false);
            tf3.setText("创建连接!");
            tf1.setText(Inet4Address.getLocalHost().getHostAddress());
            socket1=new ServerSocket(21);
            insocket1=socket1.accept();
            in1=new BufferedReader(new InputStreamReader(insocket1.getInputStream()));
            out1=new PrintWriter(insocket1.getOutputStream(),true);

            while(true){
            if(socket1.isBound()==true)
              {
              tf3.setText("在线!");
              command2.setEnabled(true);
              command3.setEnabled(true);
              break;
              }
            }
            while(true){
              inbuf=in1.readLine();
            if(inbuf.length()>0)
              {
              ta1.append("客户端:  "+time+"\n"+inbuf);
              ta1.append("\n");
              }
            }
            }
            catch(Exception e){}
        }
      }
     
        class mt625_client extends Thread{
        public mt625_client(){}
        public void run()
        {
          try{
              command.setEnabled(false);
              tf3.setText("正在等待连接!");
              socket2=new Socket();
              socket2.connect(new InetSocketAddress(tf1.getText(),21),10002);
              in1=new BufferedReader(new InputStreamReader(socket2.getInputStream()));
              out1=new PrintWriter(socket2.getOutputStream(),true);
              while(true){
              if(socket2.isConnected()==true)
              {
                  tf3.setText("在线!");
                  command2.setEnabled(true);
                  command3.setEnabled(true);
                  break;
                }
              }
              inbuf="";
              while(true){
                inbuf=in1.readLine();
              if(inbuf.length()>0)
              { 
                ta1.append("服务端:  "+time+"\n"+inbuf);
                ta1.append("\n");
              }
            }
          }catch(Exception e){}

      }
      }

    }


    正好有这个程序,给你了,自己看看,想想。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xiyuan1999
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 3

    发表于:2008-06-04 20:17:273楼 得分:0
    服务器端:ChatServer.java

    Java code
    import java.io.*; import java.net.*; import java.util.*; public class ChatServer { boolean started = false; ServerSocket ss = null; List<Client> clients = new ArrayList<Client>(); public static void main(String[] args) { new ChatServer().start(); } public void start() { try { ss = new ServerSocket(8000); started = true; } catch (BindException e) { System.out.println("端口使用中...."); System.out.println("请关掉相关程序并重新运行服务器!"); System.exit(0); } catch (IOException e) { e.printStackTrace(); } try { while(started) { Socket s = ss.accept(); Client c = new Client(s); System.out.println("a client connected!"); new Thread(c).start(); clients.add(c); //dis.close(); } } catch (IOException e) { e.printStackTrace(); } finally { try { ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Client implements Runnable { private Socket s; private DataInputStream dis = null; private DataOutputStream dos = null; private boolean bConnected = false; public Client(Socket s) { this.s = s; try { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); bConnected = true; } catch (IOException e) { e.printStackTrace(); } } public void send(String str) { try { dos.writeUTF(str); } catch (IOException e) { clients.remove(this); System.out.println("对方退出了!我从List里面去掉了!"); //e.printStackTrace(); } } public void run() { try { while(bConnected) { String str = dis.readUTF(); System.out.println(str); for(int i=0; i<clients.size(); i++) { Client c = clients.get(i); c.send(str); //System.out.println(" a string send !"); } /* for(Iterator<Client> it = clients.iterator(); it.hasNext(); ) { Client c = it.next(); c.send(str); } */ /* Iterator<Client> it = clients.iterator(); while(it.hasNext()) { Client c = it.next(); c.send(str); } */ } } catch (EOFException e) { System.out.println("Client closed!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if(dis != null) dis.close(); if(dos != null) dos.close(); if(s != null) { s.close(); //s = null; } } catch (IOException e1) { e1.printStackTrace(); } } } } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xiyuan1999
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 3

    发表于:2008-06-04 20:18:174楼 得分:0
    客户端:ChatClient.java

    Java code
    import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class ChatClient extends Frame { Socket s = null; DataOutputStream dos = null; DataInputStream dis = null; private boolean bConnected = false; TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); Thread tRecv = new Thread(new RecvThread()); public static void main(String[] args) { new ChatClient().launchFrame(); } public void launchFrame() { setLocation(400, 300); this.setSize(300, 300); add(tfTxt, BorderLayout.SOUTH); add(taContent, BorderLayout.NORTH); pack(); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent arg0) { disconnect(); System.exit(0); } }); tfTxt.addActionListener(new TFListener()); setVisible(true); connect(); tRecv.start(); } public void connect() { try { s = new Socket("127.0.0.1", 8888); dos = new DataOutputStream(s.getOutputStream()); dis = new DataInputStream(s.getInputStream()); System.out.println("connected!"); bConnected = true; } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void disconnect() { try { dos.close(); dis.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } /* try { bConnected = false; tRecv.join(); } catch(InterruptedException e) { e.printStackTrace(); } finally { try { dos.close(); dis.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } */ } private class TFListener implements ActionListener { public void actionPerformed(ActionEvent e) { String str = tfTxt.getText().trim(); //taContent.setText(str); tfTxt.setText(""); try { //System.out.println(s); dos.writeUTF(str); dos.flush(); //dos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } private class RecvThread implements Runnable { public void run() { try { while(bConnected) { String str = dis.readUTF(); //System.out.println(str); taContent.setText(taContent.getText() + str + '\n'); } } catch (SocketException e) { System.out.println("退出了,bye!"); } catch (EOFException e) { System.out.println("推出了,bye - bye!"); } catch (IOException e) { e.printStackTrace(); } } } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • amdwinter
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-16 22:39:255楼 得分:0
    MARK
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • nosxcy
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-21 11:47:516楼 得分:0
    顶上上楼的
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Sprite25
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-09-06 16:53:467楼 得分:0
    引用 3 楼 xiyuan1999 的回复:
    服务器端:ChatServer.java

    Java code
    import java.io.*;
    import java.net.*;
    import java.util.*;

    public class ChatServer {
        boolean started = false;
        ServerSocket ss = null;
       
        List <Client> clients = new ArrayList <Client>();
       
        public static void main(String[] args) {
            new ChatServer().start();
        }
       
        public void start() {
            try {
                ss = new Server…

    如果服务器这样写的话,客户端连接很多的话会产生太多的线程
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wangmao2008
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-10-08 15:19:218楼 得分:0
    诚聘IT兼职教师

    北京思源培训中心诚聘既有教学经验又开发经验的兼职教师,
    待遇优厚
    兼职高级讲师若干
    1. MS.NET讲师(主讲.NET 系列课程asp.net, C#)
    2. JAVA讲师 (主讲JAVA,JSP,J2EE等)
    3. MS SQL Server讲师(主讲MS SQL Server课程)
    4. Oracle 讲师(主讲 Oracle 系列课程)
    5.软件测试讲师(主讲软件测试课程)
    5. VC++讲师 (主讲VC++,C++等)
    6. LINUX/UNIX讲师(主讲LINUX / UNIX 系列课程)
    7.IBM Aix /SUN Solaris讲师(主讲系统管理和维护)
    8. Office讲师(精通办公软件)
    9.Ajax讲师(主讲程序设计和WEB高级开发)
    10.3DMax讲师(主讲 3DMax 基础和建模)
    11.应用服务器weblogic/jboss/websphere
    12.CCNA,CCNP,CCSP,CCIE 网络信息安全技术讲师
    13.中间件(Weblogic,Websphere,eclipse等)讲师
    联系方式:abc@ciitc.com  QQ:174629429 MSN:bjcosun@hotmail.com 

    网址:http://www.ciitc.com

    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved