?? 第二十一章例子.txt
字號:
{ 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 + -