网络编程在ISO模型中的传输层进行。
网络通讯的三要素是:
①IP地址(对应的对象InetAddress);
②端口号:0-65535,其中0-1024是系统使用或者保留端口;
③传输协议:UDP,TCP;
UDP特点:
①将数据源和目的封装成数据包,不需要建立连接;
②每个数据报的大小限制在64k;
③因为无连接,所以是不可靠协议;
④不需要建立连接 ,所有速度快;
应用:聊天,共享,视频会议
TCP特点:
①建立连接,形成传输数据的通道;
②在连接中进行了大数据量传输;
③通过三次握手完成连接,是可靠协议;
④必须建立连接,效率稍低。
应用:如迅雷下载等
UDP传输:使用DatagramSocket对象接受和发送数据,数据需要封装到DatagramPacket对象中。
示例:
/*
编写一个聊天程序,
有接收数据的部分,和发数据的部分。
这两部分需要同时执行,
那就是需要用到多线程技术,
一个线程控制收,一个线程控制发
因为收和发,动作是不一致的,所有要定义两个run方法。
而且这两个方法要封装到不同的类中。
*/
import java.io.*;
import java.net.*;
//定义一个发送端线程
class Send implements Runnable
{
private DatagramSocket ds;
public Send(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// 读取键盘录入
String line = null;
while((line = br.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10002);// 将数据封装到DatagramPacket对象中。
ds.send(dp);// 通过DatagramSocket对象将数据发送到接收端
}
ds.close();
}
catch (Exception e)
{
throw new RuntimeException("读取数据失败");
}
}
}
//定义一个接收端线程
class Rece implements Runnable
{
private DatagramSocket ds;
public Rece(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);//定义一个DatagramPacket接收发送端发过来的数据
ds.receive(dp);//接受发送端发过来的数据
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"::"+data);
}
}
catch (Exception e)
{
throw new RuntimeException("接受据失败");
}
}
}
class ChatDemo
{
public static void main(String[] args) throws Exception
{
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receSocket = new DatagramSocket(10002);
new Thread(new Send(sendSocket)).start();
new Thread(new Rece(receSocket)).start();
}
}
TCP传输:分客户端和服务端,分别使用Socket和ServerSocket对象。
示例:
/*
需求:客户端并发上传图片
客户端:
思路:
1,建立服务端点。
2,读取客户端已有的图片数据
3,通过Socket输出流将数据发送给服务端
4,读取服务端反馈回来的数据
5,关闭资源
*/
import java.io.*;
import java.net.*;
class PicClient
{
public static void main(String[] args) throws Exception
{
//让客户端通过指定文件路径的方式上传图片
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("图片格式错误,请选择一个.jpg文件");
return;
}
if(file.length()>1024*1024*10)
{
System.out.println("文件过大!!!!!!!");
return ;
}
Socket s = new Socket("192.168.1.160",10007);//指定服务器端ip和端口号。
FileInputStream fis = new FileInputStream(file);//将读取流和文件相关联
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();//关闭out流
InputStream in = s.getInputStream();//定义一个读取流,接受服务端反馈的数据
byte[] bufIn = new byte[1024];
int num =in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
/*
服务端:
这个服务端有局限性,当a客户端连接上以后,被服务端获取到,服务端执行具体流程
这是B客户端连接,只有等待
因为服务端还没有处理完客户端的请求,还没有选好会回来,执行下一场accept方法。
所以暂时获取不到B客户端对象。
为了可以让多个客户端同时被服务端处理,
服务端最好就是将每个客户端封装到一个单独的线程中。这样就可以同时处理多个客户端请求。
如何定义线程呢?
只要明确了每一个客户端要在你服务端执行的代码即可,将该代码存入run方法中。
*/
class PicThread implements Runnable
{
private Socket s;
PicThread(Socket s)
{
this.s = s;
}
public void run()
{
int count = 1;
String ip = s.getInetAddress().getHostAddress();//获取客户端IP地址
try
{
System.out.println(ip+"is connect");
InputStream in = s.getInputStream();//定义一个客户端读取流读取客户端发送过来的数据
File dir = new File("d:\\");
File file = new File(dir+ip+"("+(count)+")"+".jpg");
while(file.exists())
file = new File(dir+ip+"("+(++count)+")"+".jpg");
FileOutputStream fos = new FileOutputStream(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());
fos.close();
s.close();
}
catch (Exception e)
{
throw new RuntimeException(ip+"上传图片失败");
}
}
}
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10007);
while(true)
{
Socket s = ss.accept();
new Thread(new PicThread(s)).start();
}
}
}