1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 笔记-15 网络编程入门 UDP通信程序 TCP通信程序 练习

笔记-15 网络编程入门 UDP通信程序 TCP通信程序 练习

时间:2018-04-24 21:20:58

相关推荐

笔记-15 网络编程入门 UDP通信程序 TCP通信程序  练习

问答题

题目1

请写出端口号的范围

0 ~ 65535

题目2

判断下列说法是否正确:

由于TCP是面向连接的协议,可以保证数据的完整性,因此在传输重要数据时建议采用TCP协议.

正确,TCP协议是面向连接的,有3次握手的过程,可以保证数据的完整性;

题目3

TCP协议中”三次握手”,指的是什么?

第一次握手,客户端向服务器端发出连接请求,等待服务器确认第二次握手,服务器端向客户端回送一个响应,通知客户端收到了连接请求第三次握手,客户端再次向服务器端发送确认信息,确认连接对应的代码就是 创建客户端对象的时候,就会自动完成”三次握手”的操作;如果不能完成”三次握手”,那么创建对象会失败;

综合编程题

题目4

假如你需要设计一个小游戏,当客户端将一段文字发送给服务器后,服务器会将文字反转然后再发回给客户端;请使用程序实现效果;

要求:

客户端通过键盘输入得到一个字符串发给服务器.(键盘输入不要求循环,发一次即可,如果想增强,也可以把键盘输入加上循环,并自己制定结束的标记)

效果:

参考代码:

public class MyClient {public static void main(String[] args) throws IOException {Socket s = new Socket("127.0.0.1",8888);//键盘输入数据,并发给服务器Scanner sc = new Scanner(System.in);System.out.println("请输入一句话:");String next = sc.next();s.getOutputStream().write(next.getBytes());s.shutdownOutput();//让服务器端结束循环读的行为//接受服务器的响应InputStream in = s.getInputStream();byte[] arr = new byte[1024*8];int i;System.out.print("我是客户端,我收到的服务器响应是:");while ((i=in.read(arr))!=-1){System.out.print(new String(arr,0,i));}System.out.println();s.close();}}class MyServer{public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(8888);Socket s = ss.accept();//接受客户端请求//读数据InputStream in = s.getInputStream();byte[] arr = new byte[1024*8];int i;//创建sb,用于保存每次循环收到的客户端发送的数据StringBuilder sb = new StringBuilder();while ((i=in.read(arr))!=-1){sb.append(new String(arr,0,i));}System.out.print("我是服务器端,我收到的客户端请求是:");System.out.println(sb);s.getOutputStream().write(sb.reverse().toString().getBytes());s.shutdownOutput();s.close();ss.close();}}

题目5

假如你想通过程序和同桌建立沟通,请设计一个程序模拟飞秋的聊天功能;

提示:

客户端和服务器端分别都需要写两个线程呦;

要求:

1:客户端(你)需求: 能够通过键盘录入不断地向服务端发送数据.也能接收到服务端发过来的数据.

2:服务端(你同桌)要求: 能够接收客户端的信息.能够通过键盘录入向浏览器发送数据.

3:输入"886" 结束聊天,停止程序;

效果:

参考代码:

public class MyClient {public static void main(String[] args) throws IOException {Socket s = new Socket("127.0.0.1",9999);//创建一个线程,专门负责给服务器发送数据new Thread(()->{try {//键盘输入数据,并发给服务器Scanner sc = new Scanner(System.in);while (true){System.out.println("我是客户端,请输入您想对服务器说的话:");String next = sc.next();if("886".equals(next)){System.out.println("客户端结束输入了!");break;}s.getOutputStream().write(next.getBytes());}s.shutdownOutput();//让服务器端结束循环读的行为} catch (Exception e) {e.printStackTrace();}}).start();//创建一个线程,专门负责读服务器数据new Thread(()->{try {//接受服务器的响应InputStream in = s.getInputStream();byte[] arr = new byte[1024*8];int i;while ((i=in.read(arr))!=-1){System.out.print("我是客户端,我收到的服务器响应是:");System.out.println(new String(arr,0,i));}System.out.print("我是客户端,我停止接受服务器消息了");} catch (Exception e) {e.printStackTrace();}}).start();}}class MyServer{public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(9999);Socket s = ss.accept();//接受客户端请求//创建一个线程,专门负责读客户端发送的数据new Thread(()->{try {//读客户端的数据InputStream in = s.getInputStream();byte[] arr = new byte[1024*8];int i;while ((i=in.read(arr))!=-1){System.out.print("我是服务器,我收到客户端发来的信息是:");System.out.println(new String(arr,0,i));}System.out.print("我是服务器,我停止接受客户端消息了!");} catch (Exception e) {e.printStackTrace();}}).start();//给客户端发数据new Thread(()->{try {//键盘输入数据,并发给客户端Scanner sc = new Scanner(System.in);while (true){System.out.println("我是服务器,请输入您想对客户端说的话:");String next = sc.next();if("886".equals(next)){System.out.println("服务器结束输入了!");break;}s.getOutputStream().write(next.getBytes());}s.shutdownOutput();} catch (Exception e) {e.printStackTrace();}}).start();}}

题目6

模拟网站下载图片的功能,先让客户端给服务器发请求,告诉服务器要下载的文件名,然后服务器就给客户端传递文件,直至传递完成;

要求:

1:服务器使用线程池完成,每次客户请求下载文件时,就使用一个线程与客户端交互;

2:要求客户端保存文件时文件名使用UUID随机生成;

效果:

参考代码:

public class MyClient {public static void main(String[] args) throws IOException {Socket s = new Socket("127.0.0.1",6666);//键盘输入数据,并发给服务器Scanner sc = new Scanner(System.in);System.out.println("我是客户端,请输入您想下载的文件名:");String next = sc.next();s.getOutputStream().write((next).getBytes());s.shutdownOutput();//读服务器的数据并写入本地文件InputStream in = s.getInputStream();byte[] arr = new byte[1024*8];BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("D:\\"+ UUID.randomUUID().toString().replace("-","")+".jpg"));int i;while ((i=in.read(arr))!=-1){bout.write(arr,0,i);bout.flush();}bout.close();s.close();System.out.print("我是客户端,我下载文件成功了!");}}class MyServer{public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(6666);ExecutorService pool = Executors.newFixedThreadPool(10);while (true){System.out.println("服务器已经就绪,等待客户端请求下载...");Socket s = ss.accept();//接受客户端请求pool.submit(()->{try {//读客户端的数据InputStream in = s.getInputStream();byte[] arr = new byte[1024*8];int i=in.read(arr);String name = new String(arr, 0, i);System.out.println("我是服务器,我收到客户端发来的想要下载的文件名是:"+name);//开始给客户端响应文件信息OutputStream out = s.getOutputStream();BufferedInputStream bin = new BufferedInputStream(new FileInputStream("E:\\"+name));while ((i=bin.read(arr))!=-1){out.write(arr,0,i);}s.shutdownOutput();System.out.println("我是服务器的"+Thread.currentThread().getName()+"线程,本次下载任务已完成!");} catch (Exception e) {e.printStackTrace();}});}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。