Java简单实现UDP和TCP
TCP实现
TCP协议需要在双方之间建立连接,通过输入输出流来进行数据的交换,建立需要通过三次握手,断开需要四次挥手,保证了数据的完整性,但传输效率也会相应的降低。
简单的TCP实现
//服务端
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8886); // 建立服务端,ip为本机ip,端口为8886
Socket accept = serverSocket.accept(); // 监听客户端的连接,一旦有客户端连接,则会返回客户端对应的accept
InputStream in = accept.getInputStream(); //获取到客户端的输出流
byte b[] = new byte[1024];
int len = in.read(b);
System.out.println("接受到客户端数据,返回数据"+new String(b,0,len));
OutputStream out = accept.getOutputStream(); // 给客户端发送消息
out.write("服务端已经接受".getBytes());
serverSocket.close();
}
}
// 客户端
public class TcpClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8886); // 通过Socket来建立和服务端的连接
OutputStream out = socket.getOutputStream(); // 获取输出流(客户端输出流即向服务端输出信息)
out.write("hello tcp Server".getBytes()); // 输出信息
InputStream in = socket.getInputStream(); // 接受服务端的消息
byte b[] = new byte[1024];
int len = in.read(b);
System.out.println("接受到服务器消息 : "+new String(b,0,len)); // 输出
out.write("返回的的数据已经收到 ".getBytes()); // 向服务器返回消息
socket.close();
}
}
改进服务端,启用多线程来接受客户端的数据
// server
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8886); // 建立服务端,ip为本机ip,端口为8886
int i=4;
while(i>2){
Socket accept = serverSocket.accept(); // 监听客户端的连接,一旦有客户端连接,则会返回客户端对应的accept
ServerThread st = new ServerThread(accept); // 启动线程
Thread th = new Thread(st);
th.start();
}
serverSocket.close();
}
// thread
public class ServerThread implements Runnable {
private Socket accept;
public ServerThread(Socket s) {
this.accept = s;
}
public void run(){
InputStream in;
try {
in = accept.getInputStream();
byte b[] = new byte[1024];
int len = in.read(b);
System.out.println("接受到客户端数据,返回数据" + new String(b, 0, len));
OutputStream out = accept.getOutputStream(); // 给客户端发送消息
out.write("服务端已经接受".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
传递图片
// 服务端
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(5555);
Socket ss = serverSocket.accept();
BufferedInputStream br = new BufferedInputStream(ss.getInputStream());
BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream("copy.jpg")); // 写出文件流
byte b[] = new byte[1024];
int len = 0;
while ((len = br.read(b)) != -1) { // 写出文件
bw.write(b, 0, len);
bw.flush(); // 别忘了刷新,要不然最后一块缓冲区字符串会缺失
}
BufferedWriter bw2 = new BufferedWriter(new OutputStreamWriter(ss.getOutputStream())); // 输出
bw2.write("图片传输成功");
bw2.flush();
bw.close();
ss.close();
}
}
// 客户端
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1", 5555);
BufferedInputStream in = new BufferedInputStream(new FileInputStream("c.jpg"));
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
byte b[] = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush(); // 刷新缓冲区 要不然最后一块缓冲区字符串会缺失
}
socket.shutdownOutput(); // 关闭流以后Server段才会接收道结束字符结束接受
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
in.close();
socket.close();
}
}
UDP实现
UDP是将数据打成数据包向对方发送,只关系是否发送成功,而不关心是否接收成功,传输速度快,但是可靠性低。
udp代码实现
// 发送端
public class SendDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(); // 此类表示用来发送和接收数据报包的套接字。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 键盘输入
String line = null;
while ((line = br.readLine()) != null) {
byte[] bytes = line.getBytes();
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("127.0.0.1"), 10005); // 数据包对象
ds.send(dp);
if ("886".equals(line)) { // 当输入886时结束发送
break;
}
}
ds.close();
}
}
// 接收端
public class ReciveDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10005); // 建立服务端
byte bytes[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length); // 建立数据包对象
while (true) {
ds.receive(dp); // 接受数据包
byte[] data = dp.getData(); // 获取数据
String str = new String(data, 0, dp.getLength());
if ("886".equals(str)) {
break;
}
System.out.println(str);
}
ds.close();
}
}