网络通信
数据的发送和接收,通过端口进行
端口号的范围0-65535,自己使用的时候别使用0-1024(被系统占用)
InetAddress
把IP地址面向对象
得到本机的IP地址对应的InetAdress对象getLocalHost,得到任意IP地址的InetAdress对象getByName
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Demo1 {
public static void main(String[] args) throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost.getAddress());
System.out.println(localHost.getHostName());
InetAddress host1 = InetAddress.getByName("172.28.67.76");
System.out.println(host1.getHostName());
}
}
udp协议
面向无连接的、直接收发数据,不管对方在不在,速度快,不安全
创建Socket对象,使用DatagramSocket
发送数据
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class UdpSend {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket();
String data="hello,world";
byte[] arr = data.getBytes(StandardCharsets.UTF_8);
DatagramPacket datagramPacket = new DatagramPacket(arr,arr.length, InetAddress.getByName("172.28.67.76"),12345);
datagramSocket.send(datagramPacket);
datagramSocket.close();
}
}
接收数据
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpReceive {
public static void main(String[] args) throws IOException {
DatagramSocket receive = new DatagramSocket(12345);
byte[] arr = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(arr,arr.length);
receive.receive(datagramPacket);
byte[] data = datagramPacket.getData();
String ip = datagramPacket.getAddress().getHostAddress();
System.out.println(ip+":"+new String(data,0,datagramPacket.getLength()));
receive.close();
}
}
tcp协议
面向连接的,速度慢,安全
确认和是否和对方连接上:三次握手,发出请求,对方给响应,发送发再次发送响应表示收到了接收方的响应
Server服务端,被连接
Client客户端,主动进行连接的一方
tcp协议中的节点对象使Socket(InetAddress address,int port)创建,Socket内部有字节输入流和字节输出流,使用输入输出流实现数据的传输通信
服务端可能有多个客户端与其建立连接,为了确定服务端的消息传输不出错,服务端应该拿到客户端的Socket对象,相当于客户端和服务端使用一个流对象,使用accept方法得到客户端对象
client
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class tcpClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("172.28.67.76"),12347);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello".getBytes(StandardCharsets.UTF_8));
InputStream inputStream = socket.getInputStream();
byte[] arr = new byte[1024];
int read = inputStream.read(arr);
System.out.println(new String(arr,0,read));
}
}
Server
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class tcpServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(12347);
Socket so = serverSocket.accept();
byte[] arr = new byte[1024];
InputStream inputStream = so.getInputStream();
int len = inputStream.read(arr);
System.out.println(new String(arr,0,len));
OutputStream outputStream = so.getOutputStream();
outputStream.write("你好".getBytes(StandardCharsets.UTF_8));
}
}
文本文件的上传
客户端:读取一个本地文件,每次读取的数据发送给服务端,读取服务端发送过来的“上传成功”
public class UploadClient {
public static void main(String[] args) throws IOException {
System.out.println("客户端启动了");
Socket socket=new Socket(InetAddress.getByName("172.28.67.17"),18888);
BufferedReader bufferedReader=new BufferedReader(new FileReader("files\\temp.txt"));
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter=new PrintWriter(outputStream,true);
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader1=new BufferedReader(new InputStreamReader(inputStream));
String line =null;
while((line=bufferedReader.readLine())!=null)
{
printWriter.println(line);
}
socket.shutdownOutput();
bufferedReader.close();
String str = bufferedReader1.readLine();
System.out.println(str);
socket.close();
}
}
服务端
public class UploadServer {
public static void main(String[] args) throws IOException {
System.out.println("服务端启动了");
ServerSocket serverSocket=new ServerSocket(18888);
Socket socket = serverSocket.accept();
System.out.println(socket.getInetAddress().getHostAddress());
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("files\\temp_copy.txt"));
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter=new PrintWriter(outputStream,true);
String line = null;
while((line=bufferedReader.readLine())!=null)
{
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedWriter.close();
printWriter.println("上传成功");
socket.close();
serverSocket.close();
}
}
文件的上传
客户端
public class UploadClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("172.28.67.17"),19999);
FileInputStream fileInputStream = new FileInputStream("images\\33.jpg");
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
byte[] arr = new byte[1024];
int len;
while ((len=fileInputStream.read(arr))!=-1){
outputStream.write(arr,0,len);
}
socket.shutdownOutput();
fileInputStream.close();
len = inputStream.read(arr);
System.out.println(new String(arr,0,len));
socket.close();
}
}
服务端
public class UploadServer { public static void main(String[] args) throws IOException { System.out.println("服务端启动了"); ServerSocket serverSocket = new ServerSocket(19999); Socket accept = serverSocket.accept(); System.out.println("得到来自客户端"+accept.getInetAddress().getHostName()+"连接");
作业
实现将客户端从键盘输入的小写字母转为大写字母并返回的服务端功能
客户端
服务端
public class ChangeServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(19999); Socket socket = serverSocket.accept(); System.out.println(socket.getInetAddress().getHostName()+"连接到服务端了");
|