网络编程(2)
通信协议
网络通信协议:速率,传输码率,代码结构,传输控制…
TCP/IP协议簇:实际上是一组协议
TCP:用户传输协议
UDP:用户数据报协议
IP:网络互连协议
TCP和UDP对比
TCP:
- 连接、稳定
- 三次握手、四次断开
- 客户端、服务端
- 传输完成、释放连接
UDP:
- 不连接、不稳定
- 客户端、服务端:没有明确界限
- 不管有没有准备好,都可以发
TCP
客户端
- 连接服务器Socket
- 发送消息
package com.yangxu.net;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket =null;
OutputStream outputStream = null;
try {
InetAddress name = InetAddress.getByName("127.0.0.1");
int port = 9999;
socket = new Socket(name,port);
outputStream = socket.getOutputStream();
outputStream.write("你好".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器
- 建立服务的端口ServerSocket
- 等待用户的链接accept
- 接收用户消息
package com.yangxu.net;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket socket = null;
Socket accept = null;
InputStream inputStream =null;
ByteArrayOutputStream stream =null;
try {
socket = new ServerSocket(9999);
accept = socket.accept();
inputStream = accept.getInputStream();
stream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes))!=-1){
stream.write(bytes,0,len);
}
System.out.println(stream.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
if (stream!=null){
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (accept!=null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件上传
服务器端
package com.yangxu.net;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo02 {
public static void main(String[] args) {
ServerSocket socket = null;
Socket accept = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
socket = new ServerSocket(9000);
accept = socket.accept();
inputStream = accept.getInputStream();
fileOutputStream = new FileOutputStream(new File("receive.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (accept!=null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端
package com.yangxu.net;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo02 {
public static void main(String[] args) {
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
try {
InetAddress name = InetAddress.getByName("127.0.0.1");
socket = new Socket(name,9000);
outputStream = socket.getOutputStream();
fileInputStream = new FileInputStream(new File("jc1612011_1a(1).jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileInputStream!=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Tomcat
服务器端
- 自定义 S
- Tomcat服务器 S:Java后台服务器
客户端
UDP
不用连接,知道对方的地址即可
发送消息
接收端
package com.yangxu.net2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServerDemo01 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
socket.receive(packet);
socket.close();
System.out.println(new String(packet.getData(),0,packet.getLength()));
}
}
发送端
package com.yangxu.net2;
import java.io.IOException;
import java.net.*;
public class UdpClientDemo01 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
String msg = "你好,你个服务器";
InetAddress name = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,name,port);
socket.send(packet);
socket.close();
}
}
|