}//改进的UDP套接字程序,可以互相发送数据
--------------------------------------------------------------------------------------------------------------------------package lesson10;
--------------------------------------------------------------------------------
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Download {
public static void main(String[] args)
{
JFrame jf=new JFrame("LM下载");
jf.setSize(600,400);
jf.setLocation(100,100);
JPanel p=new JPanel();
JLabel l=new JLabel("Please input URL:");
final JTextField tf=new JTextField(30);
p.add(l);
p.add(tf);
jf.getContentPane().add(p,"North");
final JTextArea ta=new JTextArea();
jf.getContentPane().add(ta,"Center");
JButton btn=new JButton("Download");
jf.getContentPane().add(btn,"South");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str=tf.getText();
try
{
URL url=new URL(str);
URLConnection urlConn=url.openConnection();
String line=System.getProperty("line.separator");
ta.append("URLHOST"+url.getHost());
ta.append(line);
ta.append("Port:"+url.getDefaultPort());
ta.append(line);
ta.append("ContentType"+urlConn.getContentType());
ta.append(line);
ta.append("ContentLength"+urlConn.getContentLength());
InputStream is=urlConn.getInputStream();
//InputStreamReader isr=new IputStreamReader(is);
//Bufferedreader br=new Bufferedreader(isr);
FileOutputStream fos=new FileOutputStream("1.html");
//String strLine;
//while((strLine=br.readLine())!=null)
int data;
while((data=is.read())!=-1)
{
//fos.write(strLine.getBytes());
//fos.write(line.getBytes());
fos.write(data);
}
//br.close();
is.close();
fos.close();
URL与URI
URL(Uniform Resource Locator ),通用资源定位符。
URI(Uniform Resource Identifier),通用资源标识符。
URI纯粹是个符号结构,1.85传奇私服,用于指定构成Web资源的字符串的各个不同部分。URL是一种特殊类型的URI,它包含了用于查找某个资源的足够信息。其它的URI,例如:mailto:myoble@mybole.com.cn则不属于定位符,因为它里面不存在根据该标识符来查找的任何数据。这种URI称为URN(通用资源名),轻变传奇网站。
在Java库中,URI类不包含用于访问通用资源标识符设定的任何方法,它的唯一作用是进行分析。相反,URL类则可以打开到达资源的一个字符串。
import java.net.*;
import java.io.*;
public class lesson10 extends Thread
{
public static void main(String[] args)
{
if(args.length>0)
recv();
else
send();
}
public static void recv()
{
try
{
DatagramSocket ds=new DatagramSocket(6000);
byte[] buf=new byte[100];
DatagramPacket dp=new DatagramPacket(buf,100);
ds.receive(dp);
System.out.println(new String(buf,0,dp.getLength()));
String str="Welcome you!";
DatagramPacket dpSend=new DatagramPacket(str.getBytes(),str.length(),dp.getAddress(),dp.getPort());
ds.send(dpSend);
ds.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
package lesson10;
}
public static void send()
{
try
{
DatagramSocket ds=new DatagramSocket();
String str="Hello World";
DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName("localhost"),1.85传奇私服,6000);
ds.send(dp);
byte[] buf=new byte[100];
DatagramPacket dpRecv=new DatagramPacket(buf,100);
ds.receive(dpRecv);
System.out.println(new String(buf,0,dpRecv.getLength()));
ds.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}
});
jf.show();
}
}//一个下载程序,网通传奇合击SF,很有用
