1.协议:传输规则
- 网络层:核心,进行数据分组分发
- 传输层·:tcp,udp
- 以IP地址找到计算机,以端口号找到需要的服务
- 192.168是局域网开头,127.0.0.1是本机地址
- ping检测是否连通,ipconfig命令查看详细网络信息
- 端口是两个字节(0-65535)1024之前端口已经被系统占用
- 网址默认会跟80端口:www.baidu.com:80
2.InetAddress
InetAddress.getLocalHost().getHostAddress();
InetAddress.getLocalHost().getHostName();
InetAddress.getByName("127.0.0.1");通过地址获得对象
获得本机InetAddress,getHostAddress或本机地址
getHostName获本机名称
3.udp
- 不怕丢数据时候使用,不需要确认接收端是否存在
- 限制64kb之内
- packet是包裹,socket是码头
接收端
public class ReceivePacket {
public static void main(String[] args) throws IOException {
byte[] data = new byte[1024];一般写1024*64
DatagramPacket dp = new DatagramPacket(data, data.length);
DatagramSocket ds = new DatagramSocket(6000);
receive有等待特性
ds.receive(dp);
getAddress获取发送的地址对象
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
int length = dp.getLength();此处确定长度避免浪费
System.out.println(new String(data, 0, length) + ip + length);
ds.close();
}
}
-------------------------------
发送端
public class SendPacket {
public static void main(String[] args) throws IOException {
byte[] data = "你好world".getBytes();
DatagramPacket dp = new DatagramPacket(data, data.length,
InetAddress.getByName("127.0.0.1"), 6000);
DatagramSocket ds = new DatagramSocket();
ds.send(dp);
ds.close();
}
}
3.tcp
- scanner中nextline是一行为结束,next则是空格就会接收不到
- udp不区分发送接收端,tcp则区别
- 提供数据的则是服务器
- tcp服务器等待客户端链接,不会主动链接
- 在new好同路传输,io流对象就已经创建好
客户端上传照片
客户端
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream out = socket.getOutputStream();
获取同路链接通时候已经存在的流对象,
不能自己创建只能用已经有的socket包装的对象
FileInputStream fis = new FileInputStream(
"C:\\Users\\Desktop\\t.jpg");
int len = 0;
byte[] data = new byte[1024];
while ((len = fis.read(data)) != -1) {
out.write(data, 0, len);
}
socket.shutdownOutput();
在上传完成告诉服务器已经传完,
否则服务器的InputStream 对象read方法一直会读取
InputStream inputStream = socket.getInputStream();
len = inputStream.read(data);
System.out.println(new String(data, 0, len));
fis.close();
socket.close();
}
}
------------------
服务器以多线程处理客户端
public class TCPSever {
public static void main(String[] args) throws IOException {
ServerSocket ser = new ServerSocket(8888);
while (true) {
Socket socket = ser.accept();
客户端socket只能接受为了获取input和output流对象
接受一个socket开一个线程
new Thread( new TCPThreadRun(socket)).start();
}
}
----
public class TCPThreadRun implements Runnable{
private Socket socket;
public TCPThreadRun (Socket socket) {
this.socket=socket;
}
public void run() {
try{
InputStream inputStream = socket.getInputStream();
File file = new File("C:\\Users\\Desktop\\a");
if (!file.exists()) {
file.mkdirs();
}
String filename = "itcast" + System.currentTimeMillis()
+ new Random().nextInt(999999)+".jpg";随机名字
FileOutputStream fos = new FileOutputStream(file+File.separator+ filename);
int len = 0;
byte[] data = new byte[1024];
while ((len = inputStream.read(data)) != -1) {
fos.write(data, 0, len);
}
OutputStream out = socket.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
socket.close();
}catch (Exception e) {
// TODO: handle exception
System.out.println(e);
throw new RuntimeException("上传出错");
}
}
}