------- android培训、java培训、期待与您交流! ----------
import java.net.*;
/*
需求:通过udp传输方式,将一段文字数据发送出去
定义了一个udp发送端
思路:
1、建立udpsocket服务
2、提供数据,并将数据封装到数据包中
3、通过socket服务的发送功能,将数据包发出去
4、关闭资源
*/
class UdpSend
{
public static void main(String[] args) throws Exception
{
//1、创建udp服务,通过DatagramSocket对象
DatagramSocket ds = new DatagramSocket(8888);
//2、确定数据,并封装成数据包
byte[] buf = "udp ge men lai le".getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),1000);
//3、通过socket服务,将已有的数据包发送出去,通过send方法
ds.send(dp);
//4、关闭资源
ds.close();
}
}
/*
需求:
定义一个应用程序,用于接收udp协议传输的数据并处理
定义udp的接收端
思路:
1、定义udpsocket服务。通常会监听一个端口,其实就是给这个接收网络应用
程序定义数字标识,方便于明确哪些数据过来给该应用程序可以处理
2、定义一个数据包,因为要存储要接收到的字节数据,因为数据包对象中
有更多功能可以提取字节数据中的不同数据信息
3、通过socket服务的receive方法将收到的数据存入已定义好的数据包中
4、通过数据包对象的特有功能,将这些不同的数据取出,打印在控制台上
5、关闭资源
*/
class UdpRece
{
public static void main(String[] args)throws Exception
{
//1、创建udp socket,建立端点
DatagramSocket ds = new DatagramSocket(10000);
while(true)
{
//2、定义数据包,用于存储数据
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
//3、通过服务的receive方法将收到的数据存入数据包中
ds.receive(dp);//阻塞式方法
//4、通过数据包的方法获取其中的数据
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
int port = dp.getPort();
System.out.println(ip+"::"+data+"::"+port);
}
//5、关闭资源
//ds.close();
}
}
String getFile()
获取此URL的文件名
String getHost()
获取此URL的主机名(如果适用)
String getPath()
获取此URL的路径部分
int getPort()
获取此URL的协议名称
String getQuery()
获取此URL的查询部
/*
需求:长传图片
*/
/*
客户端:
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("图片格式错误,请重新选择");
return;
}
if(file.length()>1024*1024*5)
{
System.out.println("文件过大,没安好心");
return;
}
Socket s = new Socket("192.168.1.254",10007);
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();
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客户端连接,只有等待。
因为服务端还没有处理完A客户端的请求,还没有循环回来执行下次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();
try
{
System.out.println(ip+"....connected");
InputStream in = s.getInputStream();
File file = new File(ip+"("+(count)+")"+".jpg");//192.168.1.254(1).jpg
while(file.exists())
file = new File(ip+"("+(count++)+")"+".jpg");
FileOutputStream fos = new FileOutputStream("server.bmp");
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();
}
}
}