UDP
发送方
public static void main(String[] args) throws Exception{
DatagramSocket datagramSocket = new DatagramSocket();
String s = "你好UDP协议" ;
datagramSocket.send(new DatagramPacket(s.getBytes(),
0,
s.length(), InetAddress.getLocalHost(),
8084));
System.out.println(datagramSocket);
datagramSocket.close();
}
接收方
public static void main(String[] args) throws Exception{
DatagramSocket datagramSocket = new DatagramSocket(8084);
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length);
datagramSocket.receive(datagramPacket);
System.out.println(datagramPacket.getAddress());
int length = datagramPacket.getLength();
System.out.println(new String(bytes,0,length));
datagramSocket.close();
}
TCP
服务器端
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(8085);
//之后都是操作Socket对象
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len=-1;
while ((len = inputStream.read(bytes) )!= -1) {
System.out.println(new String(bytes,0,len));
}
OutputStream outputStream = socket.getOutputStream();
outputStream.write("我已收到信息".getBytes());
socket.shutdownOutput();
}
客户端
public static void main(String[] args) throws Exception{
Socket socket = new Socket("localhost",8085);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
//传过去
outputStream.write("我是你们的老朋友".getBytes());
socket.shutdownOutput();
//传回来
byte[] bytes = new byte[1024];
int len=-1;
while ((len = inputStream.read(bytes) )!= -1) {
System.out.println(new String(bytes,0,len));
}
}
BS架构基本原理
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("开启服务器");
Socket socket = serverSocket.accept();
//通过socket获得InputStream,获得请求客户端的信息
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
System.out.println(new String(bytes,0,len));
//my.html为服务器端的文件
BufferedReader br = new BufferedReader(new FileReader("my.html"));
String content = br.readLine();
br.close();
//通过socket获取outputStream,向请求客户端发送信息
OutputStream outputStream = socket.getOutputStream();
outputStream.write(content.getBytes());
}
IE浏览器测试 http:localhost:8080
服务输出数据
开启服务器
GET / HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: zh-Hans-CN,zh-Hans;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost:8888
Connection: Keep-Alive
多用户同时文件上传
客户端
public static void main(String[] args) throws Exception{
for (int i = 0; i <10 ; i++)
{
Socket socket = new Socket("localhost",8082);
OutputStream outputStream = socket.getOutputStream();
BufferedInputStream stream = new BufferedInputStream(new FileInputStream("a.jpg"));
int ll=-1;
byte[] bytes1 = new byte[1024];
while ((ll = stream.read(bytes1)) != -1) {
outputStream.write(bytes1);
}
stream.close();
//为什么要有下面这个?因为上面的write传输的时候并没有把结束符传输过去?这样对方执行(len = inputStream.read(bytes)) != -1是永远也无法读取到-1的
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len=-1;
while ((len = inputStream.read(bytes)) != -1) {
System.out.println(new String(bytes,0,len));
}
}
}
服务端
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(8082);
System.out.println("正在等待客户端发起请求......");
while(true){
Socket socket = serverSocket.accept();
new Thread(()-> {
try {
InputStream inputStream = socket.getInputStream();
String s = System.currentTimeMillis() + "" + ".jpg" ;
System.out.println(s);
BufferedOutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(new File(s)));
byte[] bytes = new byte[1024];
int i = -1;
System.out.println("书写图片");
while ((i = inputStream.read(bytes)) != -1) {
outputStream1.write(bytes, 0, i);
}
OutputStream outputStream = socket.getOutputStream();
outputStream.write("服务端收到信息".getBytes());
socket.shutdownOutput();
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}