最近搞一个 java 聊天室程序 遇到麻烦:
如何在我现有的 ChatServerRoom.java 中实现数据由服务器端传送到客户端(***比如说 “将在线人数返回到客户端界面上ChatClientRoom.java中的(panel3)上***)
由于初学 java 希望大家给个帮助哈!!谢谢
下面是我的程序
登陆界面为:christain2.java
客户端界面为:ChatClientRoom.java (在下页)
服务器端界面为:ChatServerRoom.java (在另外一个页面)
登陆界面为:christain2.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Christain2 implements ActionListener
{ Frame f;
Label t1,t2;
TextField tf1;
Label t3;
TextField tf2;
Button m;
public void display()
{
Frame f;
f = new Frame("chatRoom"); //创建框架并设置标题
f.setSize(600,350); //框架大小
f.setBackground(Color.GREEN);
f.setLayout(new FlowLayout(FlowLayout.LEFT));
t1=new Label("用户名");
t3=new Label("嘟嘟 的 乐 园");
tf1=new TextField("user1",25);
tf1.setEditable(true);
t2=new Label("密码:");
tf2=new TextField(15);
tf2.setEditable(true);
f.add(t3);
f.add(t1);
f.add(tf1);
f.add(t2);
f.add(tf2);
m =new Button("登录");
f.add(m);
m.addActionListener(this);
tf2.addActionListener(this);
f.addWindowListener(new WinClose());
f.setVisible(true); //显示框架
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()!=null && e.getSource()==m )
(new ChatRoomClient()).show();
f.setVisible(false);
}
class WinClose extends WindowAdapter{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main (String arg[])
{
(new Christain2()).display();
}
}
如何在我现有的 ChatServerRoom.java 中实现数据由服务器端传送到客户端(***比如说 “将在线人数返回到客户端界面上ChatClientRoom.java中的(panel3)上***)
由于初学 java 希望大家给个帮助哈!!谢谢
下面是我的程序
登陆界面为:christain2.java
客户端界面为:ChatClientRoom.java (在下页)
服务器端界面为:ChatServerRoom.java (在另外一个页面)
登陆界面为:christain2.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Christain2 implements ActionListener
{ Frame f;
Label t1,t2;
TextField tf1;
Label t3;
TextField tf2;
Button m;
public void display()
{
Frame f;
f = new Frame("chatRoom"); //创建框架并设置标题
f.setSize(600,350); //框架大小
f.setBackground(Color.GREEN);
f.setLayout(new FlowLayout(FlowLayout.LEFT));
t1=new Label("用户名");
t3=new Label("嘟嘟 的 乐 园");
tf1=new TextField("user1",25);
tf1.setEditable(true);
t2=new Label("密码:");
tf2=new TextField(15);
tf2.setEditable(true);
f.add(t3);
f.add(t1);
f.add(tf1);
f.add(t2);
f.add(tf2);
m =new Button("登录");
f.add(m);
m.addActionListener(this);
tf2.addActionListener(this);
f.addWindowListener(new WinClose());
f.setVisible(true); //显示框架
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()!=null && e.getSource()==m )
(new ChatRoomClient()).show();
f.setVisible(false);
}
class WinClose extends WindowAdapter{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main (String arg[])
{
(new Christain2()).display();
}
}
* In order to send a network message, use write(); In order to broadcast a
* network message, use broadcast(); You also have to implement the function
* handleServerMessage().
*/import java.nio.channels.SocketChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.ByteBuffer;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.LinkedList;
import java.io.IOException;public class AsyncServer implements Runnable {
public AsyncServer() {
new Thread(this).start();
} public void run() {
try {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
Selector s = Selector.open();
ssc.socket().bind(new InetSocketAddress(port));
ssc.register(s, SelectionKey.OP_ACCEPT);
System.out.println("echo server has been set up ......");
while (true) {
int n = s.select();
if (n == 0) {
continue;
}
Iterator it = s.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key
.channel();
SocketChannel sc = server.accept();
clientList.add(sc);
sc.configureBlocking(false);
sc.register(s, SelectionKey.OP_READ);
}
if (key.isReadable()) {
handleClientMessage(key);
}
it.remove();
}
}
} catch (Exception e) {
e.printStackTrace();
}
} public String read(SocketChannel sc) {
String returnMsg = "";
int count;
r_buff.clear();
try {
count = sc.read(r_buff);
r_buff.flip();
byte[] temp = new byte[r_buff.limit()];
r_buff.get(temp);
r_buff.clear();
returnMsg = new String(temp);
} catch (IOException ioe) {
System.out.println("read exception.");
disconnect(sc);
}
return returnMsg;
} public void write(SocketChannel sc, String message) {
w_buff.clear();
w_buff.put(message.getBytes());
w_buff.flip();
try {
while (w_buff.hasRemaining())
sc.write(w_buff);
} catch (IOException ioe) {
System.out.println("write exception.");
disconnect(sc);
} } private void broadcast(SocketChannel from, String message) {
Iterator i = clientList.iterator();
while (i.hasNext()) {
SocketChannel channel = (SocketChannel) i.next();
write(channel, message);
}
} public void handleClientMessage(SelectionKey key) {
SocketChannel sc = (SocketChannel) key.channel();
String message = read(sc); //message is here !
System.out.println(message);
broadcast(sc, "I am broadcasting !");
} public void disconnect(SocketChannel sc) {
clientList.remove(sc);
try {
sc.close();
} catch (IOException ioe) {
System.out.println("Can not close SocketChannel .");
} } public static void main(String args[]) {
new AsyncServer();
} private LinkedList clientList = new LinkedList(); private ByteBuffer r_buff = ByteBuffer.allocate(1024); private ByteBuffer w_buff = ByteBuffer.allocate(1024); private static int port = 8848;
}
* You have to create classes to extend this class and implement the function
* handleServerMessage(). In order to send a network message, use write();
*/import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;public class networkHandler {
public class IncomingMessagehandler extends Thread {
public void run() {
while (true) {
try {
handleServerMessage(read());
} catch (Exception e) {
System.exit(0);
}
}
}
} public networkHandler() {
Connect();
} public void Connect() {
try {
InetSocketAddress addr = new InetSocketAddress(host, port);//生成一个socketchannel
sc = SocketChannel.open();//连接到server
sc.connect(addr);
System.out.println("connection has been established!...");
} catch (IOException ioe) {
ioe.printStackTrace();
}
new IncomingMessagehandler().start();
} public String read() throws IOException {
int count;
r_buff.clear();
count = sc.read(r_buff);
r_buff.flip();
byte[] temp = new byte[r_buff.limit()];
r_buff.get(temp);
return new String(temp);
} public void write(String string) throws IOException {
w_buff.clear();
w_buff.put(string.getBytes());
w_buff.flip(); while (w_buff.hasRemaining())
sc.write(w_buff);
} public void handleServerMessage(String message) {
System.out.println(message);
} public static void main(String args[]) {
try {
new networkHandler().write("I love you !");
} catch (IOException e) {
e.printStackTrace();
}
} private SocketChannel sc; private static String host = "127.0.0.1"; private static int port = 8848; private final int MAX_LENGTH = 1024; private ByteBuffer r_buff = ByteBuffer.allocate(MAX_LENGTH); private ByteBuffer w_buff = ByteBuffer.allocate(MAX_LENGTH);
}