高级网络编程总结
2012-05-06 21:31
267 查看
黑马程序员
----------------------android培训、java培训 、期待与您交流! ----------------------1、 找到对方IP。
2、 数据库要发送到对方指定的应用程序上。为了标示这些应用程序,所以给这些网络应用程序都用数字进行标示。为了方便称呼这个数字,叫做端口。逻辑端口。
3、 定义通信规则。这个通讯规则称为协议。国际组织定义了通用协议TCP/IP.
UDP/TCP
UDP
将数据及源和目的封装成数据包,不需要建立连接。
每个数据报的大小在限制在64K内。
因无连接,是不可靠协议。
不需要建立连接,速度快。
TCP
建立连接,形成传输数据的通道。
在连接中进行大数据量传输。
通过三次握手完成连接,是可靠协议
必须建立连接,效率会稍低。
1、UDP面向无连接。
2、数据会被封包、包体积有限制在64k以内。
3.不可靠因为面向无连接所以不可靠。
4、UDP速度快容易丢包(聊天、视频会议、桌面共享等,步话机)
TCP面向连接:
对方必须在,我先去确定对方在,在进行数据的发送。(打电话、下载)
UDP聊天小程序: /* 编程一个聊天软件 有收数据的部分,和发数据的部分。 这两部分需要同时执行。 那就需要用到多线程技术。 一个线程控制收,一个线程控制发。 因为收和发是不一致的,所以要定义两个run方法,而且这两个方法要封装到不同的类中。 */ class Send implements Runnable{ DatagramSocket socket;//创建UDP对象DatagramSocket public Send(DatagramSocket socket){ this.socket=socket; } public void run() { BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));//创建键盘录入对象 String line=null; try { while((line=bufr.readLine())!=null){ if("886".equals(line)){ break; } byte[] buf=line.getBytes();//确定数据,将数据封装成包。 DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.0.102"),10003); //通过Socket服务通过send方法将数据发送出去。 socket.send(dp); } } catch (IOException e) { e.printStackTrace(); }finally{ try { if(bufr!=null) bufr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } socket.close();//关闭资源 } } } class Reces implements Runnable{ DatagramSocket socket;//创建UDPSocket建立服务端点。 public Reces(DatagramSocket socket){ this.socket=socket; } public void run() { try { while(true){ byte[] buf=new byte[1024];//定义数据包用于存储。 DatagramPacket dp=new DatagramPacket(buf, buf.length);//通过receive方法将接收到得数据存入数据包中 socket.receive(dp); //通过数据包中的方法获取数据 String ip=dp.getAddress().getHostAddress(); String data=new String(dp.getData(),0,dp.getLength()); System.out.println(ip+"::"+data); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ socket.close(); } } } public class ChatDemo { public static void main(String[] args) { try { DatagramSocket sends=new DatagramSocket(); DatagramSocket reces=new DatagramSocket(10003); new Thread(new Send(sends)).start(); new Thread(new Reces(reces)).start(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
TCP传输
Socket和serverSocket,建立客户端和服务端,建立连接后,通过socket中的IO流进行数据的传输,在关闭socket,同样客户端和服务端是两个独立的应用程序。
演示tcp传输 1,TCP分客户端和服务端 2.客户端对应的对象是socket,服务端对应的对象是serverSocket. 客户端: 通过查阅socket对象,发现在该对象建立时,就可以去连接指定的主机。 因为TCP是面向连接的,所以在建立socket服务时,就要有客户端存在,并连接成功 形成通路后,在该通道进行数据的传输。 需求:给服务端发送一个文本数据。 步骤: 创建socket服务。并指定连接的主机和端口。 */ class TcpClient{ public static void main(String[] args) { Socket socket=null; try { //创建客户端指定ip和端口 socket=new Socket("192.168.0.102", 10005); //为了发送数据应该获取socket的输出流。 OutputStream out=socket.getOutputStream(); out.write("clinet lai le".getBytes()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } /* 需求:定义端点接收数据并打印在控制台上。 服务端: 1,建立服务端的socket服务。serverSocket 并监听一个端口。 2,获取连接过来的客户对象。 通过serverSocket的accept方法。没有连接就会等,所以这个方法是阻塞式的 3.客户端如果发过来数据,那么服务端要使用对应的客户端对象,并获取到该客户端对象的读取流来读取发过来的数据。 并打印在控制台。 4.关闭服务端。(服务端) */ public class TcpServer { public static void main(String[] args) { //建立服务端socket服务。并监听一个端口。 try { ServerSocket ss=new ServerSocket(10005); //通过accept方法获取连接过来的客户端对象。 Socket socket=ss.accept(); String ip=socket.getInetAddress().getHostAddress(); //获取客户端发送过来的数据,那么要使用客户端对象的读取刘流读取数据。 InputStream in=socket.getInputStream(); byte[] buf=new byte[1024]; int len=in.read(buf); String data=new String(buf,0,len); System.out.println(ip+"::"+data); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
/* 演示TCP传输的客户和服务端的访问。 需求: 客户端发送数据,服务端受到后,给客户端反馈信息。 客户端: 1,建立socket服务,指定要连接的主机和端口。 2,获取socket流中的输出流。将数据写到该流中,通过网络发给服务端。 3,获取socket服务中的输入流。将服务端反馈的数据获取到并打印。 4,关闭客户端资源。 */ class TcpClient2{ public static void main(String[] args) { Socket s=null; try { s=new Socket("192.168.0.102",10006); OutputStream out=s.getOutputStream(); out.write("服务端,你好".getBytes()); InputStream in=s.getInputStream(); byte[] buf=new byte[1024]; int len=in.read(buf); System.out.println(new String(buf, 0, len)); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { s.close(); } catch (IOException e) { e.printStackTrace(); } } } } public class TcpServer2 { public static void main(String[] args) { ServerSocket ss=null; Socket s=null; try { ss=new ServerSocket(10006); s=ss.accept(); String ip=s.getInetAddress().getHostAddress(); InputStream in=s.getInputStream(); byte[] buf=new byte[1024]; int len=in.read(buf); String data=new String(buf,0,len); System.out.println(ip+"::"+data); OutputStream out=s.getOutputStream(); out.write("客户端哥们你好,收到!".getBytes()); } catch (IOException e) { e.printStackTrace(); }finally{ try { s.close(); ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
/* 需求: 建立一个文本装换服务器。 客户端给服务端发送文本,服务端会将文本转成大写在返回给客户端。 而且客户端可以不断的进行文本转换。当客户端输入over时,转换结束。 分析 客户端: 既然是操作设备上的数据,那么可以使用IO技术,并按照IO的技术规律来思考。 源:键盘录入 目地:网络输出流。 而且操作的是文本数据.可以选择字符流 步骤: 1,建立服务 2.获取键盘录入 3,将数据发给服务端。 4.获取服务端返回的大写数据。 5.结束关闭资源 都是文本数据,可以使用字符流进行操作,同时提高效率,加入缓冲。 该列子出现的问题: 现象:客户端和服务端都在莫名的等待 为什么呢? 因为客户端和服务端都有阻塞式的方法,这些数据都没有读到结束标记,那么就一直等待。 而导致两端,都在等待。 */ public class TransClinet { public static void main(String[] args) { Socket s=null; BufferedReader bufr=null; try { s=new Socket("192.168.0.102",10007); //定义键盘读取流的对象 bufr=new BufferedReader(new InputStreamReader(System.in)); //将数据写入socket流中,发送到服务端 //BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); PrintWriter out=new PrintWriter(s.getOutputStream(),true); //定义一个socket读取流返回服务端的大写信息。 BufferedReader bufr2=new BufferedReader(new InputStreamReader(s.getInputStream())); String line=null; while((line=bufr.readLine())!=null){ if("ovet".equals(line)){ break; } out.println(line); /* bufOut.write(line); bufOut.newLine(); bufOut.flush();*/ String str=bufr2.readLine(); System.out.println("Server:"+str); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { s.close(); bufr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class TransServer{ public static void main(String[] args) { ServerSocket ss=null; Socket s=null; try { ss=new ServerSocket(10007); s=ss.accept(); String ip=s.getInetAddress().getHostAddress(); System.out.println(ip+"......来了"); //读取socket读取流中的数据 BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream())); //目的,socKet输出流,将大写数据写入到Socket输出流中,并发给客户端 //BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); PrintWriter out=new PrintWriter(s.getOutputStream(),true); String line=null; while((line=bufr.readLine())!=null){ out.println(line.toUpperCase()); /* bufw.write(line.toUpperCase()); bufw.newLine(); bufw.flush();*/ } } catch (IOException e) { e.printStackTrace(); }finally{ try { ss.close(); s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
TCP文件上传 /* 建立一个上传文件的tcp */ public class TextClient { public static void main(String[] args) { Socket s=null; BufferedReader bufr=null; try { //顶一个socket s=new Socket("192.168.0.102",10008); //定义一个文本读取流 bufr=new BufferedReader(new FileReader("FileWriters.java")); //定义一个打印流,从socket获取输出流 PrintWriter out=new PrintWriter(s.getOutputStream(),true); String line=null; while((line=bufr.readLine())!=null){ out.println(line); } s.shutdownOutput();//关闭客户端的输出流。相当与给流中加入了一个-1的标记。 //顶一个输入流缓冲区,获取从服务端发响应过来的数据 BufferedReader bufrIn=new BufferedReader(new InputStreamReader(s.getInputStream())); String str=bufrIn.readLine(); System.out.println(str); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { s.close(); bufr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class TextServer{ public static void main(String[] args) { //顶一个服务端socket ServerSocket ss=null; Socket s=null; PrintWriter out=null; try { ss=new ServerSocket(10008); s=ss.accept();//连接客户端通道方法 String ip=s.getInetAddress().getHostAddress();//获取IP地址 System.out.println(ip+".........."); BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream())); out=new PrintWriter (new FileWriter("server.txt"),true); String line=null; //循环读取客户端的数据,将数据写入到文件中。 while((line=bufr.readLine())!=null){ out.println(line); } //定义打印流,将数据发到客户端去 PrintWriter pw=new PrintWriter(s.getOutputStream(),true); pw.println("上传成功"); } catch (IOException e) { e.printStackTrace(); }finally{ try { out.close(); s.close(); ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
TCP上传图片: /* TCP上传图片 客户端: 1,服务端点。 2,读取客户端已有的图片数据。 3,通过socket输出流将数据发给服务端. 4,读取服务端的反馈信息. 5,关闭. */ public class PicClient { public static void main(String[] args) { Socket s=null; FileInputStream fis=null; try { s=new Socket("192.168.0.102", 10008); fis=new FileInputStream("D:\\sex.jpg"); OutputStream out=s.getOutputStream(); byte[] buf=new byte[1024]; int len=0; while((len=fis.read(buf))!=-1){ out.write(buf, 0, len); } //告诉服务端数据已写完 s.shutdownOutput(); InputStream in=s.getInputStream(); byte[] bufin=new byte[1024]; int num=in.read(bufin); System.out.println(new String(bufin,0,num)); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { s.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class PicServer{ public static void main(String[] args) { ServerSocket ss=null; Socket s=null; FileOutputStream fos=null; try { ss=new ServerSocket(10008); s=ss.accept(); InputStream in=s.getInputStream(); fos=new FileOutputStream("D:\\sex1111.jpg"); byte[] buf=new byte[1024]; int len=0; while((len=in.read(buf))!=-1){ fos.write(buf, 0, len); } OutputStream out=s.getOutputStream(); out.write("上传成功".getBytes()); } catch (IOException e) { e.printStackTrace(); }finally{ try { s.close(); ss.close(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
TCP并发上传图片: /* 并发上传图片 /* 这个服务端有个局限性。当A客户端连接上后。被服务端获取到。服务端执行具体流程。 这时B客户端连接,只有等待。 因为服务端还没有处理完A客户端的请求,还没有循环回来执行下次accept方法。所以暂时 获取不到B客户端对象。 那么为了可以让多个客户端同时并发访问服务端。 那么服务端最好就是将每个客户端封装到一个单独的线程中,这样就可以同时处理多个客户端请求。 如何定义线程呢? 只要明确了每一个客户端要在服务端执行的代码即可。将该代码存入到Run方法中 */ public class UploadPicByThread { public static void main(String[] args) { /*if(args.length!=1){ System.out.println("请选择jpg格式的图片!"); return; } File file=new File(args[0]); if(!(file.exists()&&file.isFile())){ System.out.println("该文件有问题,要么不存在,要么不是文件"); return; } if(file.getName().endsWith(".jpg")){ System.out.println("图片格式错误,请重新选择"); return; } if(file.length()>1024*1024*8){ System.out.println("文件过大没安好心"); return; }*/ Socket s=null; FileInputStream fis=null; try { s=new Socket("192.168.0.102", 10009); fis=new FileInputStream("D:\\6[1].jpg"); OutputStream out=s.getOutputStream(); byte[] buf=new byte[1024]; int len=0; while((len=fis.read(buf))!=-1){ out.write(buf, 0, len); } //告诉服务端数据已写完 s.shutdownOutput(); InputStream in=s.getInputStream(); byte[] bufin=new byte[1024]; int num=in.read(bufin); System.out.println(new String(bufin,0,num)); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { s.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class PicThread implements Runnable{ Socket s; public PicThread(Socket s){ this.s=s; } public void run() { int count=1; InputStream in=null; FileOutputStream fos=null; String ip=s.getInetAddress().getHostAddress(); try { System.out.println(ip+".........connected"); in = s.getInputStream(); File file=new File(ip+"("+(count)+")"+".jpg"); while(file.exists()) file=new File(ip+"("+(count++)+")"+".jpg"); fos=new FileOutputStream("D:\\"+(file)); System.out.println("D:\\"+(file)); byte[] buf=new byte[1024]; int len=0; while((len=in.read(buf))!=-1){ fos.write(buf, 0, len); } OutputStream out=s.getOutputStream(); out.write("上传成功".getBytes()); } catch (IOException e) { e.printStackTrace(); }finally{ try { s.close(); fos.close(); } catch (IOException e) { throw new RuntimeException(ip+"上传失败"); } } } } class PicServers{ 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 block e.printStackTrace(); } } }
TCP并发登陆: /* TCP并发登陆 客户端通过键盘录入用户名 服务端对这个用户名进行校验 如果该用户不存在,在服务器显示xxx,已登陆 并在客户端显示xxx,欢迎光临。 如果该用户存在,在服务端显示xxx,并尝试登陆。 并在客户端显示xxx,该用户不存在。 最多就登陆3次 */ public class LoginClinet { public static void main(String[] args) { Socket s=null; BufferedReader bufr=null; try { s=new Socket("192.168.0.102", 10010); bufr=new BufferedReader(new InputStreamReader(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(); if(info.equals("欢迎")) break; System.out.println("Info::"+info); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { s.close(); bufr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class UserThread implements Runnable{ Socket s; public UserThread(Socket s){ this.s=s; } public void run() { try { String ip=s.getInetAddress().getHostAddress(); System.out.println(ip+".........conntecd"); for(int i=0;i<3;i++){ BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream())); String name=bufIn.readLine(); if(name==null) break; BufferedReader bfur=new BufferedReader(new FileReader("Login.txt")); PrintWriter out=new PrintWriter(s.getOutputStream(),true); String line=null; boolean flag=false; while((line=bfur.readLine())!=null){ if(line.equals(name)){ flag=true; break; } } if(flag){ System.out.println(name+":已登陆"); out.println(name+"欢迎光临"); }else{ System.out.println(name+":尝试登陆"); out.println(name+"用户名不存在"); } } } catch (IOException e) { e.printStackTrace(); }finally{ try { s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class ServerLogin{ public static void main(String[] args) { try { ServerSocket ss=new ServerSocket(10010); while(true){ Socket s=ss.accept(); new Thread(new UserThread(s)).start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
----------------------android培训、java培训、期待与您交流! ------------------
详细请查看:http://edu.csdn.net/heima
相关文章推荐
- JAVA高级05--网络编程--要点总结
- Java高级部分网络编程重点总结
- Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
- 总结简单Java网络编程
- JAVA高级视频 网络编程 07 访问Intentnet资源
- java基础学习总结——网络编程
- 【tcp-ip学习总结】socket编程基础/网络编程基础
- [综合面试] 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- 网络编程常见问题总结
- 网络编程中的关键问题总结
- linux TCP/UDP网络编程总结
- 手机网络编程(基于J2ME的总结)
- java网络编程_思维导图总结1
- 网络编程概念总结(笔记)-------来自传智播客视频
- 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- Java网络编程总结 --URLConnection
- (六)Linux网络编程--6. 高级套接字函数
- UNP总结 Chapter 22~25 高级UDP套接字编程、高级SCTP 套接字编程、带外数据、信号驱动I/O
- 【Java网络编程】HttpClient、URLConnection的再次总结