大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1public static void main(String[] args) throws IOException { 2 //创建数据包对象,封装要发送的数据,接收端Ip,端口号 3 byte[] data="123udp".getBytes(); 4 //创建一个InetAddress对象,封装自己的Ip地址 5 InetAddress inetAddress =InetAddress.getByName("127.0.0.1"); 6 DatagramPacket dp =new DatagramPacket(data, data.length,inetAddress,8899); 7 //创建DatagramSocket对象, 8 DatagramSocket datagramSocket=new DatagramSocket(); 9 datagramSocket.send(dp); 10 datagramSocket.close(); 11}
1public static void main(String[] args) throws IOException { 2 //创建数据包传输对象,并绑定端口号 3 DatagramSocket ds =new DatagramSocket(8899); 4 //创建字节数组 5 byte[] buf=new byte[1024]; 6 //创建数据包对象传递字节数组 7 DatagramPacket dp =new DatagramPacket(buf, buf.length); 8 //调用ds的receive传递数组 9 ds.receive(dp); 10 String ip =dp.getAddress().getHostAddress(); 11 int port =dp.getPort(); 12 int length=dp.getLength(); 13 System.out.println(new String(buf,0,length)+"..."+ip+"..."+port); 14 ds.close(); 15}
1public static void main(String[] args) throws IOException { 2 Scanner sc=new Scanner(System.in); 3 DatagramSocket datagramSocket=new DatagramSocket(); 4 InetAddress inetAddress =InetAddress.getByName("127.0.0.1"); 5 while(true){ 6 String message=sc.nextLine(); 7 byte[] data=message.getBytes(); 8 DatagramPacket dp =new DatagramPacket(data, data.length,inetAddress,8899); 9 datagramSocket.send(dp); 10 } 11}
1public static void main(String[] args) throws IOException { 2 //创建数据包传输对象,并绑定端口号 3 DatagramSocket ds =new DatagramSocket(8899); 4 //创建字节数组 5 byte[] buf=new byte[1024]; 6 //创建数据包对象传递字节数组 7 while(true){ 8 DatagramPacket dp =new DatagramPacket(buf, buf.length); 9 //调用ds的receive传递数组 10 ds.receive(dp); 11 String ip =dp.getAddress().getHostAddress(); 12 int port =dp.getPort(); 13 int length=dp.getLength(); 14 System.out.println(new String(buf,0,length)+"..."+ip+"..."+port); 15 } 16}
1public static void main(String[] args) throws IOException { 2 Socket socket=new Socket("120.27.60.73", 8899); 3 OutputStream outStream=socket.getOutputStream(); 4 outStream.write("789456".getBytes()); 5 socket.close(); 6}
1public static void main(String[] args) throws IOException { 2 ServerSocket serverSocket=new ServerSocket(8899); 3 Socket socket=serverSocket.accept(); 4 InputStream inputStream=socket.getInputStream(); 5 byte[] buf=new byte[1024]; 6 int len=inputStream.read(buf); 7 System.out.println(new String(buf,0,len)); 8 //服务器返回数据 9 OutputStream out=socket.getOutputStream(); 10 out.write("nihao".getBytes()); 11 socket.close(); 12 serverSocket.close(); 13}
1public class TCPClient { 2 public static void main(String[] args) throws IOException{ 3 Socket socket = new Socket("127.0.0.1", 8000); 4 //获取字节输出流,图片写到服务器 5 OutputStream out = socket.getOutputStream(); 6 //创建字节输入流,读取本机上的数据源图片 7 FileInputStream fis = new FileInputStream("c:\\t.jpg"); 8 //开始读写字节数组 9 int len = 0 ; 10 byte[] bytes = new byte[1024]; 11 while((len = fis.read(bytes))!=-1){ 12 out.write(bytes, 0, len); 13 } 14 //给服务器写终止序列 15 socket.shutdownOutput(); 16 17 //获取字节输入流,读取服务器的上传成功 18 InputStream in = socket.getInputStream(); 19 20 len = in.read(bytes); 21 System.out.println(new String(bytes,0,len)); 22 23 fis.close(); 24 socket.close(); 25 } 26}
1public class Upload implements Runnable{ 2 3 private Socket socket; 4 5 public Upload(Socket socket){this.socket=socket;} 6 7 public void run() { 8 try{ 9 //通过客户端连接对象,获取字节输入流,读取客户端图片 10 InputStream in = socket.getInputStream(); 11 //将目的文件夹封装到File对象 12 File upload = new File("d:\\upload"); 13 if(!upload.exists()) 14 upload.mkdirs(); 15 16 //防止文件同名被覆盖,从新定义文件名字 17 //规则: 域名+毫秒值+6位随机数 18 String filename="itcast"+System.currentTimeMillis()+new Random().nextInt(999999)+".jpg"; 19 //创建字节输出流,将图片写入到目的文件夹中 20 FileOutputStream fos = new FileOutputStream(upload+File.separator+filename); 21 //读写字节数组 22 byte[] bytes = new byte[1024]; 23 int len = 0 ; 24 while((len = in.read(bytes))!=-1){ 25 fos.write(bytes, 0, len); 26 } 27 //通过客户端连接对象获取字节输出流 28 //上传成功写回客户端 29 socket.getOutputStream().write("上传成功".getBytes()); 30 31 fos.close(); 32 socket.close(); 33 }catch(Exception ex){ 34 35 } 36 } 37 38}
1public class TCPThreadServer { 2 public static void main(String[] args) throws IOException{ 3 ServerSocket server = new ServerSocket(8000); 4 while(true){ 5 //获取到一个客户端,必须开启新线程 6 Socket socket = server.accept(); 7 new Thread( new Upload(socket) ).start(); 8 } 9 10 } 11}
1String fileName="xdclass"+System.currentTimeMillis()+new Ranadow.nextInt(999999)+".jpg";
1File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt");