简易聊天室一对多Word格式.docx

上传人:b****5 文档编号:16244461 上传时间:2022-11-21 格式:DOCX 页数:13 大小:549.22KB
下载 相关 举报
简易聊天室一对多Word格式.docx_第1页
第1页 / 共13页
简易聊天室一对多Word格式.docx_第2页
第2页 / 共13页
简易聊天室一对多Word格式.docx_第3页
第3页 / 共13页
简易聊天室一对多Word格式.docx_第4页
第4页 / 共13页
简易聊天室一对多Word格式.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

简易聊天室一对多Word格式.docx

《简易聊天室一对多Word格式.docx》由会员分享,可在线阅读,更多相关《简易聊天室一对多Word格式.docx(13页珍藏版)》请在冰豆网上搜索。

简易聊天室一对多Word格式.docx

//Applet创建一个服务线程

publicstaticvoidmain(Stringargs[])

{

ServerSocketsocket=null;

Vectorm_threads=newVector();

System.out.println("

服务器已启动,正在等待客户的请求..."

);

try

//设置Server监听端口号为8000,这个数字必须

//和程序ChatClient中的port参数一致。

socket=newServerSocket(8000);

}

catch(Exceptione)

服务接口建立失败!

"

return;

intnid=0;

while(true)

//监听是否有新ChatApplet连接到Server,

//线程运行到该语句会封锁,直到有新的连接产生。

Sockets=socket.accept();

//创建一个新的ServerThread.

ServerThread 

st=new 

ServerThread(s,m_threads);

//为该线程设置一个ID号。

st.setID(nid++);

//将该线程加入到m_threadsVector中。

m_threads.addElement(st);

//启动服务线程。

newThread(st).start();

//通知所有ChatApplet有一个新的网友加入。

for(inti=0;

i<

m_threads.size();

i++)

ServerThreadst1=(ServerThread)m_threads.elementAt(i);

st1.write("

<

服务器>

欢迎"

+st.getID()+"

号朋友进入聊天室!

接受"

号客户请求"

继续等待其他客户的请求...\n"

服务器已关闭..."

}

*监听线程,监听对应的ChatApplet是否有信息传来。

classServerThreadimplementsRunnable

Vectorm_threads;

Socketm_socket=null;

DataInputStreamm_in=null;

DataOutputStreamm_out=null;

intm_nid;

//初始化该线程。

publicServerThread(Sockets,Vectorthreads)

m_socket=s;

m_threads=threads;

m_in=newDataInputStream(m_socket.getInputStream());

m_out=newDataOutputStream(m_socket.getOutputStream());

publicvoidrun() 

//线程的执行体。

等待进程正在运行"

//监听对应的Applet是否传来消息

//程序陷入到m_in.readUTF()中,直到有信息传来才返回。

Strings=m_in.readUTF();

if(s==null)

break;

//如果ChatApplet传来的信息为"

leave"

//则通知所有其他的的ChatApplet自己退出了。

if 

(s.trim().equals("

))

