JAVA之旅(三十三)_第1页
JAVA之旅(三十三)_第2页
JAVA之旅(三十三)_第3页
JAVA之旅(三十三)_第4页
JAVA之旅(三十三)_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JAVA之旅(三十三)一.TCP说完UDP,我们就来说下我们应该重点掌握的TCP了TCP传输 Socket和ServiceSocket建立客户端和服务端建立连接后,通过Socket中的IO流进行数据的传输关闭Socket同样的,我们的客户端和服务端都是两个独立的应用我们通过查阅API文档发现,该对象在建立的时候,就可以去连接指定主机,因为tcp是面向连接的,所以在建立socket服务时,就要有服务存在,并成功连接,形成通路后,在该通道进行数据传输所以我们用代码来看下他的步骤客户端package com.lgl.hellojava;import java.io.IOException;impor

2、t java.io.InputStream;import java.io.OutputStream;import .ServerSocket;import .Socket;import .UnknownHostException;public class TcpClient public static void main(String args) try /1.创建客户端的服务,传地址和端口 Socket s = new Socket("192.168.1.102",10000); /2.为了发送数据,应该获得socket流中的输出流 OutputStream out =

3、s.getOutputStream(); out.write("你好".getBytes(); s.close(); catch (UnknownHostException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 服务端package com.lgl.hellojava;import java.io.IOException;import java.

4、io.InputStream;import .ServerSocket;import .Socket;/* * 定义端点接收数据打印出来 * 服务端: * 1.建立服务端的socket服务,servicesocket,并监听一个端口 * 2.获取连接过来的客户端对象,通过accept方法,这个方法是阻塞的,没有连接就会等 * 3.客户端如果发过来数据,那么服务端要使用对应的客户端对象,并获取到该对象的读取流 * 4.关闭服务端(可选操作) * author LGL * */public class TcpService public static void main(String args)

5、try /1.建立连接,监听端口 ServerSocket ss = new ServerSocket(10000); /2.连接客户端对象 Socket accept = ss.accept(); /获取ip String ip = accept.getInetAddress().getHostAddress(); /3.获取客户端发送过来的数据 InputStream in = accept.getInputStream(); /4.开始读取 byte buf = new byte1024; int len = in.read(buf); System.out.println(new St

6、ring(buf,0,len); /5.关闭 ss.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 二.TCP互相传输我们在来写一个实例去说明,他们的互访动作,这里为了写起来方便,就写在一个类中了package com.lgl.hellojava;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import .ServerSocket;import .Soc

7、ket;import .UnknownHostException;/* * 客户端发送信息,服务端收到,反馈信息 * * author LGL * */public class Tcp public static void main(String args) try Socket s = new Socket("192.168.1.102", 10005); OutputStream out = s.getOutputStream(); out.write("我是客户端".getBytes(); InputStream in = s.getInputSt

8、ream(); byte buf = new byte1024; int len = in.read(buf); System.out.println(new String(buf, 0, len); s.close(); catch (UnknownHostException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); /* * 服务端 * author LGL * *

9、/class Server public static void main(String args) try ServerSocket ss = new ServerSocket(10005); Socket s = ss.accept(); InputStream in = s.getInputStream(); byte buf = new byte1024; int len = in.read(buf); System.out.println(new String(buf, 0, len); OutputStream out = s.getOutputStream(); out.writ

10、e("收到后反馈".getBytes(); s.close(); ss.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 三.复制文件同样的这里也是使用的流,我们具体来看下怎么去操作,我们同样的,写在一个类中package com.lgl.socket;import java.io.BufferedReader;import java.io.FileReader;import java.io.FileWriter;import java.io.IOExc

11、eption;import java.io.InputStreamReader;import java.io.PrintWriter;import .ServerSocket;import .Socket;import .UnknownHostException;public class FileClient public static void main(String args) try Socket s = new Socket("192.168.1.102", 10006); BufferedReader bufr = new BufferedReader(new F

12、ileReader("test.txt"); PrintWriter pw = new PrintWriter(s.getOutputStream(), true); String line = null; while (line = bufr.readLine() != null) pw.println(line); pw.print("over"); BufferedReader bufIn = new BufferedReader(new InputStreamReader( s.getInputStream(); String str = buf

13、In.readLine(); System.out.println(str); bufr.close(); s.close(); catch (UnknownHostException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); class FileServer public static void main(String args) try ServerSocket s

14、s = new ServerSocket(10006); Socket s = ss.accept(); BufferedReader bufIn = new BufferedReader(new InputStreamReader( s.getInputStream(); PrintWriter out = new PrintWriter(new FileWriter("test1.txt"), true); String line = null; while (line = bufIn.readLine() != null) if ("over".e

15、quals(line) break; out.println(line); PrintWriter pw = new PrintWriter(s.getOutputStream(), true); pw.println("上传成功"); out.close(); s.close(); ss.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 四.上传图片我们再来看下图片是怎么上传的,我们先来分析下步骤客户端1.服务端点2.读取客户端已有的图片数据3.通过s

16、ocket,发送给服务端4.读取服务端反馈的信息5.关闭资源* * 客户端 * * author LGL * */public class PicClient public static void main(String args) try Socket s = new Socket("192.168.1.102", 10009); FileInputStream fis = new FileInputStream("1.png"); OutputStream out = s.getOutputStream(); byte buf = new byte1

17、024; int len = 0; while (len = fis.read(buf) != -1) out.write(buf, 0, len); /告訴服务端数据写完 s.shutdownInput(); InputStream in = s.getInputStream(); byte bufn = new byte1024; int num = in.read(bufn); System.out.println(new String(bufn, 0, num); fis.close(); s.close(); catch (UnknownHostException e) / TODO

18、 Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 服务端直接看代码/* * 服務端 * author LGL * */class PicServer public static void main(String args) try ServerSocket ss = new ServerSocket(10009); Socket s = cept(); InputStream in = s.ge

19、tInputStream(); FileOutputStream fos = new FileOutputStream("2.png"); byte buf = new byte1024; int len = 0; while (len = in.read(buf) != -1) fos.write(buf, 0, len); OutputStream out = s.getOutputStream(); out.write("上传成功".getBytes(); fos.close(); s.close(); ss.close(); catch (IOE

20、xception e) / TODO Auto-generated catch block e.printStackTrace(); 其实跟I/O区别真不大,但是概念一定要了解清楚五.多并发上传多并发这个概念就是多人互动了,这对服务器的负荷还是有考究的,这里呢,我们就模拟一下,多人上传图片的场景,我们是怎么做的?我们还是在上传图片的那份代码上更改首先我们可以确定的是,这是服务端的代码这个服务端有个局限性,当A客户端连接之后,被服务端获取到,服务端就在执行代码了,这个时候如果B客户端连接只有等待,这就是我们需要多并发的原因了,为了让多个客户端同时连接,服务端最好就是讲每个客户端封装到一个单独的线

21、程中,这样就可以同时处理多个客户端请求如何定义线程?只要明确了每个客户端要在服务端执行的代码即可/* * 服務端 * * author LGL * */class PicServer public static void main(String args) try ServerSocket ss = new ServerSocket(10009); while (true) Socket s = ss.accept(); new Thread(new PicThread(s).start(); catch (IOException e) / TODO Auto-generated catch

22、block e.printStackTrace(); /* * 并发线程 * author LGL * */class PicThread implements Runnable private Socket s; public PicThread(Socket s) this.s = s; Override public void run() try String ip = s.getInetAddress().getHostAddress(); System.out.println("ip:" + ip); long millis = System.currentTim

23、eMillis(); File file = new File(millis + ".png"); InputStream in = s.getInputStream(); FileOutputStream fos = new FileOutputStream(file); byte buf = new byte1024; int len = 0; while (len = in.read(buf) != -1) fos.write(buf, 0, len); OutputStream out = s.getOutputStream(); out.write("上

24、传成功".getBytes(); fos.close(); s.close(); catch (Exception e) throw new RuntimeException("上传失败"); 其实我写的代码还是有点烂的,但是思想在就好,我们得先把思想学会了六.多并发登录上面说的多并发的上传,实在服务端端,现在我们来说下登录,是作用在客户端package com.lgl.socket;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import j

25、ava.io.InputStreamReader;import java.io.PrintWriter;import .ServerSocket;import .Socket;import .UnknownHostException;public class LoginClient public static void main(String args) try Socket s = new Socket("192.168.1.102", 10008); BufferedReader bufr = new BufferedReader(new InputStreamRead

26、er( System.in); PrintWriter out = new PrintWriter(s.getOutputStream(), true); BufferedReader bufIn = new BufferedReader(new InputStreamReader( s.getInputStream(); for (int i = 0; i < 3; i+) String line = bufr.readLine(); if (line = null) break; out.println(line); String info = bufIn.readLine(); S

27、ystem.out.println("info:" + info); if (info.contains("欢迎") break; bufr.close(); s.close(); catch (UnknownHostException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); /* * 服务端 * * author LGL * */class LoginServer public static void main(String args) try ServerSocket ss = new ServerSocket(10008); while (true) Socket s = ss.accept(); new Thread(new UserThread(s).start(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); /*

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论