java急救聊天系统中如何实现服务器端转发数据

java急救聊天系统中如何实现服务器端转发数据,第1张

Problem to solve:
Write a stack-based Calculator program in Java to support the following operators for integer and floating point data: addition, multiplication, division, subtraction; modulo and square-root Also implement a print operator that prints out the entire stack and a pop operator that discards the top element on the stack Your program should be in pure Java, and can use any of the standard Java Development Kit class library elements Your operand stack should support mixed mode arithmetic and should promote integers to floating point numbers where appropriate Implement appropriate exception handling to cope with malformed input from the user
Specification:
Your program should accept textual input like:
1440 sqrt
120
10 20 add
30
2 3 4 add mult
14
print
12
30
14
pop
pop
20
mult
240
Where the bolded lines are representative of your program’s output See overleaf for further hints and specification notes See also the note on the paper web site on how to use the Java Compilation system

呵呵,楼主您好!要用Java做聊天室说简单也不简单,但是说难呢也不难
说简单点,就是会话跟踪技术(我个人这样理解)要做聊天室,您需要
使用到的工具: tomcat 服务器(因为是免费的,其他也可以哦,呵呵)
Myeclipse(sun公司提供的编写Java程序的工具,别说你不知道哈,
哪样的话我就晕倒了哦,呵呵)
页面框架的设计:indexjsp(聊天室主页面)index_topjsp(聊天室的顶部页面)
usersonlinejsp(在线人数的统计及显示页面) sendMessagejsp(发送信息的页面)
showMessagejsp(显示聊天信息的页面)registerjsp(用户注册的页面)
loginjsp(用户登录页面)
当然,这是最简单的设计方式咯您也可以设计得更好点
页面介绍与功能:
indexjsp 主要是聊天室的主页面由上中下3个框架组成,中间部分在分为
左右2个框架实际上indexjsp就是一个由于5个框架组成的页面
顶部框架:放index_topjsp页面可以设计自己聊天室的特色(比如说:logo)
中间部分的左边框架:showMessagejsp 显示聊天的信息
中间部分的右边框架:usersonlinejsp(在线人数的统计及显示页面)
底部框架:sendMessagejsp 这个发送信息的jsp页面不多说吧
聊天室的框架的设计大楷就是这样子咯
实现聊天:
1编写一个servlet,用户处理的信息(包括验证用户是否登录和聊天信息)。
2用户发送信息之后,将发送的信息存放到Application中(群聊)(放在session中就是私聊)
3显示信息的页面每个XX秒中获取session或者Application中的数据显示出来就OK了
更多的东西还是需要您学习Ajax之后再做,会有不一样的效果哦。祝您成功哟呵呵

1可以用一个线程阻塞
BufferedReader stdin = new BufferedReader(new InputStreamReader(Systemin));
while(true){
Systemoutprintln(stdinreadLine());
//其它 *** 作,如,编码、发送等 *** 作
}
建议采用多线程,否则无法响应别的事件了。
2同样是多线程,Threadsleep(5000);,很简单的。
3这个可以用时间来标识,比如本地记录最后一次请求时间,请求的时候把这个时间给服务器,服务器返回给定时间之后的记录。

服务器端当然是放服务器的主机名了,服务器是死的,客户端是活的,可以有多个客户端,但服务器端一般只有一个。那你想想服务器端怎么可能知道全部的客户端的地址呢?全都是客户端去连接服务器,然后服务器给客户端响应

