java net

来源:互联网 发布:厦门大学经济学院知乎 编辑:程序博客网 时间:2024/05/17 02:07

1. http

TestHttpURLConnection:

package test.http;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class TestHttpURLConnection {
public static void main(String[] args) throws Exception {
try {
// 打开连接
URL Url = new URL("http://www.baidu.com/s");
// 创建HttpURLConnection
HttpURLConnection conn = (HttpURLConnection) Url.openConnection();
// 设置属性
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
// 连接
conn.connect();

// 提交数据
PrintWriter out = new PrintWriter(conn.getOutputStream());
String param = "wd=Java";
out.println(param);
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}

TestInetAddress:

package test.http;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
public static void main(String[] args) throws Exception {
try {
// 取得本机地址
InetAddress local = InetAddress.getLocalHost();
System.out.println(local);
// 根据主机名取得地址
InetAddress local2 = InetAddress.getByName("localhost");
System.out.println(local2);
// 取得一组地址
InetAddress[] address = InetAddress.getAllByName("java.sun.com");
for (int i = 0; i < address.length; i++) {
System.out.println(address[i]);
}
// 根据IP取得地址
InetAddress ip = InetAddress.getByAddress(new byte[] { (byte) 192,
(byte) 168, 0, 1 });
System.out.println(ip);
InetAddress ip2 = InetAddress.getByAddress("java.sun.com",
new byte[] { 1, 2, 0, 1 });
System.out.println(ip2);
// 取得地址属性
System.out.println(local.getCanonicalHostName());
System.out.println(local.getHostAddress());
System.out.println(local.getHostName());
System.out.println(local.isReachable(1000));
// IPv4/IPv6
byte[] addr = local.getAddress();
if (addr.length == 4) {
System.out.println("IP版本 is ipv4");
int firstbyte = addr[0];
if (firstbyte < 0) {
firstbyte += 256;
}
if ((firstbyte & 0x80) == 0) {
System.out.println("IP类别 is A");
} else if ((firstbyte & 0xC0) == 0x80) {
System.out.println("IP类别 is B");
} else if ((firstbyte & 0xE0) == 0xC0) {
System.out.println("IP类别 is C");
} else if ((firstbyte & 0xF0) == 0xE0) {
System.out.println("IP类别 is D");
} else if ((firstbyte & 0xF8) == 0xF0) {
System.out.println("IP类别 is E");
}
} else if (addr.length == 16) {
System.out.println("IP版本 is ipv6");
}

} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

TestURL:

package test.http;
import java.net.MalformedURLException;
import java.net.URL;
public class TestURL {


public static void main(String[] args) throws Exception {
try {
// 根据地址创建
URL url = new URL("http://news.sohu.com/20080729/n258439735.shtml");

// 根据协议、地址、端口、文件创建
URL url2 = new URL("http","www.ntu.edu.cn",80,"/local/searchresult.html");
System.out.println(url2.getFile());

// 根据URL对象创建
URL base = new URL("http://www.strange.com.cn:80/x-file/1112.html");
URL url3 = new URL(base , "#change");
System.out.println(url3.getFile());

// 取得信息
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getDefaultPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
} catch (MalformedURLException  e) {
e.printStackTrace();
}
}
}


package test.http;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class TestURLConnection {
public static void main(String[] args) throws Exception {
try {
// 根据地址创建
URL url = new URL("http://news.sohu.com/20080729/n258439735.shtml");
// 取得URLConnection
URLConnection conn = url.openConnection();
// 打开连接
conn.connect();
// 取得属性
System.out.println(conn.getContentLength());
System.out.println(conn.getContentType());
System.out.println(conn.getDate());
System.out.println(conn.getExpiration());
System.out.println(conn.getLastModified());

// 读取网页数据
int c;
InputStream is = conn.getInputStream();
while (((c = is.read()) != -1)) {
System.out.print((char) c);
}
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}

2. tcp

package test.tcp;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestSocket {
public static void main(String args[]) {
// 创建Socket
try {
Socket socket = new Socket("www.sohu.com", 80);
System.out.println(socket);
} catch (UnknownHostException uex) {
} catch (IOException e) {
}

// 创建Socket
try {
InetAddress addr = InetAddress.getByName("www.sohu.com");
Socket socket = new Socket(addr, 80);
System.out.println(socket);
} catch (UnknownHostException uex) {
} catch (IOException e) {
}


Socket socket = null;
try {
// 创建Socket
socket = new Socket("www.sohu.com", 80);
InetAddress host = socket.getInetAddress();
int port = socket.getPort();
int localport = socket.getLocalPort();
InetAddress localhost = socket.getLocalAddress();
System.out.println(host);
System.out.println(port);
System.out.println(localhost);
System.out.println(localport);

// 输入流
InputStream is = socket.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bis.close();
is.close();

// 输出流
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.close();
os.close();

// 关闭Socket
socket.close();
} catch (UnknownHostException e) {
} catch (IOException e) {
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}


}
}

package test.tcp;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServerSocket {
public static void main(String[] args) {
// 建立监听
try {
ServerSocket server = new ServerSocket(12345);
System.out.println(server);
} catch (IOException e) {
}
// 建立监听
try {
ServerSocket server = new ServerSocket(12346, 20);
InetAddress host = server.getInetAddress();
int port = server.getLocalPort();
System.out.println(host);
System.out.println(port);
} catch (IOException e) {
}
// 建立监听
ServerSocket server = null;
try {
InetAddress host = InetAddress.getLocalHost();
server = new ServerSocket(65532, 20, host);
System.out.println(server.getInetAddress());

while (true) {
Socket socket = server.accept();//接收客户端连接
System.out.println(socket.getInetAddress());
// 发送数据
OutputStreamWriter osw = new OutputStreamWriter(socket
.getOutputStream());
osw.write("hello, client");
// 关闭客户端
osw.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
server.close();
} catch (IOException e) {
}
}
}
}




3. udp


0 0
原创粉丝点击