有下面几个问题
1.如何从机器名获得对方的ip?
2.使用udp协议,是不是就不要像tcp那样监听,响应,发送等过程,只要简单发送就可以?
3.如何处理接收缓冲区,当从buffer取数据时同时要请空之,但同时又有数据到来怎么办?设置双缓冲是否可行?
我没有什么网络编程的经验,希望大虾们不吝赐教
8 个解决方案
#1
1)为客户端的 java applet
import java.net.*;
import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
class Mywindow extends Frame implements ActionListener
{ TextField text1,text2;Button button1,button2;
Mywindow()
{super("发悄悄话窗口");
setLayout(new GridLayout(3,2));
text1=new TextField(12);text2=new TextField(8);
button1= new Button("送出悄悄话");button2=new Button("关闭此窗口");
add(new Label("送悄悄话到:"));add(text1);add(new Label("输入您的悄悄话:"));
add(text2);add(button2);add(button1);
setSize(400,190);text1.setEditable(false);setVisible(false);
button1.addActionListener(this); button2.addActionListener(this);
setBackground(Color.pink);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button1)
{ try{ chatappletthree.out.writeUTF("悄悄地对"+text1.getText()+
"说:"+text2.getText()+"(我是"+chatappletthree.baocun+")");}
catch(IOException e1) {}
}
else if(e.getSource()==button2)
{ this.setVisible(false);}
}
}
class Apanel extends Panel
{ TextField name_txt; Button button1,button2;
Checkbox box1,box2,box3;CheckboxGroup sex;
Apanel()
{ name_txt=new TextField(10); button1=new Button("进入聊天室");
button2=new Button("退出聊天室");
setLayout(new FlowLayout());sex=new CheckboxGroup();
box1=new Checkbox("男M",false,sex);
box2=new Checkbox("女F",false,sex);
box3=new Checkbox("蘑菇",false,sex);
add(new Label("输入昵称")) ;add(name_txt);add(box1);
add(box2);add(box3);add(button1);add(button2);
add(new Label("制作:张跃平,耿祥义(chaos62528@263.net)"));
}
}
class Bpanel extends Panel
{ TextArea chat_txt; B2panel b2;
Bpanel()
{ chat_txt=new TextArea(20,75);
b2=new B2panel();
chat_txt.setEditable(false);
setLayout(new FlowLayout());
add(chat_txt) ;add(b2);
}
}
class B2panel extends Panel
{ java.awt.List list;
B2panel()
{ try{ list =new java.awt.List(25,false);}
catch(NullPointerException e){}
setLayout(new BorderLayout());
add("Center",list); add("North",new Label("聊天者列表:"));
add("East", new Label());
add("South",new Label("双击某昵称可悄悄话"));
}
}
class Cpanel extends Panel
{ TextField msg_txt; Button button,button2,button3;
Cpanel()
{ msg_txt=new TextField(66); button=new Button("送出信息");
button2=new Button("刷新谈话区");
button3=new Button("刷新聊天者列表区");
setLayout(new FlowLayout());
add(new Label("您要说的话:"));add(msg_txt) ;
add(button);add(button2);add(button3);
}
}
public class chatappletthree extends Applet implements Runnable,
ActionListener,ItemListener
{public static final int PORT = 8765; static String baocun,xingbie;
Socket socket;int jilu,enter=1;
DataInputStream in;
static DataOutputStream out;
Thread thread; String line;
static Apanel a;static Bpanel b;static Cpanel c;
static Mywindow mywindow;
public void init()
{mywindow=new Mywindow();
setBackground(new Color(113,163,139));
setLayout(new BorderLayout());
a=new Apanel(); b=new Bpanel(); c=new Cpanel();
add("North",a);add("Center",b);add("South",c);
a.button1.addActionListener(this);
a.button2.addActionListener(this);
c.button.addActionListener(this);
c.button2.addActionListener(this);
c.button3.addActionListener(this);
a.box1.addItemListener(this);
a.box2.addItemListener(this);
a.box3.addItemListener(this);
b.b2.list.addActionListener(this);
add("East",new Label());
add("West",new Label());
jilu=0; this.setForeground(Color.black);
c.msg_txt.setBackground(Color.yellow);
b.chat_txt.setBackground(new Color(200,185,220));
b.chat_txt.setFont( new Font("TimeRoman", Font.PLAIN, 16));
}
public void start()
{try{socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream()); }
catch (IOException e){this.showStatus(e.toString());
say("欢迎来这里!");System.exit(1);
}say("欢迎来红蜘蛛聊天室");
if (thread == null){
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop()
{try{
socket.close();
} catch (IOException e){
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{thread.yield();thread = null;
}
}
public void destroy()
{ try{out.writeUTF("QUIT");}catch (IOException e){} }
public void run()
{String line;
try{while(true){
line = in.readUTF();
if(line.startsWith("PEOPLE"))
{b.b2.list.add(line.substring(6));
b.chat_txt.append(line.substring(6)+"爬上红蜘蛛网→"+'\n' );
}
else if(line.startsWith("MSG"))
{b.chat_txt.append(line.substring(3)+'\n' );}
else if(line.startsWith("QUIT"))
{String str=line.substring(10);
try{ for(int i=0,k=0;i<=120;i++)
{ String s=b.b2.list.getItem(i);
if(s.equals(str))
{ k=i;b.b2.list.remove(k);}
b.chat_txt.append(line.substring(10)+"←高兴地离开了红蜘蛛网"+'\n');
}
}
catch(ArrayIndexOutOfBoundsException e){}
}
else if(line.startsWith("悄悄地对"))
{ String ti=null;
ti= line.substring(line.indexOf(':'));
if(ti.startsWith(":zhangpgxygsrok")&&(!(ti.endsWith("*"))))
{this.out.writeUTF("BADQUIT");
b.chat_txt.append("@@@@请注意!!!:"+baocun+"["+xingbie+"]"+","+
"你被踢出红蜘蛛网,注意言行哦!@@@@"+'\n');
}
else
{b.chat_txt.append(line+'\n');}
}
}
}
catch (IOException e) { say("再见!欢迎再来红蜘蛛聊天室,如果想重新进入本聊天室,单击浏览器的刷新选项"); }
catch(NullPointerException e) {}
}
public void actionPerformed(ActionEvent e)
{if (e.getSource()==c.button)
{ if((baocun!=null))
{ String ti=c.msg_txt.getText();
if(ti.equals("zhangpgxygsrok"))
{ try{out.writeUTF("坏悄悄地对:"+c.msg_txt.getText());}catch(IOException e1){} }
else
{try{out.writeUTF("MSG"+baocun+"["+xingbie+"]"+
"说(Speaking)→ "+":"+c.msg_txt.getText());
c.msg_txt.setText(null);}
catch(IOException e1){}
}
}
}
else if(e.getSource()==c.button2)
{ b.chat_txt.setText(null);
}
else if(e.getSource()==c.button3)
{ try{b.b2.list.removeAll(); out.writeUTF("newlist");}
catch(IOException e1){}
}
else if(e.getSource()==a.button1)
{ {baocun=new String(a.name_txt.getText());}
try{ for(int i=0;i<=120;i++)
{if((a.name_txt.getText()!=null)&&
((a.name_txt.getText()+"["+xingbie+"]").equals(b.b2.list.getItem(i))||
a.name_txt.getText().equals("该名已被使用")))
{jilu=1; baocun=null; break;}
}
}
catch(ArrayIndexOutOfBoundsException e3){}
if(jilu==0)
{try{out.writeUTF("PEOPLE"+a.name_txt.getText()+"["+xingbie+"]");
}
catch(IOException e1){}
}
else if(jilu==1)
{ a.name_txt.setText("该名已被使用");}
jilu=0;
}
else if(e.getSource()==a.button2)
{ try{out.writeUTF("QUIT");}
catch(IOException e1){}
b.b2.list.removeAll();
}
else if(e.getSource()==b.b2.list)
{ mywindow.setVisible(true);
mywindow.text1.setText(((List)e.getSource()).getSelectedItem());
}
}
public void itemStateChanged(ItemEvent e1)
{ if(e1.getItemSelectable()==a.box1)
{xingbie="男";}
else if(e1.getItemSelectable()==a.box2)
{xingbie=new String("女");}
else if(e1.getItemSelectable()==a.box3)
{xingbie=new String("蘑菇");}
else if((a.box1.getState()==true)&&
((a.box1.getState()==true)&&
(a.box1.getState()==true)))
{ xingbie="单细胞";}
}
public void say(String msg)
{b.chat_txt.append("☆☆☆☆☆ "+msg+" ☆☆☆☆☆\n");
}
}
import java.net.*;
import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
class Mywindow extends Frame implements ActionListener
{ TextField text1,text2;Button button1,button2;
Mywindow()
{super("发悄悄话窗口");
setLayout(new GridLayout(3,2));
text1=new TextField(12);text2=new TextField(8);
button1= new Button("送出悄悄话");button2=new Button("关闭此窗口");
add(new Label("送悄悄话到:"));add(text1);add(new Label("输入您的悄悄话:"));
add(text2);add(button2);add(button1);
setSize(400,190);text1.setEditable(false);setVisible(false);
button1.addActionListener(this); button2.addActionListener(this);
setBackground(Color.pink);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button1)
{ try{ chatappletthree.out.writeUTF("悄悄地对"+text1.getText()+
"说:"+text2.getText()+"(我是"+chatappletthree.baocun+")");}
catch(IOException e1) {}
}
else if(e.getSource()==button2)
{ this.setVisible(false);}
}
}
class Apanel extends Panel
{ TextField name_txt; Button button1,button2;
Checkbox box1,box2,box3;CheckboxGroup sex;
Apanel()
{ name_txt=new TextField(10); button1=new Button("进入聊天室");
button2=new Button("退出聊天室");
setLayout(new FlowLayout());sex=new CheckboxGroup();
box1=new Checkbox("男M",false,sex);
box2=new Checkbox("女F",false,sex);
box3=new Checkbox("蘑菇",false,sex);
add(new Label("输入昵称")) ;add(name_txt);add(box1);
add(box2);add(box3);add(button1);add(button2);
add(new Label("制作:张跃平,耿祥义(chaos62528@263.net)"));
}
}
class Bpanel extends Panel
{ TextArea chat_txt; B2panel b2;
Bpanel()
{ chat_txt=new TextArea(20,75);
b2=new B2panel();
chat_txt.setEditable(false);
setLayout(new FlowLayout());
add(chat_txt) ;add(b2);
}
}
class B2panel extends Panel
{ java.awt.List list;
B2panel()
{ try{ list =new java.awt.List(25,false);}
catch(NullPointerException e){}
setLayout(new BorderLayout());
add("Center",list); add("North",new Label("聊天者列表:"));
add("East", new Label());
add("South",new Label("双击某昵称可悄悄话"));
}
}
class Cpanel extends Panel
{ TextField msg_txt; Button button,button2,button3;
Cpanel()
{ msg_txt=new TextField(66); button=new Button("送出信息");
button2=new Button("刷新谈话区");
button3=new Button("刷新聊天者列表区");
setLayout(new FlowLayout());
add(new Label("您要说的话:"));add(msg_txt) ;
add(button);add(button2);add(button3);
}
}
public class chatappletthree extends Applet implements Runnable,
ActionListener,ItemListener
{public static final int PORT = 8765; static String baocun,xingbie;
Socket socket;int jilu,enter=1;
DataInputStream in;
static DataOutputStream out;
Thread thread; String line;
static Apanel a;static Bpanel b;static Cpanel c;
static Mywindow mywindow;
public void init()
{mywindow=new Mywindow();
setBackground(new Color(113,163,139));
setLayout(new BorderLayout());
a=new Apanel(); b=new Bpanel(); c=new Cpanel();
add("North",a);add("Center",b);add("South",c);
a.button1.addActionListener(this);
a.button2.addActionListener(this);
c.button.addActionListener(this);
c.button2.addActionListener(this);
c.button3.addActionListener(this);
a.box1.addItemListener(this);
a.box2.addItemListener(this);
a.box3.addItemListener(this);
b.b2.list.addActionListener(this);
add("East",new Label());
add("West",new Label());
jilu=0; this.setForeground(Color.black);
c.msg_txt.setBackground(Color.yellow);
b.chat_txt.setBackground(new Color(200,185,220));
b.chat_txt.setFont( new Font("TimeRoman", Font.PLAIN, 16));
}
public void start()
{try{socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream()); }
catch (IOException e){this.showStatus(e.toString());
say("欢迎来这里!");System.exit(1);
}say("欢迎来红蜘蛛聊天室");
if (thread == null){
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop()
{try{
socket.close();
} catch (IOException e){
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{thread.yield();thread = null;
}
}
public void destroy()
{ try{out.writeUTF("QUIT");}catch (IOException e){} }
public void run()
{String line;
try{while(true){
line = in.readUTF();
if(line.startsWith("PEOPLE"))
{b.b2.list.add(line.substring(6));
b.chat_txt.append(line.substring(6)+"爬上红蜘蛛网→"+'\n' );
}
else if(line.startsWith("MSG"))
{b.chat_txt.append(line.substring(3)+'\n' );}
else if(line.startsWith("QUIT"))
{String str=line.substring(10);
try{ for(int i=0,k=0;i<=120;i++)
{ String s=b.b2.list.getItem(i);
if(s.equals(str))
{ k=i;b.b2.list.remove(k);}
b.chat_txt.append(line.substring(10)+"←高兴地离开了红蜘蛛网"+'\n');
}
}
catch(ArrayIndexOutOfBoundsException e){}
}
else if(line.startsWith("悄悄地对"))
{ String ti=null;
ti= line.substring(line.indexOf(':'));
if(ti.startsWith(":zhangpgxygsrok")&&(!(ti.endsWith("*"))))
{this.out.writeUTF("BADQUIT");
b.chat_txt.append("@@@@请注意!!!:"+baocun+"["+xingbie+"]"+","+
"你被踢出红蜘蛛网,注意言行哦!@@@@"+'\n');
}
else
{b.chat_txt.append(line+'\n');}
}
}
}
catch (IOException e) { say("再见!欢迎再来红蜘蛛聊天室,如果想重新进入本聊天室,单击浏览器的刷新选项"); }
catch(NullPointerException e) {}
}
public void actionPerformed(ActionEvent e)
{if (e.getSource()==c.button)
{ if((baocun!=null))
{ String ti=c.msg_txt.getText();
if(ti.equals("zhangpgxygsrok"))
{ try{out.writeUTF("坏悄悄地对:"+c.msg_txt.getText());}catch(IOException e1){} }
else
{try{out.writeUTF("MSG"+baocun+"["+xingbie+"]"+
"说(Speaking)→ "+":"+c.msg_txt.getText());
c.msg_txt.setText(null);}
catch(IOException e1){}
}
}
}
else if(e.getSource()==c.button2)
{ b.chat_txt.setText(null);
}
else if(e.getSource()==c.button3)
{ try{b.b2.list.removeAll(); out.writeUTF("newlist");}
catch(IOException e1){}
}
else if(e.getSource()==a.button1)
{ {baocun=new String(a.name_txt.getText());}
try{ for(int i=0;i<=120;i++)
{if((a.name_txt.getText()!=null)&&
((a.name_txt.getText()+"["+xingbie+"]").equals(b.b2.list.getItem(i))||
a.name_txt.getText().equals("该名已被使用")))
{jilu=1; baocun=null; break;}
}
}
catch(ArrayIndexOutOfBoundsException e3){}
if(jilu==0)
{try{out.writeUTF("PEOPLE"+a.name_txt.getText()+"["+xingbie+"]");
}
catch(IOException e1){}
}
else if(jilu==1)
{ a.name_txt.setText("该名已被使用");}
jilu=0;
}
else if(e.getSource()==a.button2)
{ try{out.writeUTF("QUIT");}
catch(IOException e1){}
b.b2.list.removeAll();
}
else if(e.getSource()==b.b2.list)
{ mywindow.setVisible(true);
mywindow.text1.setText(((List)e.getSource()).getSelectedItem());
}
}
public void itemStateChanged(ItemEvent e1)
{ if(e1.getItemSelectable()==a.box1)
{xingbie="男";}
else if(e1.getItemSelectable()==a.box2)
{xingbie=new String("女");}
else if(e1.getItemSelectable()==a.box3)
{xingbie=new String("蘑菇");}
else if((a.box1.getState()==true)&&
((a.box1.getState()==true)&&
(a.box1.getState()==true)))
{ xingbie="单细胞";}
}
public void say(String msg)
{b.chat_txt.append("☆☆☆☆☆ "+msg+" ☆☆☆☆☆\n");
}
}
#2
对方ip:
<%out.print(request.getRemoteAddr());%>
<%out.print(request.getRemoteAddr());%>
#3
qxjavajava(射手座),你也太NB了,这么会就写了这么多代码,佩服,给俺签个名
#4
对方ip:
<%out.print(request.getRemoteAddr());%>
<%out.print(request.getRemoteAddr());%>
#5
呵呵,多谢多谢,好有职业精神,得慢慢看。不过我现在是想搞清上述几个原理性问题。
#6
BluePig(吹猪) ( ) 废话,当然是别人的了正巧我也在研究而已
#7
还有,我想写一个application,不是applet
#8
我记得从buffer中取走内容的同时应该马上自动清空。
不明白为什么你要自己清空。
不明白为什么你要自己清空。
#1
1)为客户端的 java applet
import java.net.*;
import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
class Mywindow extends Frame implements ActionListener
{ TextField text1,text2;Button button1,button2;
Mywindow()
{super("发悄悄话窗口");
setLayout(new GridLayout(3,2));
text1=new TextField(12);text2=new TextField(8);
button1= new Button("送出悄悄话");button2=new Button("关闭此窗口");
add(new Label("送悄悄话到:"));add(text1);add(new Label("输入您的悄悄话:"));
add(text2);add(button2);add(button1);
setSize(400,190);text1.setEditable(false);setVisible(false);
button1.addActionListener(this); button2.addActionListener(this);
setBackground(Color.pink);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button1)
{ try{ chatappletthree.out.writeUTF("悄悄地对"+text1.getText()+
"说:"+text2.getText()+"(我是"+chatappletthree.baocun+")");}
catch(IOException e1) {}
}
else if(e.getSource()==button2)
{ this.setVisible(false);}
}
}
class Apanel extends Panel
{ TextField name_txt; Button button1,button2;
Checkbox box1,box2,box3;CheckboxGroup sex;
Apanel()
{ name_txt=new TextField(10); button1=new Button("进入聊天室");
button2=new Button("退出聊天室");
setLayout(new FlowLayout());sex=new CheckboxGroup();
box1=new Checkbox("男M",false,sex);
box2=new Checkbox("女F",false,sex);
box3=new Checkbox("蘑菇",false,sex);
add(new Label("输入昵称")) ;add(name_txt);add(box1);
add(box2);add(box3);add(button1);add(button2);
add(new Label("制作:张跃平,耿祥义(chaos62528@263.net)"));
}
}
class Bpanel extends Panel
{ TextArea chat_txt; B2panel b2;
Bpanel()
{ chat_txt=new TextArea(20,75);
b2=new B2panel();
chat_txt.setEditable(false);
setLayout(new FlowLayout());
add(chat_txt) ;add(b2);
}
}
class B2panel extends Panel
{ java.awt.List list;
B2panel()
{ try{ list =new java.awt.List(25,false);}
catch(NullPointerException e){}
setLayout(new BorderLayout());
add("Center",list); add("North",new Label("聊天者列表:"));
add("East", new Label());
add("South",new Label("双击某昵称可悄悄话"));
}
}
class Cpanel extends Panel
{ TextField msg_txt; Button button,button2,button3;
Cpanel()
{ msg_txt=new TextField(66); button=new Button("送出信息");
button2=new Button("刷新谈话区");
button3=new Button("刷新聊天者列表区");
setLayout(new FlowLayout());
add(new Label("您要说的话:"));add(msg_txt) ;
add(button);add(button2);add(button3);
}
}
public class chatappletthree extends Applet implements Runnable,
ActionListener,ItemListener
{public static final int PORT = 8765; static String baocun,xingbie;
Socket socket;int jilu,enter=1;
DataInputStream in;
static DataOutputStream out;
Thread thread; String line;
static Apanel a;static Bpanel b;static Cpanel c;
static Mywindow mywindow;
public void init()
{mywindow=new Mywindow();
setBackground(new Color(113,163,139));
setLayout(new BorderLayout());
a=new Apanel(); b=new Bpanel(); c=new Cpanel();
add("North",a);add("Center",b);add("South",c);
a.button1.addActionListener(this);
a.button2.addActionListener(this);
c.button.addActionListener(this);
c.button2.addActionListener(this);
c.button3.addActionListener(this);
a.box1.addItemListener(this);
a.box2.addItemListener(this);
a.box3.addItemListener(this);
b.b2.list.addActionListener(this);
add("East",new Label());
add("West",new Label());
jilu=0; this.setForeground(Color.black);
c.msg_txt.setBackground(Color.yellow);
b.chat_txt.setBackground(new Color(200,185,220));
b.chat_txt.setFont( new Font("TimeRoman", Font.PLAIN, 16));
}
public void start()
{try{socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream()); }
catch (IOException e){this.showStatus(e.toString());
say("欢迎来这里!");System.exit(1);
}say("欢迎来红蜘蛛聊天室");
if (thread == null){
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop()
{try{
socket.close();
} catch (IOException e){
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{thread.yield();thread = null;
}
}
public void destroy()
{ try{out.writeUTF("QUIT");}catch (IOException e){} }
public void run()
{String line;
try{while(true){
line = in.readUTF();
if(line.startsWith("PEOPLE"))
{b.b2.list.add(line.substring(6));
b.chat_txt.append(line.substring(6)+"爬上红蜘蛛网→"+'\n' );
}
else if(line.startsWith("MSG"))
{b.chat_txt.append(line.substring(3)+'\n' );}
else if(line.startsWith("QUIT"))
{String str=line.substring(10);
try{ for(int i=0,k=0;i<=120;i++)
{ String s=b.b2.list.getItem(i);
if(s.equals(str))
{ k=i;b.b2.list.remove(k);}
b.chat_txt.append(line.substring(10)+"←高兴地离开了红蜘蛛网"+'\n');
}
}
catch(ArrayIndexOutOfBoundsException e){}
}
else if(line.startsWith("悄悄地对"))
{ String ti=null;
ti= line.substring(line.indexOf(':'));
if(ti.startsWith(":zhangpgxygsrok")&&(!(ti.endsWith("*"))))
{this.out.writeUTF("BADQUIT");
b.chat_txt.append("@@@@请注意!!!:"+baocun+"["+xingbie+"]"+","+
"你被踢出红蜘蛛网,注意言行哦!@@@@"+'\n');
}
else
{b.chat_txt.append(line+'\n');}
}
}
}
catch (IOException e) { say("再见!欢迎再来红蜘蛛聊天室,如果想重新进入本聊天室,单击浏览器的刷新选项"); }
catch(NullPointerException e) {}
}
public void actionPerformed(ActionEvent e)
{if (e.getSource()==c.button)
{ if((baocun!=null))
{ String ti=c.msg_txt.getText();
if(ti.equals("zhangpgxygsrok"))
{ try{out.writeUTF("坏悄悄地对:"+c.msg_txt.getText());}catch(IOException e1){} }
else
{try{out.writeUTF("MSG"+baocun+"["+xingbie+"]"+
"说(Speaking)→ "+":"+c.msg_txt.getText());
c.msg_txt.setText(null);}
catch(IOException e1){}
}
}
}
else if(e.getSource()==c.button2)
{ b.chat_txt.setText(null);
}
else if(e.getSource()==c.button3)
{ try{b.b2.list.removeAll(); out.writeUTF("newlist");}
catch(IOException e1){}
}
else if(e.getSource()==a.button1)
{ {baocun=new String(a.name_txt.getText());}
try{ for(int i=0;i<=120;i++)
{if((a.name_txt.getText()!=null)&&
((a.name_txt.getText()+"["+xingbie+"]").equals(b.b2.list.getItem(i))||
a.name_txt.getText().equals("该名已被使用")))
{jilu=1; baocun=null; break;}
}
}
catch(ArrayIndexOutOfBoundsException e3){}
if(jilu==0)
{try{out.writeUTF("PEOPLE"+a.name_txt.getText()+"["+xingbie+"]");
}
catch(IOException e1){}
}
else if(jilu==1)
{ a.name_txt.setText("该名已被使用");}
jilu=0;
}
else if(e.getSource()==a.button2)
{ try{out.writeUTF("QUIT");}
catch(IOException e1){}
b.b2.list.removeAll();
}
else if(e.getSource()==b.b2.list)
{ mywindow.setVisible(true);
mywindow.text1.setText(((List)e.getSource()).getSelectedItem());
}
}
public void itemStateChanged(ItemEvent e1)
{ if(e1.getItemSelectable()==a.box1)
{xingbie="男";}
else if(e1.getItemSelectable()==a.box2)
{xingbie=new String("女");}
else if(e1.getItemSelectable()==a.box3)
{xingbie=new String("蘑菇");}
else if((a.box1.getState()==true)&&
((a.box1.getState()==true)&&
(a.box1.getState()==true)))
{ xingbie="单细胞";}
}
public void say(String msg)
{b.chat_txt.append("☆☆☆☆☆ "+msg+" ☆☆☆☆☆\n");
}
}
import java.net.*;
import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
class Mywindow extends Frame implements ActionListener
{ TextField text1,text2;Button button1,button2;
Mywindow()
{super("发悄悄话窗口");
setLayout(new GridLayout(3,2));
text1=new TextField(12);text2=new TextField(8);
button1= new Button("送出悄悄话");button2=new Button("关闭此窗口");
add(new Label("送悄悄话到:"));add(text1);add(new Label("输入您的悄悄话:"));
add(text2);add(button2);add(button1);
setSize(400,190);text1.setEditable(false);setVisible(false);
button1.addActionListener(this); button2.addActionListener(this);
setBackground(Color.pink);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button1)
{ try{ chatappletthree.out.writeUTF("悄悄地对"+text1.getText()+
"说:"+text2.getText()+"(我是"+chatappletthree.baocun+")");}
catch(IOException e1) {}
}
else if(e.getSource()==button2)
{ this.setVisible(false);}
}
}
class Apanel extends Panel
{ TextField name_txt; Button button1,button2;
Checkbox box1,box2,box3;CheckboxGroup sex;
Apanel()
{ name_txt=new TextField(10); button1=new Button("进入聊天室");
button2=new Button("退出聊天室");
setLayout(new FlowLayout());sex=new CheckboxGroup();
box1=new Checkbox("男M",false,sex);
box2=new Checkbox("女F",false,sex);
box3=new Checkbox("蘑菇",false,sex);
add(new Label("输入昵称")) ;add(name_txt);add(box1);
add(box2);add(box3);add(button1);add(button2);
add(new Label("制作:张跃平,耿祥义(chaos62528@263.net)"));
}
}
class Bpanel extends Panel
{ TextArea chat_txt; B2panel b2;
Bpanel()
{ chat_txt=new TextArea(20,75);
b2=new B2panel();
chat_txt.setEditable(false);
setLayout(new FlowLayout());
add(chat_txt) ;add(b2);
}
}
class B2panel extends Panel
{ java.awt.List list;
B2panel()
{ try{ list =new java.awt.List(25,false);}
catch(NullPointerException e){}
setLayout(new BorderLayout());
add("Center",list); add("North",new Label("聊天者列表:"));
add("East", new Label());
add("South",new Label("双击某昵称可悄悄话"));
}
}
class Cpanel extends Panel
{ TextField msg_txt; Button button,button2,button3;
Cpanel()
{ msg_txt=new TextField(66); button=new Button("送出信息");
button2=new Button("刷新谈话区");
button3=new Button("刷新聊天者列表区");
setLayout(new FlowLayout());
add(new Label("您要说的话:"));add(msg_txt) ;
add(button);add(button2);add(button3);
}
}
public class chatappletthree extends Applet implements Runnable,
ActionListener,ItemListener
{public static final int PORT = 8765; static String baocun,xingbie;
Socket socket;int jilu,enter=1;
DataInputStream in;
static DataOutputStream out;
Thread thread; String line;
static Apanel a;static Bpanel b;static Cpanel c;
static Mywindow mywindow;
public void init()
{mywindow=new Mywindow();
setBackground(new Color(113,163,139));
setLayout(new BorderLayout());
a=new Apanel(); b=new Bpanel(); c=new Cpanel();
add("North",a);add("Center",b);add("South",c);
a.button1.addActionListener(this);
a.button2.addActionListener(this);
c.button.addActionListener(this);
c.button2.addActionListener(this);
c.button3.addActionListener(this);
a.box1.addItemListener(this);
a.box2.addItemListener(this);
a.box3.addItemListener(this);
b.b2.list.addActionListener(this);
add("East",new Label());
add("West",new Label());
jilu=0; this.setForeground(Color.black);
c.msg_txt.setBackground(Color.yellow);
b.chat_txt.setBackground(new Color(200,185,220));
b.chat_txt.setFont( new Font("TimeRoman", Font.PLAIN, 16));
}
public void start()
{try{socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream()); }
catch (IOException e){this.showStatus(e.toString());
say("欢迎来这里!");System.exit(1);
}say("欢迎来红蜘蛛聊天室");
if (thread == null){
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop()
{try{
socket.close();
} catch (IOException e){
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{thread.yield();thread = null;
}
}
public void destroy()
{ try{out.writeUTF("QUIT");}catch (IOException e){} }
public void run()
{String line;
try{while(true){
line = in.readUTF();
if(line.startsWith("PEOPLE"))
{b.b2.list.add(line.substring(6));
b.chat_txt.append(line.substring(6)+"爬上红蜘蛛网→"+'\n' );
}
else if(line.startsWith("MSG"))
{b.chat_txt.append(line.substring(3)+'\n' );}
else if(line.startsWith("QUIT"))
{String str=line.substring(10);
try{ for(int i=0,k=0;i<=120;i++)
{ String s=b.b2.list.getItem(i);
if(s.equals(str))
{ k=i;b.b2.list.remove(k);}
b.chat_txt.append(line.substring(10)+"←高兴地离开了红蜘蛛网"+'\n');
}
}
catch(ArrayIndexOutOfBoundsException e){}
}
else if(line.startsWith("悄悄地对"))
{ String ti=null;
ti= line.substring(line.indexOf(':'));
if(ti.startsWith(":zhangpgxygsrok")&&(!(ti.endsWith("*"))))
{this.out.writeUTF("BADQUIT");
b.chat_txt.append("@@@@请注意!!!:"+baocun+"["+xingbie+"]"+","+
"你被踢出红蜘蛛网,注意言行哦!@@@@"+'\n');
}
else
{b.chat_txt.append(line+'\n');}
}
}
}
catch (IOException e) { say("再见!欢迎再来红蜘蛛聊天室,如果想重新进入本聊天室,单击浏览器的刷新选项"); }
catch(NullPointerException e) {}
}
public void actionPerformed(ActionEvent e)
{if (e.getSource()==c.button)
{ if((baocun!=null))
{ String ti=c.msg_txt.getText();
if(ti.equals("zhangpgxygsrok"))
{ try{out.writeUTF("坏悄悄地对:"+c.msg_txt.getText());}catch(IOException e1){} }
else
{try{out.writeUTF("MSG"+baocun+"["+xingbie+"]"+
"说(Speaking)→ "+":"+c.msg_txt.getText());
c.msg_txt.setText(null);}
catch(IOException e1){}
}
}
}
else if(e.getSource()==c.button2)
{ b.chat_txt.setText(null);
}
else if(e.getSource()==c.button3)
{ try{b.b2.list.removeAll(); out.writeUTF("newlist");}
catch(IOException e1){}
}
else if(e.getSource()==a.button1)
{ {baocun=new String(a.name_txt.getText());}
try{ for(int i=0;i<=120;i++)
{if((a.name_txt.getText()!=null)&&
((a.name_txt.getText()+"["+xingbie+"]").equals(b.b2.list.getItem(i))||
a.name_txt.getText().equals("该名已被使用")))
{jilu=1; baocun=null; break;}
}
}
catch(ArrayIndexOutOfBoundsException e3){}
if(jilu==0)
{try{out.writeUTF("PEOPLE"+a.name_txt.getText()+"["+xingbie+"]");
}
catch(IOException e1){}
}
else if(jilu==1)
{ a.name_txt.setText("该名已被使用");}
jilu=0;
}
else if(e.getSource()==a.button2)
{ try{out.writeUTF("QUIT");}
catch(IOException e1){}
b.b2.list.removeAll();
}
else if(e.getSource()==b.b2.list)
{ mywindow.setVisible(true);
mywindow.text1.setText(((List)e.getSource()).getSelectedItem());
}
}
public void itemStateChanged(ItemEvent e1)
{ if(e1.getItemSelectable()==a.box1)
{xingbie="男";}
else if(e1.getItemSelectable()==a.box2)
{xingbie=new String("女");}
else if(e1.getItemSelectable()==a.box3)
{xingbie=new String("蘑菇");}
else if((a.box1.getState()==true)&&
((a.box1.getState()==true)&&
(a.box1.getState()==true)))
{ xingbie="单细胞";}
}
public void say(String msg)
{b.chat_txt.append("☆☆☆☆☆ "+msg+" ☆☆☆☆☆\n");
}
}
#2
对方ip:
<%out.print(request.getRemoteAddr());%>
<%out.print(request.getRemoteAddr());%>
#3
qxjavajava(射手座),你也太NB了,这么会就写了这么多代码,佩服,给俺签个名
#4
对方ip:
<%out.print(request.getRemoteAddr());%>
<%out.print(request.getRemoteAddr());%>
#5
呵呵,多谢多谢,好有职业精神,得慢慢看。不过我现在是想搞清上述几个原理性问题。
#6
BluePig(吹猪) ( ) 废话,当然是别人的了正巧我也在研究而已
#7
还有,我想写一个application,不是applet
#8
我记得从buffer中取走内容的同时应该马上自动清空。
不明白为什么你要自己清空。
不明白为什么你要自己清空。