黑马程序员——网络编程
来源:互联网 发布:百度文库软件版本 编辑:程序博客网 时间:2024/06/06 15:53
---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
(教23-01)
网络编程
网络参考模型:
OSI参考模型:应用层,表示层,会话层,传输层,网络层,数据链路层,物理层。
TCP/IP参考模型:应用层,传输层,网际层,主机至网络层。
IP地址:InetAddress
网络中设备的标识。本地回环地址:127.0.0.1主机名:localhost
import java.net.*;class IPDemo{ publicstatic void main(String[] args) throws Exception { InetAddressi=InetAddress.getLocalHost(); System.out.println(i.toString()); System.out.println("address:"+i.getHostAddress()); System.out.println("name:"+i.getHostName()); }}
端口号:
用于标识进程的逻辑地址,不同进程的标识。
有效端口:0~65535,其中0~1024系统使用或保留端口。
传输协议:
通讯的规则,常见协议:TCP,UDP
必须掌握:
UDP:将数据及源和目的封装成数据包中,不需要建立连接。每个数据报的大小限制在64K内。因无连接,是不可靠的协议,不需要建立连接,速度快。
TCP:建立连接,形成传输数据的通道。在连接中进行大数据量传输,通过三次握手完成连接,是可靠协议。必须建立连接,效率会稍低。
Socket
Socket就是为网络服务提供的一种机制。通信的两端都有Socket。网络通信其实就是Socket间的通信。数据在两个Socket间通过IO传输。
Udp发送端和接收端:
需求:通过udp传输方式,讲一段文字数据发送出去。
定义发送端
思路:
1、建立udpsocket服务。
2、提供数据,并将数据封装到数据包中。
3、通过socket服务的发送功能,将数据包发送出去。
4、关闭资源。
import java.net.*;class UdpSend{ publicstatic void main(String[] args) throws Exception { //1、创建udp服务,通过DatagramSocket对象 DatagramSocketds=new DatagramSocket(); //2、确定数据,并封装成数据包。 byte[]buf ="udp wo laile".getBytes(); DatagramPacketdp=newDatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10000); //3、通过socket服务,将已有数据包发送出去,通过send方法。 ds.send(dp); //4、关闭资源 ds.close(); }}
需求:定义一个应用程序,用于接收udp协议传输的数据并处理。
定义Udp接收端。
思路:
1、定义udpsocket服务。通常会监听一个端口,其实就是给这个接收网络应用程序定义数字标识。
方便明确哪些数据过来,该应用程序可以处理。
2、定义一个数据包,因为要存储接收到的字节数据。
因为数据包对象中有更多功能可以提取字节数据中的不同数据信息。
3、通过socket服务的receive方法将收到的数据存入已定义好的数据包中。
4、通过数据包对象的特有功能,将这些不同的数据取出,打印在控制台上。
5、关闭资源。
class UdpRece{ publicstatic void main(String[] args) throws Exception { //1、创建udpSocket服务,建立端点。 DatagramSocketds =new DatagramSocket(10000); while(true) {//2、定义数据包,用于存储数据 byte[]buf=new byte[1024]; DatagramPacketdp=new DatagramPacket(buf,buf.length); //3、通过服务的receive方法将收到数据存入数据包中。 ds.receive(dp);//阻塞式方法 //4、通过数据包的方法获取其中的数据。 Stringip =dp.getAddress().getHostAddress(); Stringdata=new String(dp.getData(),0,dp.getLength()); intport=dp.getPort(); System.out.println(ip+"::"+data+"::"+port); //5、关闭资源 ds.close(); } }}
Udp键盘录入:
import java.net.*;import java.io.*;class UdpSend2{ publicstatic void main(String[] args) throws Exception { DatagramSocketds=new DatagramSocket(); BufferedReaderbufr=new BufferedReader(new InputStreamReader(System.in)); Stringline=null; while((line=bufr.readLine())!=null) { if("886".equals(line)) break; byte[]buf=line.getBytes(); DatagramPacketdp= newDatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10001); ds.send(dp); } ds.close(); }}class UdpRece2{ publicstatic void main(String[] args) throws Exception { DatagramSocketds =new DatagramSocket(10001); while(true) { byte[]buf=new byte[1024]; DatagramPacket dp=newDatagramPacket(buf,buf.length); ds.receive(dp); String ip=dp.getAddress().getHostAddress(); String data=newString(dp.getData(),0,dp.getLength()); int port=dp.getPort(); System.out.println(ip+"::"+data+"::"+port); //ds.close(); } }}
聊天程序:
编写一个聊天程序。
有收数据的部分,和发数据的部分。
这两部分需要同时执行。
那就需要用到多线程技术。
一个线程控制接收,一个控制发送。
因为收和发动作是不一致的,所以要定义两个run方法。
而且这两个方法要封装到不同的类中。
import java.io.*;import java.net.*;class Send implements Runnable{ privateDatagramSocket ds; publicSend(DatagramSocket ds) { this.ds=ds; } publicvoid run() { try { BufferedReaderbufr=new BufferedReader(new InputStreamReader(System.in)); Stringline=null; while((line=bufr.readLine())!=null) { if("886".equals(line)) break; byte[]buf=line.getBytes(); DatagramPacketdp= newDatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10002); ds.send(dp); } } catch(Exception e) { thrownew RuntimeException("发送端失败"); } }}class Rece implements Runnable{ privateDatagramSocket ds; publicRece(DatagramSocket ds) { this.ds=ds; } publicvoid run() { try { while(true) { byte[]buf=new byte[1024]; DatagramPacketdp=new DatagramPacket(buf,buf.length); ds.receive(dp); Stringip= dp.getAddress().getHostAddress(); Stringdata=new String(dp.getData(),0,dp.getLength()); System.out.println(ip+":"+data); } } catch(Exception e) { thrownew RuntimeException("接收端失败"); } }}class ChatDemo{ publicstatic void main(String[] args) throwsException { DatagramSocketsendSocket=new DatagramSocket(); DatagramSocket receSocket=new DatagramSocket(10002); newThread(new Send(sendSocket)).start(); newThread(new Rece(receSocket)).start(); }}
演示Tcp传输。
1、Tcp分客户端和服务端。
2、客户端对应的对象是Socket
服务端对应的对象是ServerSocket。
客户端:
通过查阅Socket对象,发现在该对象建立时,就可以去连接指定主机。
因为tcp是面向链接的,所以在建立socket服务时,
就要有服务端存在,并连接成功,形成通路后,在该通道进行数据的传输。
步骤:
1、创建Socket服务,并指定要连接的主机和端口。
2、为了发送数据,应该获取Socket流中的输出流。
import java.io.*;import java.net.*;class TcpClient{ publicstatic void main(String[] args) throws Exception { //创建客户端Socket服务,指定目的主机和端口。 Sockets=new Socket("127.0.0.1",10003); //为了发送数据,应该获取Socket流中的输出流。 OutputStreamout=s.getOutputStream(); out.write("tcpwo laile".getBytes()); s.close(); }}
需求:定义端点接收数据并打印在控制台上。
服务端:
1、建立服务端的Socket服务,ServerSocket();
并监听一个端口。
2、获取连接过来的客户端对象。
通过ServerSocket的accept方法,没有连接就会等,所以这个方法阻塞式的。
3、客户端如果发过来数据,那么服务端要使用对应的客户端对象,
并获取到该客户端对象的读取流来读取发过来的数据。
并打印在控制台。
4、关闭服务端(可选操作)。
class TcpServer{ publicstatic void main(String[] args) throws Exception { //建立服务端Socket服务,并监听一个端口。 ServerSocketss=new ServerSocket(10003); //通过accept方法获取连接过来的客户端对象。 Sockets =ss.accept(); Stringip=s.getInetAddress().getHostAddress(); System.out.println(ip+"......connected"); //获取客户端发送过来的数据,那么要使用客户端对象的读取流来读取数据。 InputStreamin=s.getInputStream(); byte[]buf=new byte[1024]; intlen=in.read(buf); System.out.println(newString(buf,0,len)); s.close();//关闭客户端 ss.close(); }}
上传文件:
import java.io.*;import java.net.*;class TextClient{ publicstatic void main(String[] args) throws Exception { Sockets=new Socket("127.0.0.1",10006); BufferedReaderbufr= newBufferedReader(new FileReader("IPDemo.java")); PrintWriterout=new PrintWriter(s.getOutputStream(),true); Stringline=null; while((line=bufr.readLine())!=null) { out.println(line); } s.shutdownOutput();//关闭客户端输出流,相当于给流中加一个结束标记-1 BufferedReaderbufIn=new BufferedReader(new InputStreamReader(s.getInputStream())); String str=bufIn.readLine(); System.out.println(str); bufr.close(); s.close(); }}class TextServer{ publicstatic void main(String[] args) throws Exception { ServerSocketss=new ServerSocket(10006); Sockets=ss.accept(); Stringip=s.getInetAddress().getHostAddress(); System.out.println(ip+"......connected"); BufferedReaderbufIn= newBufferedReader(new InputStreamReader(s.getInputStream())); PrintWriterout=new PrintWriter(new FileWriter("server.txt"),true);//自动刷新,能接受字节流和字符流 Stringline=null; while((line=bufIn.readLine())!=null) { out.println(line); } PrintWriterpw=new PrintWriter(s.getOutputStream(),true); pw.println("上传成功"); out.close(); s.close(); ss.close(); }}
并发上传图片:
import java.io.*;import java.net.*;class PicClient{ publicstatic void main(String[] args) throws Exception { if(args.length!=1) { System.out.println("请选择一个jpg格式的图片"); return; } Filefile=new File(arg[0]); if(!(file.exists()&&file.isFile())) { System.out.println("该文件有问题,不存在或者不是文件"); return; } if(!file.getName().endWith(".jpg")) { System.out.println("图片格式错误,请重选"); return; } if(!file.length()>1024*1024*5) { System.out.println("文件过大"); return; } Sockets=new Socket("127.0.0.1",10007); FileINputStreamfis=new FileInputStream(file); OutputStreamout=s.getOutputStream(); byte[]buf=new byte[1024]; intlen=0; while((len=fis.read(buf))!=null) { out.write(buf,0,len); } s.shutdownOutput(); InputStreamin=s.getInputStream(); byte[]bufIn=new byte[1024]; int num=in.read(bufIn); System.out.println(newString(bufIn,0,num)); fis.close(); s.close(); }}class PicThread implements Runnable{ privateSocket s; PicThread(Sockets) { this.s=s; } publicvoid run() { intcount=1; Stringip=s.getInetAddress().getHostAddress(); try { System.out.println(ip+"......connected"); InputStreamin=s.getInputStream(); Filefile=new File(ip+"("+(count)+")"+".jpg"); while(file.exists()) file=newFile(ip+"("+(count++)+")"+".jpg"); FileOutputStreamfos=new FileOutputStream(file); byte[]buf=new byte[1024]; intlen=0; while((len=in.read(buf))!=-1) { fos.write(buf,0,len); } OutputStreamout=s.getOutputStream(); out.write("上传成功".getBytes()); fos.close(); s.close(); } catch(Exception e) { thrownew RuntimeException(ip+"上传失败"); } }}class PicServer{ publicstatic void main(String[] args) throws Exception { ServerSocketss=new ServerSocket(10007); while(true) { Sockets=ss.accept(); newThread(new PicThread(s)).start(); } }}
客户端通过键盘录入用户名。
服务端对这个用户名进行校验。
如果该用户存在,在服务端显示xxx,已登录。
并在客户端显示xxx,欢迎光临。
如果该用户不存在,在服务端显示xxx,尝试登陆。
并在客户端显示xxx,该用户不存在。
最多就登陆三次。
import java.io.*;import java.net.*;class LoginClient{ publicstatic void main(String[] args) throws Exception { Socket s=newSocket("127.0.0.1",10008); BufferedReaderbufr= newBufferedReader(new InputStreamReader(System.in)); PrintWriterout=new PrintWriter(s.getOutputStream(),true); BufferedReader bufIn= newBufferedReader(new InputStreamReader(s.getInputStream())); for(int x=0; x<3;x++ ) { Stringline =bufr.readLine(); if(line==null) break; out.println(line); Stringinfo=bufIn.readLine(); System.out.println("info:"+info); if(info.contains("欢迎")) break; } bufr.close(); s.close(); }}class UserThred implements Runnable{ privateSocket s; UserThread(Sockets) { this.s=s; } publicvoid run() { String ip=s.getInetAddress().getHostAddress(); System.out.println(ip+"......connected"); try { BufferedReaderbufIn=new BufferedReader(new InputStreamReader(s.getInputStream())); Stringname=bufIn.readLine(); if(name==null) { break; } BufferedReaderbufr=new BufferedReader(new FileReader("user.txt")); PrintWriterout=new PrintWriter(s.getOutputStream(),true); Stringline=null; booleanflag=false; while((line=bufr.readLine())!=null) { if(line.equals(name)) { flag=true; break; } } if(flag) { System.out.println(name+",已登录"); out.println(name+",欢迎光临"); break; } elee { System.out.println(name+",尝试登录"); out.println(name+",用户名不存在"); } s.close(); } catch(Exception e) { thrownew RuntimeException(ip+"校验失败"); } }}class LoginServer{ publicstatic void main(String[] args) { Sockets=ss.accept(); newThread(new UserThread(s)).start(); }}
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
- 黑马程序员— 网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员 — 网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- spring3.2 hibernate3.6 整合中 超类的使用HibernateDaoSupport
- 403 You don't have permission to access /phone/query_name.php
- OGRE EXCEPTION
- python解析xml——generateDS.py
- 300多条MTK工作笔记为你的工作节省一半时间
- 黑马程序员——网络编程
- 日积月累:EditText软键盘的显示和隐藏
- 过程:先Edit,然后SetFieldValue,接着Update,最后MoveNext
- 关于Spring的注入方式
- Java线程
- mysql权限问题
- RMAN还原恢复知识点小结
- 负载均衡的基本算法
- JSP九大内置对象