下面是一个《Java Network Programming》上面的例子,很简单的!
一个是命令行的,一个是GUI的,GUI那个可以把处理网络连接放在专门的Thread里面,这样更好的。
程序如下:
public final static int DEFAULT_PORT = 43;
public final static String DEFAULT_HOST = "whois.internic.net"; public static void main(String[] args){
InetAddress server; try{
server = InetAddress.getByName(DEFAULT_HOST);
}
catch(UnknownHostException e){
System.err.println("Error: Could not locate default host " + DEFAULT_HOST);
System.err.println("Check to make sure you're connected to the Internet and that DNS is funtioning");
System.err.println("Usage: java WhiosClient host port ");
return;
} int port = DEFAULT_PORT;
try{
Socket theSocket = new Socket(server,port);
Writer out = new OutputStreamWriter(theSocket.getOutputStream(),"8859_1");
for(int i = 0;i < args.length;i++) out.write(args[i] + " ");
out.write("\r\n");
out.flush();
InputStream raw = theSocket.getInputStream();
InputStream in = new BufferedInputStream(raw);
int c;
while((c = in.read()) != -1) System.out.write(c);
}
catch(IOException e){
System.err.println(e);
}
}
一个是命令行的,一个是GUI的,GUI那个可以把处理网络连接放在专门的Thread里面,这样更好的。
程序如下:
public final static int DEFAULT_PORT = 43;
public final static String DEFAULT_HOST = "whois.internic.net"; public static void main(String[] args){
InetAddress server; try{
server = InetAddress.getByName(DEFAULT_HOST);
}
catch(UnknownHostException e){
System.err.println("Error: Could not locate default host " + DEFAULT_HOST);
System.err.println("Check to make sure you're connected to the Internet and that DNS is funtioning");
System.err.println("Usage: java WhiosClient host port ");
return;
} int port = DEFAULT_PORT;
try{
Socket theSocket = new Socket(server,port);
Writer out = new OutputStreamWriter(theSocket.getOutputStream(),"8859_1");
for(int i = 0;i < args.length;i++) out.write(args[i] + " ");
out.write("\r\n");
out.flush();
InputStream raw = theSocket.getInputStream();
InputStream in = new BufferedInputStream(raw);
int c;
while((c = in.read()) != -1) System.out.write(c);
}
catch(IOException e){
System.err.println(e);
}
}
import java.io.*;
import com.macfaq.io.SafeBufferedReader;public class Whois {
public final static int DEFAULT_PORT = 43;
public final static String DEFAULT_HOST = "whois.internic.net";
private int port = DEFAULT_PORT;
private InetAddress host;
public Whois(InetAddress host,int port){
this.host = host;
this.port = port;
}
public Whois(InetAddress host){
this(host,DEFAULT_PORT);
}
public Whois(String hostname,int port) throws UnknownHostException{
this(InetAddress.getByName(hostname),port);
}
public Whois(String hostname) throws UnknownHostException{
this(InetAddress.getByName(hostname),DEFAULT_PORT);
}
public Whois() throws UnknownHostException{
this(DEFAULT_HOST,DEFAULT_PORT);
}
public static class SearchFor{
public static SearchFor ANY = new SearchFor();
public static SearchFor NETWORK = new SearchFor();
public static SearchFor PERSON = new SearchFor();
public static SearchFor HOST = new SearchFor();
public static SearchFor DOMAIN = new SearchFor();
public static SearchFor ORGANIZATION = new SearchFor();
public static SearchFor GROUP = new SearchFor();
public static SearchFor GATEWAY = new SearchFor();
public static SearchFor ASN = new SearchFor();
private SearchFor(){};
}
public static class SearchIn{
public static SearchIn ALL = new SearchIn();
public static SearchIn NAME = new SearchIn();
public static SearchIn MAILBOX = new SearchIn();
public static SearchIn HANDLE = new SearchIn();
private SearchIn(){};
}
public String lookUpNames(String target,SearchFor category,SearchIn group,boolean exactMatch)
throws IOException{
String suffix = "";
if(!exactMatch) suffix = ".";
String searchInLabel = "";
String searchForLabel = "";
if(group == SearchIn.ALL) searchInLabel = "";
else if(group == SearchIn.NAME) searchInLabel = "Name ";
else if(group == SearchIn.MAILBOX) searchInLabel = "Mailbox ";
else if(group == SearchIn.HANDLE) searchInLabel = "!";
if(category == SearchFor.NETWORK) searchForLabel = "Network ";
else if(category == SearchFor.PERSON) searchForLabel = "Person ";
else if(category == SearchFor.HOST) searchForLabel = "Host ";
else if(category == SearchFor.DOMAIN) searchForLabel = "Domain ";
else if(category == SearchFor.ORGANIZATION) searchForLabel = "Organization ";
else if(category == SearchFor.GROUP) searchForLabel = "Group ";
else if(category == SearchFor.GATEWAY) searchForLabel = "Gateway ";
else if(category == SearchFor.ASN) searchForLabel = "ASN ";
String prefix = searchForLabel + searchInLabel;
String query = prefix + target + suffix;
Socket theSocket = new Socket(host,port);
Writer out = new OutputStreamWriter(theSocket.getOutputStream(),"ASCII");
SafeBufferedReader in = new SafeBufferedReader(new
InputStreamReader(theSocket.getInputStream(),"ASCII"));
out.write(query + "\r\n");
out.flush();
StringBuffer response = new StringBuffer();
String theLine = null;
while((theLine = in.readLine()) != null){
response.append(theLine);
response.append("\r\n");
}
theSocket.close();
return response.toString();
}
public InetAddress getHost(){
return this.host;
}
public int getPort(){
return this.port;
}}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class WhoisGUI extends JFrame{
private JTextField searchString = new JTextField(30);
private JTextArea names = new JTextArea(15,80);
private JButton findButton = new JButton("Find");
private ButtonGroup searchIn = new ButtonGroup();
private ButtonGroup searchFor = new ButtonGroup();
private JCheckBox exactMatch = new JCheckBox("Exact Match",true);
private JTextField chosenServer = new JTextField();
private Whois server;
public WhoisGUI(Whois whois){
super("Whois");
this.server = whois;
Container pane = this.getContentPane();
Font f = new Font("Monospaced",Font.PLAIN,12);
names.setFont(f);
names.setEditable(false);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(1,1,10,10));
JScrollPane jsp = new JScrollPane(names);
centerPanel.add(jsp);
pane.add("Center",centerPanel);
JPanel northPanel = new JPanel();
JPanel northPanelTop = new JPanel();
northPanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
northPanelTop.add(new JLabel("Whois: "));
northPanelTop.add("North",searchString);
northPanelTop.add(exactMatch);
northPanelTop.add(findButton);
northPanel.setLayout(new BorderLayout(2,1));
northPanel.add("North",northPanelTop);
JPanel northPanelBottom = new JPanel();
northPanelBottom.setLayout(new GridLayout(1,3,5,5));
northPanelBottom.add(initRecordType());
northPanelBottom.add(initSearchFields());
northPanelBottom.add(initServerChoice());
northPanel.add("Center",northPanelBottom);
pane.add("North",northPanel);
ActionListener al = new LookupNames();
findButton.addActionListener(al);
searchString.addActionListener(al);
}
private JPanel initRecordType(){
JPanel p = new JPanel();
p.setLayout(new GridLayout(6,2,5,2));
p.add(new JLabel("Search for:"));
p.add(new JLabel(""));
JRadioButton any = new JRadioButton("Any",true);
any.setActionCommand("Any");
searchFor.add(any);
p.add(any);
p.add(this.makeButton("Network"));
p.add(this.makeButton("Person"));
p.add(this.makeButton("Host"));
p.add(this.makeButton("Domain"));
p.add(this.makeButton("Organization"));
p.add(this.makeButton("Group"));
p.add(this.makeButton("Gateway"));
p.add(this.makeButton("ASN"));
return p;
}
private JRadioButton makeButton(String label){
JRadioButton button = new JRadioButton(label,false);
button.setActionCommand(label);
searchFor.add(button);
return button;
}
private JRadioButton makeSearchInRadioButton(String label){
JRadioButton button = new JRadioButton(label,false);
button.setActionCommand(label);
searchIn.add(button);
return button;
}
private JPanel initSearchFields(){
JPanel p = new JPanel();
p.setLayout(new GridLayout(6,1,5,2));
p.add(new JLabel("Search In: "));
JRadioButton all = new JRadioButton("All",true);
all.setActionCommand("All");
searchIn.add(all);
p.add(all);
p.add(this.makeSearchInRadioButton("Name"));
p.add(this.makeSearchInRadioButton("Mailbox"));
p.add(this.makeSearchInRadioButton("Handle"));
return p;
}
private JPanel initServerChoice(){
JPanel p = new JPanel();
p.setLayout(new GridLayout(6,1,5,2));
p.add(new JLabel("Search At: "));
chosenServer.setText(server.getHost().getHostName());
p.add(chosenServer);
chosenServer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
try{
InetAddress newHost = InetAddress.getByName(chosenServer.getText());
Whois newServer = new Whois(newHost);
server = newServer;
}
catch(Exception e){
chosenServer.setText(server.getHost().getHostName());
}
}
});
return p;
}
class LookupNames implements ActionListener{
public void actionPerformed(ActionEvent event){
Whois.SearchIn group = Whois.SearchIn.ALL;
Whois.SearchFor category = Whois.SearchFor.ANY;
String searchForLabel = searchFor.getSelection().getActionCommand();
String searchInLabel = searchIn.getSelection().getActionCommand();
if(searchInLabel.equals("Name")) group = Whois.SearchIn.NAME;
else if(searchInLabel.equals("Mailbox")) group = Whois.SearchIn.MAILBOX;
else if(searchInLabel.equals("Handle")) group = Whois.SearchIn.HANDLE;
if(searchForLabel.equals("Network")) category = Whois.SearchFor.NETWORK;
else if(searchForLabel.equals("Person")) category = Whois.SearchFor.PERSON;
else if(searchForLabel.equals("Host")) category = Whois.SearchFor.HOST;
else if(searchForLabel.equals("Domain")) category = Whois.SearchFor.DOMAIN;
else if(searchForLabel.equals("Organization")) category = Whois.SearchFor.ORGANIZATION;
else if(searchForLabel.equals("Group")) category = Whois.SearchFor.GROUP;
else if(searchForLabel.equals("Gateway")) category = Whois.SearchFor.GATEWAY;
else if(searchForLabel.equals("ASN")) category = Whois.SearchFor.ASN;
try{
names.setText("");
String result = server.lookUpNames(searchString.getText(),category,group,exactMatch.isSelected());
names.setText(result);
}
catch(IOException e){
names.setText("Lookup failed due to " + e);
}
}
}
public static void main(String[] args){
try{
Whois server = new Whois();
WhoisGUI a = new WhoisGUI(server);
a.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
a.pack();
a.show();
}
catch(UnknownHostException e){
System.err.println("Error: Could not locate default host " + Whois.DEFAULT_HOST);
System.err.println("Check to make sure you're connected to the Internet and that DNS is funtioning");
System.err.println("Usage: java WhoisGUI");
return;
}
}
}