Java网络编程
TCP
客户端
-
连接服务器 Socket
-
发送消息
-
package com.guanxing.lesson02; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; //客户端 public class TcpClientDemo01 { public static void main(String[] args) throws IOException { Socket socket = null; OutputStream os = null; try { //1.要知道服务器的地址,端口 InetAddress serverIP = InetAddress.getByName("127.0.0.1"); int port = 9999; //2.创建一个socket连接 socket = new Socket(serverIP, port); //3.发送信息 IO流 os = socket.getOutputStream(); os.write("你好,欢迎离开TCP聊天".getBytes()); } catch (UnknownHostException e) { e.printStackTrace(); } finally { //关闭资源 if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
服务端
-
建立服务的端口 ServerSocket
-
等到用户的连接 accept
-
接收用户的消息
-
package com.guanxing.lesson02; import java.io.ByteArrayInputStream; 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 serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1.我得有一个地址 serverSocket = new ServerSocket(9999); //循环持续接收 while (true) { //2.等待客户端连接过来 socket = serverSocket.accept(); //3.读取客户端的消息 is = socket.getInputStream(); //管道流(防止乱码超过规定的字节数) baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len=is.read(buffer)) != -1) { baos.write(buffer, 0, len); } System.out.println(baos.toString()); } } catch (IOException e) { e.printStackTrace(); } finally { //关闭资源 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(); } } } } }
文件上传
客户端
package com.guanxing.lesson02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception {
//1.创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream fis = new FileInputStream(new File("heibao.jpg"));
//4.写出文件
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1) {
os.write(buffer, 0, len);
}
//通知服务器已经传输完了
socket.shutdownOutput();
//确定服务端接收完毕
InputStream is = socket.getInputStream();
//字符串的管道流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[2014];
int len2;
while ((len2=is.read(buffer2))!=-1) {
baos.write(buffer2, 0, len2);
}
System.out.println(baos.toString());
//5.关闭资源
baos.close();
is.close();
fis.close();
os.close();
socket.close();
}
}
服务端
package com.guanxing.lesson02;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo02 {
public static void main(String[] args) throws Exception {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端的连接
Socket socket = serverSocket.accept(); //阻塞式监听,会一直等待客户端连接
//3.获取输入流
InputStream is = socket.getInputStream();
//4.文件输出(文件的管道流)
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1) {
fos.write(buffer, 0, len);
}
//通知客户端接收完毕
OutputStream os = socket.getOutputStream();
os.write("我接收完毕了,你可以爬了".getBytes());
//5.关闭资源
os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
Udp
udp没有明确的客户端和服务端 !
发送方
package com.guanxing.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
//1.建立一个socket
DatagramSocket socket = new DatagramSocket();
//2.建个包(5个参数: 数据 数据长度 接收者 端口)
String msg = "你好啊, 服务器!";
//发送给谁
InetAddress localhost = InetAddress.getByName("127.0.0.1");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//3.发送包
socket.send(packet);
//4.关闭流
socket.close();
}
}
接收方
package com.guanxing.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//还是要等待客户端的连接!
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
//处理数据
System.out.println(packet.getAddress().getHostAddress()); //127.0.0.1
System.out.println(new String(packet.getData())); //你好啊, 服务器!
//关闭连接
socket.close();
}
}
Udp聊天基础版
发送方
package com.guanxing.lesson04Chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpSenderDemo01 {
public static void main(String[] args) throws Exception {
//1.建立连接
DatagramSocket socket = new DatagramSocket(8888);
//2.创建包
//创建包里的数据 : 控制台读取
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String msg = reader.readLine();
//字符串要转化为字节
byte[] msgs = msg.getBytes();
//接收者和端口
DatagramPacket packet = new DatagramPacket(msgs, 0, msgs.length, new InetSocketAddress("localhost", 6666));
//3.发送包
socket.send(packet);
//如果输入为 bye,则停止
if (msg.equals("bye")) {
break;
}
}
//4.关闭流
socket.close();
}
}
接收方
package com.guanxing.lesson04Chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
//1.开放端口
DatagramSocket socket = new DatagramSocket(6666);
//2.死循环接收数据
while (true) {
//准备接收数据的容器
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet); //阻塞式接收
//断开连接 bye
byte[] data = packet.getData();
String msg = new String(data, 0, packet.getLength()); //这里避免接收多余的空字节
//打印接收到的信息
System.out.println(msg);
if (msg.equals("bye")) {
break;
}
}
socket.close();
}
}