Java基础--网络编程1

时间:2022-05-01 20:16:53

使用socket连接到服务器

public static void main(String[] args) throws IOException {
Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13); // 构建一个套接字,链接给定主机和端口
s.setSoTimeout(10000); // 设置超时时间10s
InputStream inStream = s.getInputStream();
Scanner in = new Scanner(inStream);
while (in.hasNext()) {
String line = in.nextLine();
System.out.println(line); // 57196 15-06-23 13:16:02 50 1 0 43.7
// UTC(NIST) *
}
in.close();
s.close();
/*
* 使用上述超时设置,socket会一直阻塞,直到达到主机的初始链接
*
* 可以先构建一个无连接的套接字,然后在设置一个超时来进行链接
*
* InetAddress:主机名和因特网地址之间转换
*/
Socket s2 = new Socket();
s2.connect(new InetSocketAddress("time-A.timefreq.bldrdoc.gov", 13),
10000);
s2.close();
InetAddress address = InetAddress
.getByName("time-A.timefreq.bldrdoc.gov");
System.out.println(address.toString()); // time-A.timefreq.bldrdoc.gov/132.163.4.101
byte[] addressBytes = address.getAddress();
for (byte b : addressBytes) {
System.out.print(b + ":"); // -124:-93:4:101:
}
System.out.println();
System.out.println("===============");
String host = "132.163.4.101";
if (!host.equals("")) {
InetAddress[] addresses = InetAddress.getAllByName(host);
for (InetAddress a : addresses) {
System.out.println(a);
}
} else {
InetAddress localHostAddress = InetAddress.getLocalHost();
System.out.println(localHostAddress);
}
}
实现服务器

1.简单的处理单连接的服务器

public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(8189); // 建立一个监听8189端口的服务器
Socket incoming = s.accept(); // 服务器等待客户端连接
InputStream in = incoming.getInputStream();
OutputStream out = incoming.getOutputStream();
Scanner console = new Scanner(in);
PrintWriter pw = new PrintWriter(out, true);
pw.println("Hello! Enter BYE to exit"); // 向客户端发出信息
boolean done = false;
// 等待客户端输入
while (!done && console.hasNext()) {
String line = console.nextLine();
pw.println(line);
if (line.trim().equals("BYE")) {
done = true;
}
}
console.close();
s.close();
}

2.可以处理多连接的服务器

public class ThreadEchoServer {

public static void main(String[] args) throws IOException {
int i = 1;
ServerSocket server = new ServerSocket(8189);
boolean done = false;
while (!done) {
Socket socket = server.accept();
System.out.println("Spwwning " + i);
Runnable threadSocket = new ThreadSocket(socket);
Thread thread = new Thread(threadSocket);
thread.start();
i++;
if (i > 4) {
done = true;
}
}
System.out.println("超过最大连接数");
server.close();
}
}

class ThreadSocket implements Runnable {
private Socket socket;

public ThreadSocket(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
try {
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
pw.println("Hello, Welcome to my server....");
Scanner console = new Scanner(is);
boolean done = false;
while (!done && console.hasNext()) {
String line = console.nextLine();
pw.println("Server response: " + line);
if (line.trim().equals("BYE")) {
done = true;
}
}
console.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

半关闭(half-close)

功能:套接字连接的一端可以终止其输出,同时仍然可以接收另一端的数据

Socket socket = new Socket(host, port);
Scanner console = new Scanner(socket.getInputStream());
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.print("....");
pw.flush();
socket.shutdownOutput();
// 关闭输出流,此时处于半连接状态
while (console.hasNext()) {
String line = console.nextLine();
}
socket.close();