㈠ java中怎样实现网上聊天对话功能
如果采用HTTP协议,也就是网页传输那种方式很简单,同过内嵌对象象application,session为核心的JSP编程,即可实现。但基于TCP/IP通讯方式,无论是在JAVAEE还是在安卓系统,这种开发出来的聊天通讯更快,更实用。
㈡ java 编程题网络聊天室
Public
class
Client
___extends_____
Thread
//定义一个Client线程类
{
Private
____Socket____
m_socket;//定义一个socket对象;
Public
Client()//默认构造函数
{
}
Public
boolean
ConnToServer(String
strip,int
port)//连接指定IP和端口的服务器,如果连接成功,返回true,否则返回false。
{
_m_socket=new
ServerSocket(strip,port)______________________
____return
(m_socket==null)________________________
}
Class
SendData
____extends_____
Thread
{
Socket
m_sendso;
BufferReader
in;
Outprintstream
out;
Public
SendData(Socket
so)
{
___________________
Start();
}
Public
void
run(
)
{
_____in=new
BufferedReader(new
InputStreamReader(System.in)_____________
//不断从键盘获取字符串发送给服务器
}
}
Class
RecvData
___extends______
Thread
{
Socket
m_recvso;
BufferReader
in;
Outprintstream
out;
Public
RecvData
(Socket
so)
{
___________________
Start();
}
已经填了几个空了。。。有些不太会。。。但是填了的一定正确的。。。。
其他你自己搞定吧。。。
㈢ Java网络聊天室
你自己调试编译下,四个java文件:
import java.awt.*;
import java.net.*;
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)
{
}
}
}
}
========================内容过多,未完……
㈣ java 如何实现简单的网络聊天功能,给点思路嘛谢谢,想写写,但是完全没有思路
网络聊天,比如A跟B聊天,首先A和B都必须连接到公用的服务器,然后服务器根据消息的接受者来讲消息转发给对应的人。比如A给B发消息,A首先将消息发送个服务器,服务器接收到该消息,然后看到是A给B发的消息,然后将消息转发给B。A和B不能直接通信,而是用服务器周转。所以你需要实现一个服务端,一个客户端,然后服务端可以同时与多个客户端建立连接,然后服务端实现消息的转发。
㈤ 用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();
}
}
}
㈥ 怎么用java做局域网的聊天工具(聊天室)
呵呵,楼主您好!要用Java做聊天室说简单也不简单,但是说难呢也不难.
说简单点,就是会话跟踪技术(我个人这样理解).要做聊天室,您需要
使用到的工具: tomcat 服务器(因为是免费的,其他也可以哦,呵呵).
Myeclipse(sun公司提供的编写Java程序的工具,别说你不知道哈,
哪样的话我就晕倒了哦,呵呵)
页面框架的设计:index.jsp(聊天室主页面)index_top.jsp(聊天室的顶部页面)
usersonline.jsp(在线人数的统计及显示页面) sendMessage.jsp(发送信息的页面)
showMessage.jsp(显示聊天信息的页面)register.jsp(用户注册的页面)
login.jsp(用户登录页面)
当然,这是最简单的设计方式咯.您也可以设计得更好点.
页面介绍与功能:
index.jsp 主要是聊天室的主页面.由上中下3个框架组成,中间部分在分为
左右2个框架.实际上index.jsp就是一个由于5个框架组成的页面
顶部框架:放index_top.jsp页面.可以设计自己聊天室的特色(比如说:logo)
中间部分的左边框架:showMessage.jsp 显示聊天的信息
中间部分的右边框架:usersonline.jsp(在线人数的统计及显示页面)
底部框架:sendMessage.jsp 这个发送信息的jsp页面.不多说吧
聊天室的框架的设计大楷就是这样子咯
实现聊天:
1.编写一个servlet,用户处理的信息(包括验证用户是否登录和聊天信息)。
2.用户发送信息之后,将发送的信息存放到Application中(群聊)(放在session中就是私聊)
3.显示信息的页面每个XX秒中获取session或者Application中的数据显示出来就OK了
更多的东西还是需要您学习Ajax之后再做,会有不一样的效果哦。祝您成功哟.呵呵
㈦ Java在线聊天的实现技术
socket套接字
㈧ 想用Java web实现在线聊天,求大神指点。
这个只有通过客户端向服务器主动请求的方式实现,因为http协议是无状态的一次请求结束之后,服务器就没法再找到客户端的浏览器了,所以只能是客户端定期到服务器查询有无新消息。消息页面的弹出可以使用js实现。打开多个相同页面可能会同时都弹出吧,这个我不太清楚,不过一般很少有人会去开多个页面吧。至于服务器压力的问题,我觉得应该不是什么大问题,因为每次请求的数据量也不是很大,你可以将请求时间间隔设置的长一点。希望我的回答能对你有帮助。
㈨ 如何使用Java实现网站页面在线聊天
那种timeout不断去查询这种方法,就将之前堵塞的线程解锁,思路应该是下面这种。。,同时再次传递信息给服务器,服务器就会主动将信息推送给客户端,所以让线程堵塞,具体的我有代码,onload事件激发自动传送一个指令给服务器,直到有客户端向服...
㈩ 简单网络聊天程序 java程序代码
1.服务器端的代码:
//ChatServer.Java
import 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.(true);
ChatServer cs = new ChatServer(8888);
cs.startServer();
}
}
2.客户端的代码:
//ChatClient.java
import 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.(true);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ChatClient cc = new ChatClient();
String str = br.readLine();
while (str != null && str.length() != 0) {
cc.send(str);
str = br.readLine();
}
cc.disconnect();
}
class ReceiveThread implements Runnable {
public void run() {
if (s == null)
return;
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while (str != null && str.length() != 0) {
ChatClient.this.ta.append(str + "\n");
str = dis.readUTF();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} }