Java 网络编程

Java 中网络编程接口在java.net 包中

在使用C/C++进行网络编程时,针对TCP Server端需要这些操作

  1. 创建SOCKET
  2. 绑定
  3. 监听
  4. 接受连接
  5. 收取数据包
  6. 发送数据包

TCP Client端需要这些操作

  1. 创建SOCKET
  2. 连接Server端
  3. 发送数据包
  4. 读取响应包

Java中针对Server 端和Client端分别提供了两个类 ServerSocketSocket

ServerScoket 使用步骤如下:

  1. 创建ServerSocket 对象并传入一个端口号到构造函数中。在构造的时候会自动创建Socket对象并执行绑定端口、监听端口的操作
  2. 调用对象的 accept 方法等待连接
  3. 调用对象的 getInputStreamgetOutputStream 获取输入输出流,并通过输入输出流来进行收发数据
  4. 在不用时调用 close 方法关闭套接字

Socket 类使用步骤如下:

  1. 创建 Socket 对象
  2. 调用 connet 方法连接到指定服务器端口(或者在构造时传入服务器和端口进行连接)
  3. 调用对象的 getInputStreamgetOutputStream 获取输入输出流,并通过输入输出流来进行收发数据
  4. 在不用时调用 close 方法关闭套接字

一个普通的TCP通信的实例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;

public class TCPServer{
public static void main(String[] args) throws IOException{
ServerSocket server = new ServerSocket(6666);
Socket socket = server.accept() ;

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();

byte[] bytes = new byte[1024];
int len = is.read(bytes);
os.write(("echo:" + new String(bytes, 0, len)).getBytes());

server.close();
socket.close();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.net.Socket;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;

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

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();

os.write("hello world!".getBytes());
byte[] bytes = new byte[1024];
int len = is.read(bytes);

System.out.println(new String(bytes, 0, len));

socket.close();
}
}

下面是一个使用TCP协议进行文件传输的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.net.Socket;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;

public class FileUploadServer{
public static void main(String[] args) throws IOException{
ServerSocket server = new ServerSocket(6666);
FileOutputStream fos = new FileOutputStream("1.avi");
Socket socket = server.accept();
InputStream is = socket.getInputStream();
byte[] buff = new byte[1024];
int len = 0;
while((len = is.read(buff)) != -1){
fos.write(buff, 0, len);
}

System.out.println("upload success!");
fos.close();
server.close();
socket.close();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.net.Socket;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.OutputStream;

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

FileInputStream fis = new FileInputStream("1.avi");
OutputStream os = socket.getOutputStream();
int len = 0;
byte[] buff = new byte[1024];
while((len = fis.read(buff)) != -1){
os.write(buff, 0, len);
}

fis.close();
socket.close();
}
}

最后提供一个简易的http server 的例子

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
42
43
import java.net.Socket;
import java.net.ServerSocket;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.BufferedReader;
import java.io.IOException;

public class HttpServer{
public static void main(String[] args)throws IOException{
ServerSocket server = new ServerSocket(8080);
while(true){
Socket socket = server.accept();
new Thread(()->{
try{
BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
OutputStream os = socket.getOutputStream();
String head = bf.readLine();
String path = head.split(" ")[1].substring(1);
System.out.println(path);
FileInputStream fis = new FileInputStream(path);
int len = 0;
byte[] buff = new byte[1024];
os.write("HTTP/1.1 200 OK\r\n".getBytes());
os.write("Content-Type:text/html\r\n".getBytes());
os.write("\r\n".getBytes());
while((len = fis.read(buff)) != -1){
os.write(buff, 0, len);
}

bf.close();
fis.close();
socket.close();
}catch(IOException e){
e.printStackTrace();
}
}).start();
}

//server.close();
}
}