网络协议
网络协议
TCP协议
-
使用TCP协议前,须先建立TCP连接,形成传输数据通道
-
传输前,采用“三次握手”方式,点对点通信,是可靠的

-
TCP协议进行通信的两个应用进程:客户端、服务端
-
在连接中可进行大数据量的传输
-
传输完毕,需释放已建立的连接,效率低
UDP协议
- 将数据、源、目的封装成数据包,不需要建立连接
- 每个数据报的大小限制在64k内
- 发送不管对方是否准备好,接收方收到也不确认,是不可靠的
- 可以广播发送
- 发送数据结束时,无需释放资源,开销小,速度快
TCP网络编程例题1
客户端发送信息给服务端,服务端将信息显示在控制台上
客户端代码:
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
try {
//1、创建socket对象,指明服务器端的ip和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 60000);
//2、获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3、写出数据的操作
os.write("你好!我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、关闭资源流
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务端代码:
@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1、创建服务器端的ServerSocket,指明自己的端口号
serverSocket = new ServerSocket(60000);
//2、调用accept()方法表示接受来自于客户端的socket
socket = serverSocket.accept();
//3、获取一个输入流
is = socket.getInputStream();
//不建议这么写,有乱码
// byte[] buffer = new byte[20];
// int len;
// while ((len = is.read(buffer)) != -1){
// String str = new String(buffer, 0, len);
// System.out.print(str);
// }
//4、读取输入流中的数据
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while ((len = is.read(buffer)) != -1){
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据");
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、关闭资源流
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
TCP网络编程例题2
客户端发送文件给服务端,服务端将文件保存在本地
客户端代码:
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
os = socket.getOutputStream();
fis = new FileInputStream(new File("pic.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1){
os.write(buffer, 0 ,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务端代码:
@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
try {
serverSocket = new ServerSocket(9090);
socket = serverSocket.accept();
is = socket.getInputStream();
fos = new FileOutputStream(new File("pic_server.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer, 0, len);
}
System.out.println("接受成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
TCP网络编程例题3
客户端给服务端发送文件,服务端接受文件保存到本地,并返回发送成功的反馈
客户端代码:
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
ByteArrayOutputStream baos = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
os = socket.getOutputStream();
fis = new FileInputStream(new File("pic.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1){
os.write(buffer, 0 ,len);
}
//关闭数据的输出
socket.shutdownOutput();
//接受来自于服务器端的数据,并显示到控制台上
InputStream is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer_ = new byte[20];
int len_is;
while ((len_is = is.read(buffer_)) != -1){
baos.write(buffer_, 0, len_is);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务端代码:
@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
OutputStream os = null;
try {
serverSocket = new ServerSocket(9000);
socket = serverSocket.accept();
is = socket.getInputStream();
fos = new FileOutputStream(new File("pic_server2.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer, 0, len);
}
System.out.println("服务器收到来自:"
+ "主机名【" + InetAddress.getLocalHost().getHostName() + "】,"
+ "ip地址【" + InetAddress.getLocalHost().getHostAddress() + "】"
+ "的文件");
//服务器端给予客户端反馈
os = socket.getOutputStream();
os.write("服务器已接收!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
UDP网络编程例题(了解即可)
发送端代码:
//发送端
@Test
public void sender(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
InetAddress inet = InetAddress.getLocalHost();
String str = "UDP方式";
byte[] data = str.getBytes();
DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 9090);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null)
socket.close();
}
}
接收端代码:
//接收端
@Test
public void receiver(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket(9090);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(), 0, packet.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null)
socket.close();
}
}

浙公网安备 33010602011771号