-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
meng.wu1
committed
Oct 9, 2022
1 parent
057c6e8
commit 5025e71
Showing
57 changed files
with
1,370 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.mirkowu.xsocekt; | ||
|
||
import java.net.DatagramPacket; | ||
import java.net.DatagramSocket; | ||
import java.net.InetAddress; | ||
import java.util.Scanner; | ||
|
||
public class ClientDemo1 { | ||
public static void main(String[] args) throws Exception { | ||
System.out.println("=====客户端启动======"); | ||
|
||
// 1、创建发送端对象:发送端自带默认的端口号 | ||
DatagramSocket socket = new DatagramSocket(); | ||
|
||
Scanner sc = new Scanner(System.in); | ||
while (true) { | ||
System.out.println("请说:"); | ||
String msg = sc.nextLine(); | ||
|
||
if("exit".equals(msg)){ | ||
System.out.println("离线成功!"); | ||
socket.close(); | ||
break; | ||
} | ||
|
||
// 2、创建一个数据包对象封装数据 | ||
byte[] buffer = msg.getBytes(); | ||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, InetAddress.getLocalHost() , 8888); | ||
|
||
// 3、发送数据出去 | ||
socket.send(packet); | ||
} | ||
|
||
socket.close(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.mirkowu.xsocekt; | ||
|
||
import java.net.DatagramPacket; | ||
import java.net.DatagramSocket; | ||
|
||
public class ServerDemo1 { | ||
public static void main(String[] args) throws Exception { | ||
System.out.println("=====服务端启动======"); | ||
// 1、创建接收端对象:注册端口 | ||
DatagramSocket socket = new DatagramSocket(8888); | ||
|
||
// 2、创建一个数据包对象接收数据 | ||
byte[] buffer = new byte[1024 * 64]; | ||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length); | ||
|
||
while (true) { | ||
// 3、等待接收数据。 | ||
socket.receive(packet); | ||
// 4、取出数据即可 | ||
// 读取多少倒出多少 | ||
int len = packet.getLength(); | ||
String rs = new String(buffer,0, len); | ||
System.out.println("收到了来自:" + packet.getAddress() +", 对方端口是" + packet.getPort() +"的消息:" + rs); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.