1、windows系统手动设置ip地址(ipv4)很容易。ipv6用16个字节表示ip地址。
443端口主要用于https服务。
2、java网络编程
2.1、
// 服务器端
public ServerSocket(int port) throws IOException {} // 指定服务器端监听的端口号
public Socket accept() throws IOException {} // 监听连接
// 客户端
public Socket(InetAddress address, int port) throws IOException {} // 客户端指定服务器地址
getInputStream(),getOutputStream()获取socket中的流和服务器进行IO通信。
2.2、
public void shutdownOutput() throws IOException{} // 关闭java.net.Socket的输出流
2.3、模拟文件上传
public class TCPServer { // 可以每次开启一个线程处理请求,如用上while(true){}
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("src/output.jpg");
byte[] buff = new byte[1024];
int len = 0;
ServerSocket ss = new ServerSocket(7891);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
while ((len = is.read(buff)) != -1) {
fos.write(buff, 0, len);
}
OutputStream os = socket.getOutputStream();
os.write("上传成功".getBytes());
fos.close();
socket.close();
ss.close();
}
}
public void clientTest() throws IOException{
FileInputStream fis = new FileInputStream("src/test.jpeg");
byte[] buff = new byte[1024];
int len = 0;
Socket socket = new Socket("127.0.0.1", 7891);
OutputStream os = socket.getOutputStream();
while ((len = fis.read(buff)) != -1) {
os.write(buff, 0, len);
}
socket.shutdownOutput(); // 写一个结束标记!!!否则与服务器交互时将阻塞在IO处。
System.out.println("22222222");
InputStream is = socket.getInputStream();
while ((len = is.read(buff)) != -1) {
System.out.println(new String(buff, 0, len));
}
fis.close();
socket.close();
}
2.4、B/S架构,浏览器充当客户端的角色;