import javaawtColor;
import javaawtDimension;
import javaawtFont;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventKeyEvent;
import javaawteventKeyListener;
import javaioIOException;
import javanetDatagramPacket;
import javanetDatagramSocket;
import javanetInetAddress;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJScrollPane;
import javaxswingJSplitPane;
import javaxswingJTextArea;
public class ChatFrame extends JPanel{
private static final long serialVersionUID = 1L;
private int width=450,height=450;
boolean flag;
int port = 10086;
String address = "1921683352";
// int port = 2425;
DatagramSocket send;
DatagramSocket receive;
UDP_Send udpSend;
JTextArea showTextArea;
JTextArea inputTextArea;
JPanel rightPanel;
JScrollPane scrollPane1;
JScrollPane scrollPane2;
JSplitPane splitPane1;
JSplitPane splitPane2;
JButton sendButton;
JButton closeButton;
JPanel buttonpane;
public ChatFrame() throws Exception {
super(null, true);
send = new DatagramSocket();
receive = new DatagramSocket(port);
udpSend =new UDP_Send(send,port);
showTextArea = new JTextArea();
showTextAreasetLineWrap(true);
showTextAreasetEditable(false);
showTextAreasetForeground(new Color(0xf90033));
scrollPane1 = new JScrollPane(showTextArea);
inputTextArea = new JTextArea();
inputTextAreasetLineWrap(true);
scrollPane2 = new JScrollPane(inputTextArea);
rightPanel = new JPanel();
rightPanelsetBackground(new Color(0x0099ff));
splitPane1 = new JSplitPane(JSplitPaneVERTICAL_SPLIT,true,scrollPane1,scrollPane2);
splitPane2 = new JSplitPane(JSplitPaneHORIZONTAL_SPLIT,true,splitPane1,rightPanel);
inputTextAreaaddKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
switch (egetKeyCode()) {
case KeyEventVK_ENTER:
udpSendsend(port);
inputTextAreasetText("");
break;
default:
break;
}
}
});
sendButton = new JButton("发送");
sendButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
udpSendsend(port);
inputTextAreasetText("");
}
});
closeButton = new JButton("关闭");
closeButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Systemexit(0);
}
});
buttonpane = new JPanel();
buttonpaneadd(closeButton);
buttonpaneadd(sendButton);
splitPane1setBounds(0, 0, width, height-32);
splitPane1setDividerLocation(08);
splitPane2setBounds(0, 0, width, height-32);
splitPane2setDividerLocation(08);
buttonpanesetBounds(width-250, height-32, 140, 32);
thisadd(splitPane2);
thisadd(buttonpane);
thissetPreferredSize(new Dimension(width,height));
new Thread(new UDP_Receive(receive))start();
}
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
framesetContentPane(new ChatFrame());
framepack();
framesetResizable(false);
framesetLocationRelativeTo(null);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
class UDP_Send{
DatagramSocket datagramSocket;
int port;
String text;
public UDP_Send(DatagramSocket datagramSocket,int port) {
thisdatagramSocket = datagramSocket;
thisport = port;
}
public void send(int port) {
try {
text = new String(inputTextAreagetText()getBytes(), "utf-8");
byte buffered[] = textgetBytes();
DatagramPacket database = new DatagramPacket(buffered,
bufferedlength, InetAddressgetByName(address),port) ;
showTextAreasetFont(new Font("微软雅黑", FontCENTER_BASELINE, 24));
showTextAreaappend("本机"+":\n");
showTextAreasetFont(new Font("仿宋", FontCENTER_BASELINE, 16));
showTextAreaappend(" "+text+"\n");
datagramSocketsend(database);
} catch (IOException e) {
eprintStackTrace();
}
}
}
class UDP_Receive implements Runnable {
DatagramSocket datagramSocket;
public UDP_Receive(DatagramSocket datagramSocket) {
thisdatagramSocket = datagramSocket;
}
@Override
public void run() {
try {
byte buff[] = new byte[4096];
DatagramPacket database = new DatagramPacket(buff, bufflength);
while (true) {
datagramSocketreceive(database);
String text = new String(databasegetData(), 0,databasegetLength(),"utf-8");
showTextAreasetFont(new Font("微软雅黑", FontCENTER_BASELINE, 24));
showTextAreaappend(databasegetAddress()+":\n");
showTextAreasetFont(new Font("仿宋", FontCENTER_BASELINE, 16));
showTextAreaappend(" "+text+"\n");
}
} catch (IOException e) {
eprintStackTrace();
}
}
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10834600.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存