UDP通信
发送:
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
/*
@author Nian
@Date 2022/8/10 14:41
@purpose
@Note
*/
public class UDPSender {
public static void main(String[] args) {
try {
// send();
send2();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void send() throws IOException {
DatagramSocket datagramSocket = new DatagramSocket();
String message = "哈哈哈哈哈哈哈哈";
byte[] bytes = message.getBytes();
int len = bytes.length;
int port = 10086;
InetAddress inetAddress = InetAddress.getLocalHost();
DatagramPacket datagramPacket = new DatagramPacket(bytes,len,inetAddress,port);
datagramSocket.send(datagramPacket);
datagramSocket.close();
}
public static void send2() throws IOException {
DatagramSocket datagramSocket = new DatagramSocket();
int port = LocalParameter.PORT;
String message = "哈哈哈1";
byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
int len = bytes.length;
InetAddress inetAddress = InetAddress.getLocalHost();
DatagramPacket datagramPacket = new DatagramPacket(bytes,len,inetAddress,port);
datagramSocket.send(datagramPacket);
}
}
接收:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
/*
@author Nian
@Date 2022/8/10 14:42
@purpose UDP receiver
@Note
*/
public class UDPReceiver {
public static void main(String[] args) {
String path = "D:" + File.separator + "receive.txt";
try {
// receive(path);
receive2();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void receive(String path) throws IOException {
int port = 10086;
DatagramSocket datagramSocket = new DatagramSocket(port);
byte[] bytes = new byte[1024*1024];
int len = bytes.length;
DatagramPacket datagramPacket = new DatagramPacket(bytes,len);
datagramSocket.receive(datagramPacket);
int length = datagramPacket.getLength();
String s = new String(bytes,0,length);
System.out.println(s);
FileOutputStream fileOutputStream = new FileOutputStream(path,true);
fileOutputStream.write(bytes,0,length);
fileOutputStream.close();
datagramSocket.close();
}
public static void receive2() throws Exception{
DatagramSocket datagramSocket = new DatagramSocket(LocalParameter.PORT);
byte[] bytes = new byte[1024];
int len = bytes.length;
DatagramPacket datagramPacket = new DatagramPacket(bytes,len);
datagramSocket.receive(datagramPacket);
int length = datagramPacket.getLength();
String s = new String(bytes,0,length, StandardCharsets.UTF_8);
System.out.println(s);
}
}
TCP通信:
客户端:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
/*
@author Nian
@Date 2022/8/10 15:17
@purpose
@Note
*/
public class Client {
public static void main(String[] args) {
try {
// TCPAccept();
send();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void TCPAccept() throws IOException {
String message = "hello";
InetAddress inetAddress = InetAddress.getLocalHost();
//build Socket by ip and port
Socket socket = new Socket(inetAddress,LocalParameter.PORT);
//use socket get OutputStream
OutputStream outputStream = socket.getOutputStream();
byte[] bytes = message.getBytes();
outputStream.write(bytes);
//get output stream and send message
InputStream inputStream = socket.getInputStream();
byte[] bytes1 = new byte[1024];
int len;
if((len = inputStream.read(bytes1))!= -1){
String s = new String(bytes1,0,len, StandardCharsets.UTF_8);
System.out.println(s);
}
outputStream.flush();
outputStream.close();
}
public static void send() throws IOException {
InetAddress inetAddress = InetAddress.getLocalHost();
Socket socket = new Socket(inetAddress,LocalParameter.PORT);
OutputStream outputStream = socket.getOutputStream();
String s = "请求服务器应答";
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
outputStream.write(bytes);
socket.close();
}
}
服务端:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
/*
@author Nian
@Date 2022/8/10 15:18
@purpose
@Note
*/
public class Server {
public static void main(String[] args) {
try {
// receiveMessage();
receive();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void receiveMessage() throws IOException {
ServerSocket socket = new ServerSocket(LocalParameter.PORT);
System.out.println("before");
Socket accept = socket.accept();
InputStream inputStream = accept.getInputStream();
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
String s = new String(bytes,0,len);
System.out.println(s);
OutputStream outputStream = accept.getOutputStream();
String s1 = "通讯完毕";
outputStream.write(s1.getBytes(StandardCharsets.UTF_8));
// inputStream.close();
accept.close();
socket.close();
}
public static void receive() throws IOException {
ServerSocket serverSocket = new ServerSocket(LocalParameter.PORT);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes))!= -1){
String s = new String(bytes,0,len,StandardCharsets.UTF_8);
System.out.println(s);
}
accept.close();
serverSocket.close();
}
}