Skip to content

Commit b66f3f5

Browse files
committed
java网络编程部分代码
1 parent ba029ed commit b66f3f5

7 files changed

Lines changed: 318 additions & 0 deletions

File tree

src/main/java/Maintest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.io.File;
2+
import java.lang.reflect.InvocationTargetException;
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
import java.util.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Queue;
9+
10+
/**
11+
* @author :chenxin
12+
* @date :Created in 2020/3/28 12:20
13+
* @description:
14+
* @modified By:
15+
* @version: $
16+
*/
17+
public class Maintest {
18+
19+
20+
public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
21+
final URL resource = Maintest.class.getResource("");
22+
System.out.println(resource);
23+
24+
int offset = Maintest.class.getResource("/").getPath().length();
25+
26+
Queue<File> files = new LinkedList<>();
27+
files.add(new File(resource.getFile()));
28+
List<Object> beans = new ArrayList<>();
29+
30+
while (!files.isEmpty()) {
31+
File tmp = files.poll();
32+
for (File f : tmp.listFiles()) {
33+
if (f.isDirectory()) {
34+
files.add(f);
35+
} else {
36+
if (f.getName().endsWith(".class")) {
37+
38+
String clsName = f.toURI().toURL().getPath().substring(offset).replaceAll("/", ".").replace(".class", "");
39+
System.out.println(clsName);
40+
final Class<?> aClass = Maintest.class.getClassLoader().loadClass(clsName);
41+
if (!aClass.isInterface()) {
42+
beans.add(aClass.getConstructor().newInstance());
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
System.out.println(offset);
50+
System.out.println();
51+
}
52+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package netWork.tcp;
2+
3+
import java.io.*;
4+
import java.net.InetAddress;
5+
import java.net.Socket;
6+
7+
/**
8+
* TCP客户端
9+
*
10+
* @author :chenxin
11+
* @date :Created in 2020/3/29 21:17
12+
* @description:TCP客户端
13+
* @modified By:
14+
* @version: $
15+
*/
16+
public class TcpClient {
17+
18+
public static void main(String[] args) throws Exception {
19+
//需要服务器端先开启
20+
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8002);
21+
//同一个通道,服务端的输出流就是客户端的输入流,服务端的输入流就是客户端的输出流
22+
InputStream inputStream = socket.getInputStream();
23+
BufferedReader brNet = new BufferedReader(new InputStreamReader(inputStream));
24+
25+
//开启通道的输出流
26+
OutputStream ops = socket.getOutputStream();
27+
DataOutputStream dos = new DataOutputStream(ops);
28+
final BufferedReader brKey = new BufferedReader(new InputStreamReader(System.in));
29+
while (true) {
30+
String strWord = brKey.readLine();
31+
if ("quit".equalsIgnoreCase(strWord)) {
32+
break;
33+
} else {
34+
System.out.println("I want to send " + strWord);
35+
dos.writeBytes(strWord + System.getProperty("line.separator"));
36+
System.out.println("Server said " + brNet.readLine());
37+
}
38+
39+
}
40+
41+
dos.close();
42+
ops.close();
43+
brNet.close();
44+
inputStream.close();
45+
socket.close();
46+
47+
48+
}
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package netWork.tcp;
2+
3+
import java.io.*;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
7+
/**
8+
* tcp服务器端
9+
*
10+
* @author :chenxin
11+
* @date :Created in 2020/3/29 21:11
12+
* @description:tcp服务器端
13+
* @modified By:
14+
* @version: $
15+
*/
16+
public class TcpServer {
17+
public static void main(String[] args) {
18+
try {
19+
//创建一个绑定到8001端口的TCP服务器
20+
ServerSocket ss = new ServerSocket(8001);
21+
//开启监听
22+
Socket s = ss.accept();
23+
24+
System.out.println("Welcome to Java world");
25+
//有人连上来,打开输入流
26+
InputStream ins = s.getInputStream();
27+
//打开输出流
28+
OutputStream ops = s.getOutputStream();
29+
30+
ops.write("Hello ,Client!".getBytes());
31+
32+
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
33+
34+
System.out.println("Client said: " + br.readLine());
35+
36+
ins.close();
37+
ops.close();
38+
s.close();
39+
ss.close();
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
}
43+
44+
}
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package netWork.tcp;
2+
3+
import java.net.ServerSocket;
4+
import java.net.Socket;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
8+
/**
9+
* TCP服务端可以处理多个请求
10+
*
11+
* @author :chenxin
12+
* @date :Created in 2020/3/29 21:33
13+
* @description:TCP服务端可以处理多个请求
14+
* @modified By:
15+
* @version: $
16+
*/
17+
public class TcpServerConcurrent {
18+
public static void main(String[] args) {
19+
try {
20+
ServerSocket ss = new ServerSocket(8002);
21+
final ExecutorService executorService = Executors.newCachedThreadPool();
22+
while (true) {
23+
final Socket s = ss.accept();
24+
System.out.println("来了一个client");
25+
executorService.submit(new Worker(s));
26+
}
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package netWork.tcp;
2+
3+
import java.io.*;
4+
import java.net.Socket;
5+
6+
/**
7+
* 每个socket的工作
8+
*
9+
* @author :chenxin
10+
* @date :Created in 2020/3/29 21:38
11+
* @description:每个socket的工作
12+
* @modified By:
13+
* @version: $
14+
*/
15+
public class Worker implements Runnable {
16+
17+
private Socket socket;
18+
19+
public Worker(Socket socket) {
20+
this.socket = socket;
21+
}
22+
23+
@Override
24+
public void run() {
25+
try {
26+
System.out.println("服务人员已启动");
27+
//同一个通道,服务端的输出流就是客户端的输入流,服务端的输入流就是客户端的输出流
28+
InputStream ips = socket.getInputStream();
29+
BufferedReader br = new BufferedReader(new InputStreamReader(ips));
30+
31+
//开启通道的输出流
32+
OutputStream ops = socket.getOutputStream();
33+
DataOutputStream dos = new DataOutputStream(ops);
34+
35+
while (true) {
36+
String s = br.readLine();
37+
System.out.println("client said :" + s + ":" + s.length());
38+
if ("quit".equalsIgnoreCase(s)) {
39+
break;
40+
}
41+
String strEcho = s + " 666";
42+
System.out.println("server said:" + strEcho + " :" + strEcho.length());
43+
dos.writeBytes(s + "--->" + strEcho + System.getProperty("line.separator"));
44+
}
45+
br.close();
46+
dos.close();
47+
ops.close();
48+
ips.close();
49+
socket.close();
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
}
53+
54+
}
55+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package netWork.udp;
2+
3+
import java.net.DatagramPacket;
4+
import java.net.DatagramSocket;
5+
import java.net.InetAddress;
6+
7+
/**
8+
* upd发送
9+
*
10+
* @author :chenxin
11+
* @date :Created in 2020/3/29 20:13
12+
* @description:upd发送
13+
* @modified By:
14+
* @version: $
15+
*/
16+
public class UdpSend {
17+
18+
public static void main(String[] args) throws Exception {
19+
DatagramSocket ds = new DatagramSocket();
20+
21+
String str = "hello world";
22+
23+
final DatagramPacket datagramPacket = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName("127.0.0.1"), 3000);
24+
System.out.println("UdpSend:我要发信息");
25+
ds.send(datagramPacket);
26+
System.out.println("UdpSend:我发送消息结束");
27+
28+
Thread.sleep(1000);
29+
30+
byte[] buf = new byte[1024];
31+
DatagramPacket dp2 = new DatagramPacket(buf, 1024);
32+
System.out.println("UdpSend:我等待接收信息");
33+
ds.receive(dp2);
34+
System.out.println("UdpSend:我接收到消息");
35+
36+
String strRecv = new String(dp2.getData(), 0, dp2.getLength()) +
37+
" from " + dp2.getAddress().getHostAddress() + ":" + dp2.getPort();
38+
39+
System.out.println(strRecv);
40+
41+
ds.close();
42+
43+
}
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package netWork.udp;
2+
3+
import java.io.IOException;
4+
import java.net.DatagramPacket;
5+
import java.net.DatagramSocket;
6+
import java.net.InetAddress;
7+
import java.net.SocketException;
8+
9+
/**
10+
* UDP协议接收方
11+
*
12+
* @author :chenxin
13+
* @date :Created in 2020/3/29 20:05
14+
* @description:UDP协议接收方
15+
* @modified By:
16+
* @version: $
17+
*/
18+
public class UpdRecv {
19+
public static void main(String[] args) throws Exception {
20+
DatagramSocket ds = new DatagramSocket(3000);
21+
byte[] buf = new byte[1024];
22+
DatagramPacket dp = new DatagramPacket(buf, 1024);
23+
24+
System.out.println("UdpRecv:我在等待消息");
25+
ds.receive(dp);
26+
System.out.println("UdpRecv:我接收到消息");
27+
28+
String strRecv = new String(dp.getData(), 0, dp.getLength()) +
29+
" from " + dp.getAddress().getHostAddress() + ":" + dp.getPort();
30+
31+
System.out.println(strRecv);
32+
33+
Thread.sleep(1000);
34+
System.out.println("UdpRecv:我要发送消息");
35+
String str = "hello world 222";
36+
final DatagramPacket datagramPacket = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName("127.0.0.1"), dp.getPort());
37+
ds.send(datagramPacket);
38+
System.out.println("UdpRecv:我发送消息结束");
39+
ds.close();
40+
41+
}
42+
}

0 commit comments

Comments
 (0)