TCP实现网络通信
服务端
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) {
try {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
ByteArrayOutputStream 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());
baos.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
}
}
}
客户端
package tcpChatting;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TCPClientDemo01 {
public static void main(String[] args) {
try {
InetAddress serverIP = InetAddress.getByName("192.168.3.12");
int port = 9999;
Socket socket = new Socket(serverIP, port);
OutputStream os = socket.getOutputStream();
os.write("你好,欢迎学习Java Web编程!".getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Tomcat
服务端
- 自定义S
- Tomcat服务器S
客户端 - 自定义C
- 浏览器B
UDP消息发送
类似发短信,不用建立连接,但是要知道对方的地址。
package UDPTest;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClientDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String msg="号的";
InetAddress address = InetAddress.getByName("192.168.3.174");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.length(),address,port);
socket.send(packet);
socket.close();
}
}
接收消息
package UDPTest;
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());
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
UDP聊天实现
循环发送消息
package UDPChatting;
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 {
DatagramSocket socket = new DatagramSocket(8888);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("172.17.218.109", 6666));
socket.send(packet);
if(data.equals("bye")){
break;
}
}
socket.close();
}
}
循环接收消息
package UDPChatting;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceiveDemo01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);
byte[] data = packet.getData();
String receive_data = new String(data,0,data.length);
System.out.println(receive_data);
if(receive_data.equals("bye")){
break;
}
}
socket.close();
}
}
在线两个人都可以是发送方,都可以是接收方。
接收代码
package UDPChatting;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TalkReceive implements Runnable{
DatagramSocket socket= null;
private String msgFrom;
private int port;
public TalkReceive(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
}catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);
byte[] data = packet.getData();
String receive_data = new String(data, 0, data.length);
System.out.println(msgFrom+":"+receive_data);
if (receive_data.trim().equals("bye")) {
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
发送代码
package UDPChatting;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private String fromIP;
private String toIP;
private int toPort;
private int fromPort;
public TalkSend(String fromIP, String toIP, int toPort, int fromPort) {
this.fromIP = fromIP;
this.toIP = toIP;
this.toPort = toPort;
this.fromPort = fromPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try{
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
socket.send(packet);
if(data.equals("bye")){
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
开启多线程进行即时聊天。
package UDPChatting;
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend("192.168.3.136"," 172.17.218.109",9999,7777)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
另一端
package UDPChatting;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend("192.168.3.136","192.168.3.136",8888,5555)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
URL
https://www.baidu.com 统一资源定位符,用来定位资源的,定位互联网上的某一个资源。
协议://ip地址:端口号/项目名/资源
package URLTest;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLDownload {
public static void main(String[] args) throws Exception {
URL url = new URL("https://gitee.com/dinglide/Dummy-Robot/raw/main/5.Docs/1.Images/dummy.jpg");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("dummy.jpg");
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}
|