1. 首页
  2. 聊天

网络聊天室源代码

本文主要为您介绍网络聊天室源代码,内容包括HTML网页聊天室代码,哭求一个HTML网页聊天室代码.高手请进百度,求java网络聊天室代码,简单网络聊天程序java程序代码。没些你要求的那个 我 写了个 简单的 呵呵 package udp;import java.awt.BorderLayout;impor

1. 求java网络聊天室代码

没些你要求的那个 我 写了个 简单的 呵呵 package udp;import java.awt.BorderLayout;import java.awt.List;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;import javax.swing.JFrame;import javax.swing.JPanel;public class netchat { JFrame jf=new JFrame("我的网络聊天程序"); List l=new List(6); JPanel jp=new JPanel(); TextField tip=new TextField(15); TextField tdata=new TextField(1。

dp=new DatagramPacket(buf;import java.0;/);import java; } catch (UnknownHostException e1) { / jf.0.getText()),0.io; byte[] buf;/.awt;/.add(tip, -1);//.awt; jp; try { ds=new DatagramSocket(3000);).receive(dp);/ } catch (IOException e) { /.printStackTrace().length.add(l); jf;import java.TextField; l; /.add(jp;+new String(buf;".awt; DatagramPacket dp;import java.InetAddress; ds:"/ TODO Auto-generated catch block e1; TODO Auto-generated catch block e1; DatagramPacket dp;/.add(tdata.getAddress();+new String(buf,+dp;/.UnknownHostException;/,3000).DatagramSocket; byte [] buf.BorderLayout.setDefaultCloseOperation(JFrame.printStackTrace(); {//.init(), 300); jp; l;/ /, -1).getActionCommand().add(dp; TODO Auto-generated catch block e;import java.SocketException,buf; TODO Auto-generated catch block e.SOUTH),InetAddress;import java; }public static void main(String[] args) { / TextField tip=new TextField(15).setText(".send(dp); } }); buf=e,BorderLayout; public void run() { byte [] buf=new byte[1024]; JPanel jp=new JPanel().add(dp.ActionListener.printStackTrace(); dp=new DatagramPacket(buf; public netchat() { tip;// jf.JFrame.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {//我的网络聊天程序".getText());import javax; tdata.getLength()).List,dp.net.getHostName()+".setSize(300.event.net;DatagramPacket dp;/public class netchat { JFrame jf=new JFrame(".getSource()).getLength()); DatagramSocket ds, 3000).getBytes(); try/.JPanel;/ say;/ TextField tdata=new TextField(15);import java; try { dp= new DatagramPacket(buf,BorderLayout; byte[] buf=new byte[1024];import java,buf;/.net; } new Thread(new Runnable() { DatagramPacket dp.EXIT_ON_CLOSE).length.net.WEST);).swing;/.swing; buf=e,InetAddress.1" while(true) { try { ds:"/.awt.setText("/.DatagramPacket.start();say.printStackTrace().getActionCommand().getHostAddress()+" ds;/import java;127.ActionEvent.getAddress().EAST); TODO Auto-generated method stub new netchat();import javax,0,1024),BorderLayout;/.getByName(tip; } ((TextField)e; List l=new List(6); } catch (IOException e1) { // jf.event.IOException.net; } } } }).getByName(tip; } private void init() { jf.send(dp).getBytes(); } catch (SocketException e) { /import java没些你要求的那个 我 写了个 简单的 呵呵 package udp;import java,1024).awt.setVisible(true); dp=new DatagramPacket(buf;/。

2. 简单网络聊天程序 java程序代码

1.服务器端的代码://ChatServer.Javaimport java.net.*; import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ChatServer extends JFrame { JTextArea ta = new JTextArea(); ServerSocket server = null; Collection cClient = new ArrayList(); public ChatServer(int port) throws Exception { server = new ServerSocket(port); add(ta, BorderLayout.CENTER); setBounds(200, 200, 300, 450); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } public void startServer() throws Exception { while (true) { Socket s = server.accept(); cClient.add(new ClientConn(s)); ta.append(s.getInetAddress().getHostName() + "进入" + " " + "端口号" + s.getPort()); ta.append("\n" + "当前在前总人数: " + cClient.size() + "\n\n"); } } class ClientConn extends Frame implements Runnable, ActionListener { TextArea ta1 = null; TextArea ta2 = null; Button btn = null; Socket s = null; public ClientConn(Socket s) { ta1 = new TextArea(3, 30); ta2 = new TextArea(2, 15); btn = new Button("发送"); this.setLayout(new BorderLayout()); this.add(ta1, BorderLayout.CENTER); this.add(ta2, BorderLayout.SOUTH); this.add(btn, BorderLayout.EAST); this.setSize(300, 200); this.setVisible(true); this.setTitle("" + s.getInetAddress().getHostName() + "端口" + s.getPort()); this.s = s; (new Thread(this)).start(); btn.addActionListener(this); } public void actionPerformed(ActionEvent e) { try { DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF("服务器:\n" + ta2.getText() + "\n"); ta1.append("服务器:\n" + ta2.getText() + "\n"); ta2.setText(""); } catch (IOException E) { } } public void send(String str, String st) throws IOException { DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF(st + "说:\n" + str); } public void dispose() { try { super.dispose(); ta.append(s.getInetAddress().getHostName() + "退出" + "\n"); if (s != null) s.close(); cClient.remove(this); ta.append("当前在线人数: " + cClient.size() + "\n\n"); } catch (Exception e) { e.printStackTrace(); } } public void run() { try { DataInputStream dis = new DataInputStream(s.getInputStream()); String str = dis.readUTF(); String st = s.getInetAddress().getHostName(); while (str != null && str.length() != 0) { for (Iterator it = cClient.iterator(); it.hasNext();) { ClientConn cc = (ClientConn) it.next(); if (this != cc) { cc.send(str, st); } } ta1.append(st + "说:\n" + str + "\n"); str = dis.readUTF(); } this.dispose(); } catch (Exception e) { this.dispose(); } } } public static void main(String[] args) throws Exception { JFrame.setDefaultLookAndFeelDecorated(true); ChatServer cs = new ChatServer(8888); cs.startServer(); } } 2.客户端的代码://ChatClient.javaimport java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ChatClient extends JFrame { JTextArea ta = new JTextArea("你可以通过此客户端的聊天!" + "\n" ); TextArea tf = new TextArea(3, 21); JButton btn = new JButton("发送"); JPanel jp = new JPanel(); Socket s = null; public ChatClient() throws Exception { this.setLayout(new BorderLayout(10, 10)); this.add(ta, BorderLayout.CENTER); jp.add(btn, BorderLayout.SOUTH); this.add(tf, BorderLayout.SOUTH); this.add(jp, BorderLayout.EAST); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { String sSend = tf.getText(); if (sSend.trim().length() == 0) return; ChatClient.this.send(sSend); tf.setText(""); ta.append("你说:" + "\n"); ta.append(sSend + "\n"); } catch (Exception e) { e.printStackTrace(); } } }); btn.setMnemonic(KeyEvent.VK_ENTER); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setBounds(300, 300, 400, 500); setVisible(true); tf.requestFocus(); try { s = new Socket("10.6.86.28", 8888); } catch (Exception e) { ta.append("对不起!无法连接服务器" + "\n"); } (new Thread(new ReceiveThread())).start(); } public void send(String str) throws Exception { DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF(str); } public void disconnect() throws Exception { s.close(); } public static void main(String[] args) throws Exception { JFrame.setDefaultLookAndFeelDecorated(true); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ChatClient cc = new ChatClient(); String str = br.readLine(); while (str != null && str.length() != 0) { cc.se。

3. 求网络聊天室源码,用JSP或Servlet都行

用ajax和jsp写的,可以实现无刷新页面显示聊天记录 js/js.js文件 var obj; var getId=document.getElementById; function objXMLHttp() { var XmlHttpObj = false; if(window.ActiveXObject) { XmlHttpObj = new ActiveXObject("MSXML2.XMLHTTP.3.0"); } else if (window.XMLHttpRequest) { XmlHttpObj = new XMLHttpRequest(); } return XmlHttpObj; } function sendMsg(auto) { obj=objXMLHttp(); if(!auto)// { var myContent = getId("myText").value; if(myContent != "") { if(obj) { obj.onreadystatechange = addChat; obj.open("POST","do.jsp", true); obj.setRequestHeader("Content-Type","application/x-.*;import java.awt.event.*;import java.io.*;import java.util.Hashtable;public class ChatArea extends Panel implements ActionListener,Runnable{ Socket socket=null; DataInputStream in=null; DataOutputStream out=null; Thread threadMessage=null; TextArea 谈话显示区,私聊显示区=null; TextField 送出信息=null; Button 确定,刷新谈话区,刷新私聊区; Label 提示条=null; String name=null; Hashtable listTable; List listComponent=null; Choice privateChatList; int width,height; public ChatArea(String name,Hashtable listTable,int width,int height) { setLayout(null); setBackground(Color.orange); this.width=width; this.height=height; setSize(width,height); this.listTable=listTable; this.name=name; threadMessage=new Thread(this); 谈话显示区=new TextArea(10,10); 私聊显示区=new TextArea(10,10); 确定=new Button("送出信息到:"); 刷新谈话区=new Button("刷新谈话区"); 刷新私聊区=new Button("刷新私聊区"); 提示条=new Label("双击聊天者可私聊",Label.CENTER); 送出信息=new TextField(28); 确定.addActionListener(this); 送出信息.addActionListener(this); 刷新谈话区.addActionListener(this); 刷新私聊区.addActionListener(this); listComponent=new List(); listComponent.addActionListener(this); privateChatList=new Choice(); privateChatList.add("大家(*)"); privateChatList.select(0); add(谈话显示区); 谈话显示区.setBounds(10,10,(width-120)/2,(height-120)); add(私聊显示区); 私聊显示区.setBounds(10+(width-120)/2,10,(width-120)/2,(height-120)); add(listComponent); listComponent.setBounds(10+(width-120),10,100,(height-160)); add(提示条); 提示条.setBounds(10+(width-120),10+(height-160),110,40); Panel pSouth=new Panel(); pSouth.add(送出信息); pSouth.add(确定); pSouth.add(privateChatList); pSouth.add(刷新谈话区); pSouth.add(刷新私聊区); add(pSouth); pSouth.setBounds(10,20+(height-120),width-20,60); } public void setName(String s) { name=s; } public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out) { this.socket=socket; this.in=in; this.out=out; try { threadMessage.start(); } catch(Exception e) { } } public void actionPerformed(ActionEvent e) { if(e.getSource()==确定||e.getSource()==送出信息) { String message=""; String people=privateChatList.getSelectedItem(); people=people.substring(0,people.indexOf("(")); message=送出信息.getText(); if(message.length()>0) { try { if(people.equals("大家")) { out.writeUTF("公共聊天内容:"+name+"说:"+message); } else { out.writeUTF("私人聊天内容:"+name+"悄悄地说:"+message+"#"+people); } } catch(IOException event) { } } } else if(e.getSource()==listComponent) { privateChatList.insert(listComponent.getSelectedItem(),0); privateChatList.repaint(); } else if(e.getSource()==刷新谈话区) { 谈话显示区.setText(null); } else if(e.getSource()==刷新私聊区) { 私聊显示区.setText(null); } } public void run() { while(true) { String s=null; try { s=in.readUTF(); if(s.startsWith("聊天内容:")) { String content=s.substring(s.indexOf(":")+1); 谈话显示区.append("\n"+content); } if(s.startsWith("私人聊天内容:")) { String content=s.substring(s.indexOf(":")+1); 私聊显示区.append("\n"+content); } else if(s.startsWith("聊天者:")) { String people=s.substring(s.indexOf(":")+1,s.indexOf("性别")); String sex=s.substring(s.indexOf("性别")+2); listTable.put(people,people+"("+sex+")"); listComponent.add((String)listTable.get(people)); listComponent.repaint(); } else if(s.startsWith("用户离线:")) { String awayPeopleName=s.substring(s.indexOf(":")+1); listComponent.remove((String)listTable.get(awayPeopleName)); listComponent.repaint(); 谈话显示区.append("\n"+(String)listTable.get(awayPeopleName)+"离线"); listTable.remove(awayPeopleName); } Thread.sleep(5); } catch(IOException e) { listComponent.removeAll(); listComponent.repaint(); listTable.clear(); 谈话显示区.setText("和服务器的连接已中断\n必须刷新浏览器才能再次聊天"); break; } catch(InterruptedException e) { } } }}ChatServer.javaimport java.io.*;import java.net.*;import java.util.*;public class ChatServer { public static void main(String args[]) { ServerSocket server=null; Socket you=null; Hashtable peopleList; peopleList=new Hashtable(); while(true) { try { server=new ServerSocket(6666); } catch(IOException e1) { System.out.println("正在监听"); } try { you=server.accept(); InetAddress address=you.getInetAddress(); System.out.println("用户的IP:"+address); } 。

8. 用JAVA 编写简单网络聊天程序

/*** 基于UDP协议的聊天程序* * 2007.9.18* *///导入包import java.awt.*;import java.awt.event.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import java.net.*;public class Chat extends JFrame implements ActionListener{ //广播地址或者对方的地址 public static final String sendIP = "172.18.8.255"; //发送端口9527 public static final int sendPort = 9527; JPanel p = new JPanel(); List lst = new List(); //消息显示 JTextField txtIP = new JTextField(18); //填写IP地址 JTextField txtMSG = new JTextField(20); //填写发送消息 JLabel lblIP = new JLabel("IP地址:"); JLabel lblMSG = new JLabel("消息:"); JButton btnSend = new JButton("发送"); byte [] buf; //定义DatagramSocket的对象必须进行异常处理 //发送和接收数据报包的套接字 DatagramSocket ds = null;//=============构造函数=====================public Chat(){ CreateInterFace(); //注册消息框监听器 txtMSG.addActionListener(this); btnSend.addActionListener(this); try { //端口:9527 ds =new DatagramSocket(sendPort); } catch(Exception ex) { ex.printStackTrace(); } //============接受消息============ //匿名类 new Thread(new Runnable() { public void run() { byte buf[] = new byte[1024]; //表示接受数据报包 while(true) { try { DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort); ds.receive(dp); lst.add("【消息来自】◆" + dp.getAddress().getHostAddress() + "◆"+"【说】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0); } catch(Exception e) { if(ds.isClosed()) { e.printStackTrace(); } } } } }).start(); //关闭窗体事件 this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent w) { System.out.println("test"); int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION); if(n==JOptionPane.YES_OPTION) { dispose(); System.exit(0); ds.close();//关闭ds对象//关闭数据报套接字 } } });}//界面设计布局public void CreateInterFace(){ this.add(lst,BorderLayout.CENTER); this.add(p,BorderLayout.SOUTH); p.add(lblIP); p.add(txtIP); p.add(lblMSG); p.add(txtMSG); p.add(btnSend); txtIP.setText(sendIP); //背景颜色 lst.setBackground(Color.yellow); //JAVA默认风格 this.setUndecorated(true); this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); this.setSize(600,500); this.setTitle("〓聊天室〓"); this.setResizable(false);//不能改变窗体大小 this.setLocationRelativeTo(null);//窗体居中 this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); this.setVisible(true); txtMSG.requestFocus();//消息框得到焦点}//===============================Main函数===============================public static void main(String[]args){ new Chat();}//================================发送消息=============================== //消息框回车发送消息事件 public void actionPerformed(ActionEvent e) { //得到文本内容 buf = txtMSG.getText().getBytes(); //判断消息框是否为空 if (txtMSG.getText().length()==0) { JOptionPane.showMessageDialog(null,"发送消息不能为空","提示",JOptionPane.WARNING_MESSAGE); } else{ try { InetAddress address = InetAddress.getByName(sendIP); DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort); ds.send(dp); } catch(Exception ex) { ex.printStackTrace(); } } txtMSG.setText("");//清空消息框 //点发送按钮发送消息事件 if(e.getSource()==btnSend) { buf = txtMSG.getText().getBytes(); try { DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort); } catch(Exception ex) { ex.printStackTrace(); } txtMSG.setText("");//清空消息框 txtMSG.requestFocus(); } }}。

9. 急求一个简易聊天室的代码

小小聊天室 <% Session("user

现在共有<%=Application("user_online")%>人在线
************以上为index.asp******************* 发言区 <body bgcolor="#FFFFCC" topmargin="0" leftmargin="0" onl oad="document.form1.say.focus();"> <% IF Request("say")<>"" Then '获取发言的有关信息 Dim say,mycolor,myemote,user_name '声明变量待用 user_name=Session("user_name") '返回用户昵称 say=Server.HtmlEncode(Request("say")) '返回发言,并用HtmlEncode编码 mycolor=Request("mycolor") '返回本次发言的颜色 myemote=Request("myemote") '返回本次发言表情 If myemote="无" Then myemote="" End If '下面三句将得到本次发言的字符串 Dim sayStr sayStr="

本文来自投稿,不代表本站立场,如若转载,请注明出处。