概述
一、基于socket通信——TCP
客户端输入一个hello,服务端进行输出
这个传输过程就是先建立一个会话,然后有一个流,基于TCP的套接字完成
//基于socket通信的服务端
public class ServerSocket_Server {
public static void main(String[] args) throws IOException {
//
ServerSocket serverSocket=null;
//
BufferedReader bufferedReader=null;
try {
//打开一个服务 8080端口
serverSocket=new ServerSocket(8080);
//接收一个客户端连接,等的客户端连接
//socket中基于流作为传输
Socket socket=serverSocket.accept();
//获得输入流
bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(bufferedReader.readLine());//输出
}catch (IOException e){
e.printStackTrace();
}finally {
if(bufferedReader!=null){
bufferedReader.close();
}
if(serverSocket!=null){
serverSocket.close();
}
}
}
}
//客户端
public class ServerSocket_Client {
public static void main(String[] args) throws IOException {
Socket socket=null;
try {
//用socket建立连接
socket=new Socket("127.0.0.1",8080);
//传输
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
out.println("hello");
}catch (Exception e){
e.printStackTrace();
}finally {
if(socket!=null){
socket.close();
}
}
}
}
二、基于DatagramSocket的通信——UDP
public class UDPServerDemo {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket=null;
try {
//创建服务,并且接收一个数据包
datagramSocket=new DatagramSocket(8080);
byte[] receiveData=new byte[1024];
//接收数据的数据包
DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
datagramSocket.receive(receivePacket);
//收到数据之后输出来
System.out.println(new String(receiveData,0,receivePacket.getLength()));
}finally {
if(datagramSocket!=null){
datagramSocket.close();
}
}
}
}
public class UDPClientDemo {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket=new DatagramSocket();
try {
//获得地址信息
InetAddress address=InetAddress.getByName("localhost");
byte[] sendData="Hello,whm".getBytes();
//要发送的数据包
DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,address,8080);
//发送
datagramSocket.send(sendPacket);
}catch (IOException e){
e.printStackTrace();
}finally {
if(datagramSocket!=null){
datagramSocket.close();
}
}
}
}
三、双向通信——全双工
public class DoubleSocketServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=null;
try {
serverSocket=new ServerSocket(8080);//监听端口号
//等待客户端连接
Socket socket=serverSocket.accept();
//通过BufferedReader拿到客户端输入流
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//输出流
PrintWriter os=new PrintWriter(socket.getOutputStream());
//通过控制台信息作为坐蓐
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
//拿到客户端信息
System.out.println("Client:"+is.readLine());
String line=sin.readLine();
while (!line.equals("bye")){
os.println(line);//输出数据
os.flush();//清空缓存
System.out.println("Server:"+line);
System.out.println("Client:"+is.readLine());
line=sin.readLine();
}
os.close();
is.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}finally {
if(serverSocket!=null){
serverSocket.close();
}
}
}
}
//客户端基于socket建立通信
public class DoubleSocketClient {
public static void main(String[] args) throws IOException {
Socket socket=new Socket("127.0.0.1",8080);
try {
//拿到输入流
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line=sin.readLine();
while (!line.equals("bye")){
os.println(line);
os.flush();//清空
System.out.println("Client:"+line);
System.out.println("Server:"+is.readLine());
line=sin.readLine();
}
}finally {
socket.close();
}
}
}
最后
以上就是腼腆音响为你收集整理的java 通信 协议_Java——通信协议的全部内容,希望文章能够帮你解决java 通信 协议_Java——通信协议所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复