📄
字号:
{ Socket client=listen.accept();
firstthread f=new firstthread(this, client);
f.setPriority(Thread.MIN_PRIORITY);f.start();
connections.addElement(f);
}
} catch (IOException e)
{System.err.println("Erro:"+e);
System.exit(1);
}
}
public static void main(String[] args)
{new chatserverthree();
}
public void broadcast(String msg)
{int i;firstthread you;
for (i=0; i<connections.size(); i++)
{you = (firstthread) connections.elementAt(i);
try{you.out.writeUTF(msg);} catch(IOException e) {}
}
}
public void broadcast1(String msg)
{int i;String s1,s2,s3;
firstthread you; s1=new String("PEOPLE");s2=new String(msg.substring(4));
s3=s1.concat(s2);
for (i=0; i<connections.size(); i++)
{you = (firstthread) connections.elementAt(i);
if(s3.startsWith(you.name))
{try{you.out.writeUTF(msg);} catch(IOException e) {}}
}
}
}
class firstthread extends Thread
{protected Socket client;String line,name;int k=0;
protected DataOutputStream firstout,out;
protected chatserverthree server;
protected DataInputStream in;
public firstthread(chatserverthree server, Socket client)
{this.server=server; this.client=client;
try{in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
firstout = new DataOutputStream(client.getOutputStream());
}
catch (IOException e)
{try {server.connections.removeElement(this);
client.close(); } catch (IOException e2)
{System.err.println("有问题哦:" + e);
return;}
}
if(this.client==null)
{ server.broadcast("QUIT"+this.name); this.name=null;}
}
public void run()
{ try
{for(int i=0; i<server.connections.size(); i++)
{ firstthread c=(firstthread) (server.connections.elementAt(i));
if(c.name!=null)
{ try{firstout.writeUTF(c.name);}
catch (IOException e){}
}
}
}
catch(ArrayIndexOutOfBoundsException e) {}
catch(NullPointerException e) {}
try { while(true)
{line=in.readUTF();
if(line.startsWith("PEOPLE"))
{ try{
{ firstthread
d=(firstthread)(
server.connections.elementAt(server.connections.indexOf(this)));
if(d.name==null)
{ d.name=line;
}
else if(d.name!=null)
{ server.broadcast("QUIT"+this.name);
d.name=line;
}
}
}
catch(ArrayIndexOutOfBoundsException e) {}
catch(NullPointerException e) {}
finally {server.broadcast(line);}
}
else if(line.startsWith("MSG"))
{server.broadcast(line);}
else if(line.startsWith("QUIT"))
{ server.broadcast("QUIT"+this.name);
server.connections.removeElement(this);
this.client.close();this.yield();
}
else if(line.startsWith("BADQUIT"))
{ server.broadcast("MSG"+(this.name).substring(6)+"被踢出去了");
server.broadcast("QUIT"+this.name);
server.connections.removeElement(this);
this.client.close();this.yield();
}
else if(line.startsWith("newlist"))
{ try{ for(int i=0; i<server.connections.size(); i++)
{firstthread c=(firstthread) (server.connections.elementAt(i));
if(c.name!=null)
{ try{firstout.writeUTF(c.name);}
catch (IOException e){}
}
}
}
catch(ArrayIndexOutOfBoundsException e) {}
catch(NullPointerException e) {}
}
else if(line.startsWith("悄悄地对"))
{ this.out.writeUTF(line+"*");
server.broadcast1(line);
}
else if(line.startsWith("坏悄悄地对"))
{ this.out.writeUTF(line.substring(1)+"*");
server.broadcast(line.substring(1));
}
}
}
catch (IOException e){ server.connections.removeElement(this);}
catch(NullPointerException e) {server.connections.removeElement(this);}
}
}
21-例子8
import java.net.*;
public class DomainName
{ public static void main(String args[])
{ try{InetAddress address_1=InetAddress.getByName("www.sina.com.cn");
System.out.println(address_1.toString());
InetAddress address_2=InetAddress.getByName("166.111.222.3");
System.out.println(address_2.toString());
}
catch(UnknownHostException e)
{System.out.println("无法找到 www.sina.com.cn");
}
}
}
21-例子9
import java.net.*;
public class DomainName
{ public static void main(String args[])
{ try{InetAddress address=InetAddress.getByName("www.yahoo.com");
String domain_name=address.getHostName();//获取 address所含的域名。
String IP_name=address.getHostAddress(); //获取 address所含的IP地址。
System.out.println(domain_name);
System.out.println(IP_name);
}
catch(UnknownHostException e)
{System.out.println("无法找到 www.yahoo.com");
}
}
}
运行结果:
www.yahoo.com
64.58.76.227
21-例子10
import java.net.*;
public class DomainName
{ public static void main(String args[])
{ try{InetAddress address=InetAddress.getLocalHost();
String domain_name=address.getHostName();//获取 address所含的域名。
String IP_name=address.getHostAddress();//获取 address所含的IP地址。
System.out.println(domain_name);
System.out.println(IP_name);
}
catch(UnknownHostException e)
{
}
}
}
21-例子11
(1)主机1:
import java.net.*;import java.awt.*; import java.awt.event.*;
class Shanghai_Frame extends Frame implements Runnable,ActionListener
{ TextField out_message=new TextField("发送数据到北京:");
TextArea in_message=new TextArea();
Button b=new Button("发送数据包到北京");
byte data[]=new byte[8192];
DatagramPacket pack=null;
Shanghai_Frame()
{ super("我是上海");
setSize(200,200);
setVisible(true);
b.addActionListener(this);
add(out_message,"South");
add(in_message,"Center");
add(b,"North");
pack=new DatagramPacket(data,data.length);
Thread thread=new Thread(this);
thread.start();//线程负责接收数据包
}
//点击按扭发送数据包:
public void actionPerformed(ActionEvent event)
{byte buffer[]=out_message.getText().trim().getBytes();
try{InetAddress address=InetAddress.getByName("localhost");
//数据包的目标端口是888(那么收方(北京)需在这个端口接收):
DatagramPacket data_pack=
new DatagramPacket(buffer,buffer.length, address,888);
DatagramSocket mail_data=new DatagramSocket();
in_message.append("数据报目标主机地址:"+data_pack.getAddress()+"\n");
in_message.append("数据报目标端口是:"+data_pack.getPort()+"\n");
in_message.append("数据报长度:"+data_pack.getLength()+"\n");
mail_data.send(data_pack);
}
catch(Exception e){}
}
//接收数据包:
public void run()
{DatagramSocket mail_data=null;
try{//使用端口666来接收数据包(因为北京发来的数据报的目标端口是666)。
mail_data=new DatagramSocket(666);
}
catch(Exception e){}
while(true)
{ if(mail_data==null) break;
else
try{mail_data.receive(pack);
int length=pack.getLength(); //获取收到的数据的实际长度。
InetAddress adress=pack.getAddress();//获取收到的数据包的始发地址。
int port=pack.getPort();//获取收到的数据包的始发端口。
String message=new String(pack.getData(),0,length);//获取收到的
//数据包中的数据。
in_message.append("收到数据长度:"+length+"\n");
in_message.append("收到数据来自:"+adress+"端口:"+port+"\n");
in_message.append("收到数据是:"+message+"\n");
}
catch(Exception e){}
}
}
}
public class Shanghai
{public static void main(String args[])
{ Shanghai_Frame shanghai_win=new Shanghai_Frame();
shanghai_win.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
shanghai_win.pack();
}
}
(2)主机2:
import java.net.*;import java.awt.*; import java.awt.event.*;
class Beijing_Frame extends Frame implements Runnable,ActionListener
{ TextField out_message=new TextField("发送数据到上海:");
TextArea in_message=new TextArea();
Button b=new Button("发送数据包到上海");
byte data[]=new byte[8192];
DatagramPacket pack=null;
Beijing_Frame()
{ super("我是北京");
setSize(200,200);
setVisible(true);
b.addActionListener(this);
add(out_message,"South");
add(in_message,"Center");
add(b,"North");
pack=new DatagramPacket(data,data.length);//用来收取数据的数据包。
Thread thread=new Thread(this);
thread.start();//线程负责接收数据包
}
//点击按扭发送数据包:
public void actionPerformed(ActionEvent event)
{byte buffer[]=out_message.getText().trim().getBytes();
try{InetAddress address=InetAddress.getByName("localhost");
//数据包的目标端口是666(那么收方(上海)需在这个端口接收):
DatagramPacket data_pack=
new DatagramPacket(buffer,buffer.length, address,666);
DatagramSocket mail_data=new DatagramSocket();
in_message.append("数据报目标主机地址:"+data_pack.getAddress()+"\n");
in_message.append("数据报目标端口是:"+data_pack.getPort()+"\n");
in_message.append("数据报长度:"+data_pack.getLength()+"\n");
mail_data.send(data_pack);
}
catch(Exception e){}
}
public void run()
{DatagramSocket mail_data=null;
try{//使用端口888来接收数据包(因为上海发来的数据报的目标端口是888)。
mail_data=new DatagramSocket(888);
}
catch(Exception e){}
while(true)
{ if(mail_data==null) break;
else
try{ mail_data.receive(pack);
int length=pack.getLength(); //获取收到的数据的实际长度。
InetAddress adress=pack.getAddress();//获取收到的数据包的始发地址。
int port=pack.getPort();//获取收到的数据包的始发端口。
String message=new String(pack.getData(),0,length);//获取收到
//的数据包中的数据。
in_message.append("收到数据长度:"+length+"\n");
in_message.append("收到数据来自:"+adress+"端口:"+port+"\n");
in_message.append("收到数据是:"+message+"\n");
}
catch(Exception e){}
}
}
}
public class Beijing
{public static void main(String args[])
{Beijing_Frame beijing_win=new Beijing_Frame();
beijing_win.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
beijing_win.pack();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -