网络编程相关的类:
1、InetAddress
Java中用于描述IP的类
2、Socket
基于TCP协议的客户端的表示。
3、ServerSocket
基于TCP协议的服务端的表示。
TCP编程的流程
服务端:ip地址:192.168.1.100,port:9527
step1:创建ServerSocket,一个服务端的程序:
step2:等待客户端申请链接,accept()接收链接。——>Socket
step3:创建流:InputStream,OutputStream
step4:关闭链接,断开资源
客户端:ip地址:192.168.1.150,port:20000
step1:创建Socket,客户端的程序
申请链接服务器:(服务器的ip地址和port)
step2:创建流:InputStream,OutputStream
step3:关闭链接,断开资源
客户端:
public class Test1ClientECHO {
public static void main(String[] args) {
// step1:创建Socket类,申请链接服务器
Socket client = null;
BufferedWriter bw = null;
BufferedReader br = null;
try {
client = new Socket("192.168.220.1", 9527);
System.out.println("客户端已经将建立,申请链接服务器。。。");
// step2:获取流:InputStream,OutputStream,
//创建一个缓存流,用于读取键盘输入
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//写给服务器
bw = new BufferedWriter( new OutputStreamWriter(client.getOutputStream()));
//读服务器
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
//用于接收键盘输入的数据
String str = "";
//用于接收服务器的数据
String line = "";
while(true){
//A:客户端先读取键盘输入
str = input.readLine();//阻塞式
//B:将数据发送给服务器
bw.write(str);//不赖我
bw.newLine();//
bw.flush();//刷新
//判断
if("bye".equals(str)){
System.out.println("客户端即将结束。。。");
break;
}
//E:客户端接收服务器的回应
line = br.readLine();//阻塞
System.out.println("服务器回应:"+line);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(br!= null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(client != null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器端
public class Test1ServerECHO {
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
// 1.step1:创建ServerSocket类
ServerSocket serverSocket = null;
Socket socket = null;
serverSocket = new ServerSocket(9527);
System.out.println("服务器已经建立,等待客户端链接。。");
// step2:等待客户端链接:accept()——>Socket
socket = serverSocket.accept();//阻塞式
System.out.println("已有客户端连入。。。" + socket.getInetAddress() + ",#" + socket.getPort());
// step3:获取流:InputStream,OutputStream,
// 用于读取客户端输入的数据
// 用于向客户端写出数据
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// 用于表示接收数据
while(true){
// C:服务器接收客户端传来的数据
String line = br.readLine();//阻塞式
System.out.println("客户端说:" + line);
//判断
if("bye".equals(line)){
System.out.println("客户端跑啦。。。");
break;
}
// D:服务器写客户端
System.out.println("输入");
String str =sc.next();
bw.write(str);
bw.newLine();
bw.flush();
System.out.println("服务器已经回应。。");
}
// 先关闭缓存流
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
// 再关闭socket
if (socket != null) {
socket.close();
}
}
}
上传文件&图片
服务端
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器Socket对象
ServerSocket ss = new ServerSocket(10000);
//监听客户端连接,返回一个对应的Socket对象
Socket s = ss.accept();
System.out.println("已有客户端连入。。"+s.getInetAddress()+","+s.getPort());
//接收数据
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
//把数据写入文本文件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("english.txt"));
int sum = 0;
int data = 0;
while((data = bis.read()) !=-1){
bos.write(data);
sum++;
}
bos.flush();
System.out.println("服务器接收完毕。。"+sum);
if(bis != null){
bis.close();
}
if(bos != null){
bos.close();
}
if(s != null){
s.close();
}
}
}
客户端
public class ClientDemo {
public static void main(String[] args) throws Exception {
//创建客户端Socket对象
Socket s = new Socket("192.168.220.1",10000);
System.out.println("客户端已经建立,申请链接服务器。。");
//封装文本文件的数据
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test\\pro\\english.txt"));
//封装输出流写数据
BufferedOutputStream bos = new BufferedOutputStream (s.getOutputStream());
int sum = 0;
int data = 0;
while ((data=bis .read())!=-1) {
bos.write(data);
sum++;
}
bos.flush();
//接收反馈
System.out.println("客户端写完了。。"+sum);
if(bis != null){
bis.close();
}
if(bos != null){
bos.close();
}
if(s != null){
s.close();
}
}
}