学习目标:
一周掌握 Tcp协议 入门知识
学习内容:
第一题:实现简单客户端发送一条消息,服务器接受并反馈给客 户端进行输出
客户端:
//客户端
package rrr;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SocKet {
public static void main(String[] args) throws IOException {
//创建结尾后数据socket 端口号于发送端一致
ServerSocket serverSocket = new ServerSocket(2001);
//监听客户链接
Socket accept = serverSocket.accept();
//获取输入流,读取数据,一次读取一个字节数组
InputStream inputStream = accept.getInputStream();
byte[] a=new byte[1024];
// 返回实际读取到的长度
int read = inputStream.read(a);
System.out.println(new String(a,0,read));
// 反馈数据
OutputStream outputStream = accept.getOutputStream();
outputStream.write("反馈的数据是".getBytes());
// 关闭服务器,释放资源
accept.close();
}
}
//服务器
package rrr;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
public class ServerSocket {
public static void main(String[] args) throws ParseException, IOException {
// 返回本地主机。
InetAddress localHost = InetAddress.getLocalHost();
String hostAddress = localHost.getHostAddress();
//创建客户端Socket
Socket socket = new Socket(hostAddress, 2001);
// 创建输出流
OutputStream outputStream = socket.getOutputStream();
outputStream.write("发送的数据".getBytes());
//获得反馈
InputStream inputStream =socket.getInputStream();
byte[] a=new byte[1024];
int read = inputStream.read(a);
System.out.println(new String(a,0,read));
//关闭客户端
socket.close();
}
}
服务器:
第二题:客户端的数据来源于键盘录入,直到输入over为止,服务器 不断地接收数据
//客户端
package rrr;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class SocKet {
public static void main(String[] args) throws IOException {
InetAddress localHost = InetAddress.getLocalHost();
Socket socket = new Socket(localHost, 1001);
//键盘录入
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String c;
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while ((c = bufferedReader.readLine()) != null) {
//如果等于over就关闭
if (c.equals("over")) {
socket.close();
break;
}
bufferedWriter.write(c);
bufferedWriter.newLine();//换行
bufferedWriter.flush();//刷新
}
}
}
//服务器
package rrr;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSocket1z {
public static void main(String[] args) throws IOException, IOException {
ServerSocket serverSocket = new ServerSocket(1001);
Socket accept = serverSocket.accept();
BufferedReader bufferedWriter = new BufferedReader(new InputStreamReader(accept.getInputStream()));
String c;
// 读取一行
while ((c=bufferedWriter.readLine())!=null){
System.out.println(c);
}
accept.close();
}
}
第三题:客户端的数据来自键盘录入,直到over为止,服务器端读取传过来的数据,保存在文本文件中;
//客户端
package rrr;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class Socket1z {
public static void main(String[] args) throws IOException {
InetAddress localHost = InetAddress.getLocalHost();
Socket socket = new Socket(localHost,50);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String c;
while ((c=bufferedReader.readLine())!=null){
if (c.equals("over")){
socket.close();
break;
}
bufferedWriter.write(c);
bufferedWriter.newLine();
bufferedWriter.flush();
}
}
}
//服务器
package rrr;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSocKet1 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(50);
Socket accept = serverSocket.accept();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(accept.getInputStream()));
BufferedWriter ddd=new BufferedWriter(new FileWriter("D:\\aa.text"));
String c;
while ((c=bufferedReader.readLine())!=null){
ddd.write(c);
ddd.newLine();
ddd.flush();
System.out.println(c);
}
}
}
学习时间:
提示:这里可以添加计划学习的时间
例如:
- 周一至周五晚上 7 点—晚上9点
- 周六上午 9 点-上午 11 点
- 周日下午 3 点-下午 6 点
学习产出:
提示:这里统计学习计划的总量
例如:
- 技术笔记 2 遍
- CSDN 技术博客 3 篇
- 习的 vlog 视频 1 个