简易网络通信框

实现分为七部分

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1.
package cn.cduestc.fire;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ClientInputThread extends Thread{
private Socket socket;
public ClientInputThread (Socket s) {
socket = s;
}
@Override
public void run(){
try {
InputStream is =socket.getInputStream();
byte[]buffer=new byte[1024];
int length;
while (true){
length = is.read();
String str = new String(buffer,0,length);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2.
package cn.cduestc.fire;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ClientOutputThread extends Thread{
private Socket socket;

public ClientOutputThread(Socket s) {
socket = s;
}

@Override
public void run(){
try {
InputStream is=socket.getInputStream();
byte[]buffer=new byte[1024];
int length;
while(true) {
length = is.read(buffer);
String str = new String(buffer, 0, length);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
3.
package cn.cduestc.fire;

import java.io.IOException;
import java.net.Socket;

public class MainClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 5000);

new ClientInputThread(socket).start();
new ClientOutputThread(socket).start();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
4.
package cn.cduestc.fire;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class MainServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(5000);

Socket socket=serverSocket.accept();

new ServerInputThread(socket).start();
new ServerOutputThread(socket).start();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
5.
package cn.cduestc.fire;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ServerInputThread extends Thread{
private Socket socket;

public ServerInputThread(Socket s) {
socket = s;
}

@Override
public void run(){
try {
InputStream is=socket.getInputStream();
byte[]buffer=new byte[1024];
int length;
while(true) {
length = is.read(buffer);
String str = new String(buffer, 0, length);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
6.
package cn.cduestc.fire;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class ServerOutputThread extends Thread{
private Socket socket;
public ServerOutputThread(Socket s){
socket=s;
}
@Override
public void run() {
try {
OutputStream os=socket.getOutputStream();
BufferedReader br=new BufferedReader(
new InputStreamReader(System.in));
while (true){
String line=br.readLine();//从键盘读入一行
os.write(line.getBytes(StandardCharsets.UTF_8));
}
} catch (IOException e) {
e.printStackTrace();
}

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
6.
package cn.cduestc.fire;
/*
socket编程:让开发网络更简单。屏蔽掉网络协议的复杂性
socket 被翻译为“套接字“编程

服务器端:
1.需要建立一个ServerSocket对象
2.在该对象上进行监听客户端连接请求。-----accept
3.由2返回一个连接到该客户端的客户套接字
4.可以获得套接字对应的输入输出流
5.进行输入输出
6.关闭流,关闭套接字符

客户端:
需要:
1.利用Socket类,创建一个连接到服务器端(IP:port)的套接字:socket
2.通过Socket得到该Socket对应的输入输出流
3.进行输入输出
4.关闭流,关闭套接字符





java.linux.c#支持几乎一样
端点,至少有两个
1.server 提供数据和服务端点
2.client 请求数据的客户端

B/S 或者 C/S构架做系统,都会端点区分

客户端发出数据请求,服务端响应应该请求

网址+端口。
*/
public class Story {
public static void main(String[] args) {
System.out.println("hello console ");
}
}