//简单的示例
package Ch3;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Scanner;
public class SocketTest {
public static void main(String[] args) throws IOException {
// socket(host,port)也可能超时。一直连接不上。
/*
Socket s = new Socket();
s.connect(new InetSocketAddress(host,port),timeout);
解决
*/
try(Socket s = new Socket("time-A.timefreq.bldrdoc.gov",13)){
InputStream inStream = s.getInputStream();
//设置超时时间
s.setSoTineout(10000);
Scanner in = new Scanner(inStream);
while (in.hasNextLine()){
String line = in.nextLine();
System.out.println(line);
}
}
3.1.2 因特网地址
package Ch3;
import java.io.IOException;
import java.net.InetAddress;
public class InetAddressTest {
public static void main(String[] args) throws IOException{
if(args.length>0){
String host = args[0];
InetAddress address = InetAddress.getByName(host);
//主机名/地址
System.out.println(address);
//主机名
System.out.println(address.getHostName());
//byte[] 返回的数字型地址
System.out.println(address.getAddress());
// 十进制地址字符串
System.out.println(address.getHostAddress());
//有些主机名对应多个因特网地址
InetAddress[] addresses = InetAddress.getAllByName(host);
for(InetAddress e:addresses){
System.out.println(e);
}
}
else {
//取得本地主机名
InetAddress localHostAddress = InetAddress.getLocalHost();
System.out.println(localHostAddress);
}
}
}
/*
* PYLC/10.192.20.85
*
* google.com/172.217.24.14
google.com/172.217.24.14
* */
3.2 实现服务器
package Ch3;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class EchoServer {
public static void main(String[] args) throws IOException{
//新建位于端口8189的服务器
try(ServerSocket s = new ServerSocket(8189)){
//accept()告诉程序等待,一旦有连接成功,就会返回对象,下面是incoming。
try(Socket incoming = s.accept()){
//建立输入和输出流
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
//封装为扫描器
try(Scanner in = new Scanner(inStream)){
//封装为打印器
PrintWriter out = new PrintWriter(outStream ,true );
out.println("Hello! Enter BYE to exit.");
boolean done = false;
//回显
while(!done && in.hasNextLine()){
String line = in.nextLine();
out.println("Echo: "+line);
//trim除掉String前后空格
if(line.trim().equals("BYE")) done = true;
}
}
}
}
3.2.1 为多个客户端服务
package Ch3;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ThreadedEchoServer {
public static void main(String[] args) {
try{
int i = 1;
ServerSocket s = new ServerSocket(8189);
//循环等待,
while(true){
//没有一个连接,新建一个线程
Socket incoming = s.accept();
System.out.println("Spawning "+i);
Runnable r = new ThreadedEchoHandler(incoming);
Thread t = new Thread(r);
t.start();
i++;
}
}catch (IOException e){
e.printStackTrace();
}
}
}
class ThreadedEchoHandler implements Runnable{
private Socket incoming;
public ThreadedEchoHandler(Socket i){
incoming = i;
}
public void run(){
try{
try{
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true);
out.println("Hello! Enter BYE to exit.");
boolean done = false;
while(!done&&in.hasNextLine()){
String line = in.nextLine();
out.println("Echo: "+line);
if(line.trim().equals("BYE"))
done =true;
}
}finally{
incoming.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
/*
Spawning 1
Spawning 2*/
3.2.2 半关闭
套接字的一端可以终止其输出,同时仍旧可以接受来自另一端的数据。
Socket socket = new Socket(host, port);
Scanner in = new Scanner(socket.getInputStream());
PrintWriter writer = new PrintWriter(socket.getOutputStream());
//现在就是半关闭,可以读入,不能输出。
socket.shutdownOutput();
boolean isOutputShutdown()
//
boolean inInputShutdown()
socket.shutdownInput()
3.3 可中断套接字
3.4 获取Web数
3.4.2使用URLConnection来获取信息
1) URLConnection connection = url.openConnection();
返回URLConnectioin对象,负责管理对象与资源之间的连接
2) 使用以下方法设置请求的属性
java
setDoInput(boolean f) //控制用户是否可以接受输入URLConnection
setDoOutput() //控制用户是否可以发送输出到URLConnection
setIfModifiedSince()//
setUseCaches()
setAllowUserInteraction()
setRequestProperty(String key, String value)//设置请求头的一个字段
setConnectTimeout()
setReadTimeout()
3) connection.connect() 建立连接
4)查询头信息
Map<String, List<String>> map getHeaderFields()//返回响应的映射表
String getHeaderFieldKey(int n)//返回响应第n个字段
String getHeaderField(int n)//得到响应的字段值
5)访问资源
getInputStream()方法获取一个输入流
3.4.3 提交表单数据
Get和Post