Java 中网络编程接口在java.net 包中
在使用C/C++进行网络编程时,针对TCP Server端需要这些操作
- 创建SOCKET
- 绑定
- 监听
- 接受连接
- 收取数据包
- 发送数据包
TCP Client端需要这些操作
- 创建SOCKET
- 连接Server端
- 发送数据包
- 读取响应包
Java中针对Server 端和Client端分别提供了两个类 ServerSocket
和 Socket
ServerScoket
使用步骤如下:
- 创建ServerSocket 对象并传入一个端口号到构造函数中。在构造的时候会自动创建Socket对象并执行绑定端口、监听端口的操作
- 调用对象的
accept
方法等待连接 - 调用对象的
getInputStream
和getOutputStream
获取输入输出流,并通过输入输出流来进行收发数据 - 在不用时调用
close
方法关闭套接字
Socket
类使用步骤如下:
- 创建
Socket
对象 - 调用
connet
方法连接到指定服务器端口(或者在构造时传入服务器和端口进行连接) - 调用对象的
getInputStream
和getOutputStream
获取输入输出流,并通过输入输出流来进行收发数据 - 在不用时调用
close
方法关闭套接字
一个普通的TCP通信的实例如下:
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();
}
}
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协议进行文件传输的例子:
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();
}
}
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 的例子
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
".getBytes());
os.write("Content-Type:text/html
".getBytes());
os.write("
".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();
}
}