标签:
------- android培训、java培训、期待与您交流! ----------、
网络编程
(1)用编程语言来实现计算机的资源共享和信息传递。<span style="white-space:pre"> </span>DatagramSocket ds = new DatagramSocket(); byte[] bys = "udp,我来了".getBytes(); DatagramPacket dp = new DatagramPacket (bys,bys.length,InetAddress.getByName("192.168.32.80"),12345); ds.send(dp); ds.close();
<span style="white-space:pre"> </span>DatagramSocket ds = new DatagramSocket(12345); byte[] bys = new byte[1024]; DatagramPacket dp = new DatagramPacket(bys,bys.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); String data = new String(dp.getData(),0,dp.getLength()); System.out.println(ip+"---"+data); ds.close();<span style="font-family: 'Microsoft YaHei'; background-color: rgb(255, 255, 255);"> </span>(4)TCP发送和接收数据
Socket s = new Socket("192.168.32.80",12345); OutputStream os = s.getOutputStream(); os.write("tcp,我来了".getBytes()); s.close();B:服务器端
ServerSocket ss = new ServerSokcet(12345); Socket s = ss.accept(); InputStream is = s.getInputStream(); byte[] bys = new byte[1024]; int len = is.read(bys); String data = new String(bys,0,len); System.out.println(data); s.close(); ss.close();
E:客户端是读取图片,服务器是写到图片。反馈信息
客户端代码:
<span style="white-space:pre"> </span>public class ClientDemo { <span style="white-space:pre"> </span><span style="white-space:pre"> </span>public static void main(String[] args) throws IOException { <span style="white-space:pre"> </span> // 窗口客户端对象 <span style="white-space:pre"> </span> Socket s = new Socket("192.168.237.1", 12345); <span style="white-space:pre"> </span> InputStream is = new FileInputStream("test.jpg"); <span style="white-space:pre"> </span> // 写到通道 <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>OutputStream os = s.getOutputStream(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>int by = 0; <span style="white-space:pre"> </span> while ((by = is.read()) != -1) { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> os.write(by); <span style="white-space:pre"> </span> } <span style="white-space:pre"> </span><span style="white-space:pre"> </span>s.shutdownOutput(); <span style="white-space:pre"> </span> BufferedReader reader = new BufferedReader(new InputStreamReader( <span style="white-space:pre"> </span> s.getInputStream())); <span style="white-space:pre"> </span> String data = reader.readLine(); // 阻塞式 <span style="white-space:pre"> </span> System.out.println("client:" + data); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> // 释放资源 <span style="white-space:pre"> </span> is.close(); <span style="white-space:pre"> </span> s.close(); <span style="white-space:pre"> </span><span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}服务器端代码:
<span style="white-space:pre"> </span>public class ServerDemo { <span style="white-space:pre"> </span> public static void main(String[] args) throws IOException { <span style="white-space:pre"> </span> // 创建服务器对象 <span style="white-space:pre"> </span> ServerSocket ss = new ServerSocket(12345); <span style="white-space:pre"> </span> // 监听客户端连接 <span style="white-space:pre"> </span> Socket s = ss.accept(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>// 获取输入流 <span style="white-space:pre"> </span> InputStream is = s.getInputStream(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>// 获取输出流 <span style="white-space:pre"> </span> OutputStream os = new FileOutputStream("copy.jpg"); <span style="white-space:pre"> </span> int by = 0; <span style="white-space:pre"> </span> while ((by = is.read()) != -1) { <span style="white-space:pre"> </span> os.write(by); <span style="white-space:pre"> </span> } <span style="white-space:pre"> </span> // 给出反馈 <span style="white-space:pre"> </span><span style="white-space:pre"> </span> BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( <span style="white-space:pre"> </span><span style="white-space:pre"> </span> s.getOutputStream())); <span style="white-space:pre"> </span><span style="white-space:pre"> </span> writer.write("文件上传成功"); <span style="white-space:pre"> </span><span style="white-space:pre"> </span> writer.newLine(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>writer.flush(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span><span style="white-space:pre"> </span>os.close(); <span style="white-space:pre"> </span><span style="white-space:pre"> </span> s.close(); <span style="white-space:pre"> </span> ss.close(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}运行结果:
<span style="white-space:pre"> </span>client:文件上传成功
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/chaoyangmemory/article/details/46698605