for(inti=0;

ServerThreadst=(ServerThread)m_threads.elementAt(i);

st.write("

*各位朋友,"

+getID()+"

号朋友离开聊天室"

+"

*"

else

//向所有ChatApplet广播该信息。

朋友说>

+s);

e.printStackTrace();

//从m_threadsVector中删除该线程,表示该线程已经离开聊天室。

m_threads.removeElement(this);

{m_socket.close();

catch(Exceptione){}

//将msg送回对应的Applet

publicvoidwrite(Stringmsg)

synchronized(m_out)

m_out.writeUTF(msg);

catch(IOExceptione){}

publicintgetID() 

//获得该线程的ID.

returnm_nid;

publicvoidsetID(intnid) 

////设置线程的ID.

m_nid=nid;

客户端用的Applet代码如下:

importjava.awt.*;

importjava.awt.event.*;

importjava.applet.*;

//importcom.borland.jbcl.layout.*;

importjavax.swing.*;

publicclassChatClientAppletextendsAppletimplementsRunnable{

privatebooleanisStandalone=false;

JLabeljLabel1=newJLabel();

JTextFieldjTextField1=newJTextField();

JLabeljLabel2=newJLabel();

JButtonjButton1=newJButton();

DataInputStream 

m_in;

//消息输入流

DataOutputStream 

m_out;

//消息输出流

JScrollPanejScrollPane1=newJScrollPane();

JTextAreajTextArea1=newJTextArea();

JLabeljLabel3=newJLabel();

//Getaparametervalue

publicStringgetParameter(Stringkey,Stringdef){

returnisStandalone?

System.getProperty(key,def):

(getParameter(key)!

=null?

getParameter(key):

def);

//Constructtheapplet

publicChatClientApplet(){

//Initializetheapplet

publicvoidinit(){

m_in=null;

m_out=null;

try{

URL 

url=getCodeBase();

//获取applet的URL值。

//获取服务器IP地址

InetAddress 

inetaddr=InetAddress.getByName(url.getHost());

Socket 

m_socket;

//屏幕显示服务器IP地址、通讯协议

服务器:

+inetaddr+"

"

+url.getHost()+"

+url.getProtocol());

m_socket=newSocket(inetaddr,8000);

//创建与服务器IP地址连接的套接口

//在套接口上建立输入流

m_in=new 

DataInputStream(m_socket.getInputStream());

//在套接口上建立输出流

catch(Exceptione)

Error:

+e);

try{

jbInit();

catch(Exceptione){

//Componentinitialization

privatevoidjbInit()throwsException{

jLabel1.setFont(newjava.awt.Font("

DialogInput"

1,20));

jLabel1.setForeground(Color.red);

jLabel1.setToolTipText("

作者:

杨林森 

Email:

yanglinsen@"

jLabel1.setText("

一对多聊天程序JavaApplet客户端"

jLabel1.setBounds(newRectangle(81,22,358,38));

this.setBackground(UIManager.getColor("

ComboBox.selectionBackground"

));

this.setLayout(null);

jLabel2.setFont(newjava.awt.Font("

Dialog"

1,16));

jLabel2.setForeground(Color.blue);

jLabel2.setText("

输入发送消息:

jLabel2.setBounds(newRectangle(35,78,124,37));

jButton1.setBackground(UIManager.getColor("

OptionPane.questionDialog.titlePane.background"

jButton1.setBounds(newRectangle(413,77,70,39));

jButton1.setFont(newjava.awt.Font("

jButton1.setForeground(Color.blue);

jButton1.setToolTipText("

点击发送按钮即可发送文本框中输入的消息"

jButton1.setText("

发送"

jButton1.addActionListener(newChatClientApplet_jButton1_actionAdapter(this));

jTextField1.setBackground(newColor(182,231,223));

jTextField1.setFont(newjava.awt.Font("

0,17));

jTextField1.setToolTipText("

此文本框用来输入发送的消息"

jTextField1.setBounds(newRectangle(153,80,248,35));

jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

jScrollPane1.getViewport().setBackground(UIManager.getColor("

MenuItem.acceleratorForeground"

jScrollPane1.setBounds(newRectangle(54,130,410,231));

jTextArea1.setBackground(UIManager.getColor("

Desktop.background"

jTextArea1.setFont(newjava.awt.Font("

0,18));

jTextArea1.setForeground(Color.black);

jLabel3.setFont(newjava.awt.Font("

0,16));

jLabel3.setForeground(Color.blue);

jLabel3.setText("

如想离开聊天室,可在发送框中输入字符串leave并发送"

jLabel3.setBounds(newRectangle(62,367,394,24));

this.add(jLabel1,null);

this.add(jLabel2,null);

this.add(jTextField1,null);

this.add(jButton1,null);

this.add(jScrollPane1,null);

this.add(jLabel3,null);

jScrollPane1.getViewport().add(jTextArea1,null);

//启动监听线程

newThread(this).start();

//GetAppletinformation

publicStringgetAppletInfo(){

return"

AppletInformation"

;

//Getparameterinfo

publicString[][]getParameterInfo(){

returnnull;

publicvoidrun()

//监听服务者发来的消息,线程将阻塞在该语句中,直到消息到来。

//读一个UTF格式字符串。

if(s!

=null)

//将消息显示在信息显示窗口中。

jTextArea1.append(s+"

\n"

jTextArea1.append("

NetworkproblemorSeverdown.\n"

jTextArea1.setVisible(false);

publicvoidstop()

m_out.writeUTF("

//如想离开聊天室,可在发送框中输入字符串leave

voidjButton1_actionPerformed(ActionEvente){

Stringb=jTextField1.getText();

jTextField1.setText("

//将用户输入的消息发送给ChatServer

m_out.writeUTF(b);

//向服务者发送一UTF格式字符串。

catch(IOExceptiong){}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 水产渔业

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1