Java学习笔记53(网络编程:TCP协议案例)
2018-06-18 03:42:30来源:未知 阅读 ()
简易的案例
客户端:
package demo; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class TCPClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1", 7000); OutputStream out = socket.getOutputStream(); out.write("hello".getBytes()); InputStream in = socket.getInputStream(); byte[] data = new byte[1024]; int len = in.read(data); System.out.println(new String(data, 0, len)); socket.close(); } }
服务器端:
package demo; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(7000); Socket socket = server.accept(); InputStream in = socket.getInputStream(); byte[] data = new byte[1024]; int len = in.read(data); System.out.println(new String(data, 0, len)); OutputStream out = socket.getOutputStream(); out.write("收到".getBytes()); socket.close(); server.close(); } }
先启动服务器端,再启动客户端,服务器端打印:hello,然后客户端打印:收到
注意区分这里的流对象
接下来在这个案例基础上做一个图片上传案例
图片上传的本质就是文件复制
客户端:
package demo1; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class TCPClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1", 7000); OutputStream out = socket.getOutputStream(); FileInputStream fis = new FileInputStream("d:\\666.jpeg"); int len = 0; byte[] bytes = new byte[1024]; while ((len = fis.read(bytes)) != -1) { out.write(bytes, 0, len); } socket.shutdownOutput(); // 服务器端读不到-1,不会终止,调用这个方法可以终止 InputStream in = socket.getInputStream(); len = in.read(bytes); System.out.println(new String(bytes, 0, len)); fis.close(); socket.close(); } }
服务器端:
package demo1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Random; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(7000); Socket socket = server.accept(); InputStream in = socket.getInputStream(); File upload = new File("d:\\upload"); if (!upload.exists()) { upload.mkdirs(); } String filename = System.currentTimeMillis() + new Random().nextInt(999999) + ".jpeg"; FileOutputStream fos = new FileOutputStream(upload + File.separator + filename); // 这里是处理图片重名的问题 byte[] bytes = new byte[1024]; int len = 0; while ((len = in.read(bytes)) != -1) { fos.write(bytes, 0, len); } socket.getOutputStream().write("上传成功".getBytes()); fos.close(); socket.close(); server.close(); } }
如果多个客户端一起上传,就需要多线程的技术,每个客户端占一个线程
客户端不需要修改
服务器端:
package demo1; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(7000); while (true) { Socket socket = server.accept(); new Thread(new Upload(socket)).start(); } } }
package demo1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Random; public class Upload implements Runnable { private Socket socket; public Upload(Socket socket) { this.socket = socket; } public void run() { try { InputStream in = socket.getInputStream(); File upload = new File("d:\\upload"); if (!upload.exists()) { upload.mkdirs(); } String filename = System.currentTimeMillis() + new Random().nextInt(999999) + ".jpeg"; FileOutputStream fos = new FileOutputStream(upload + File.separator + filename); // 这里是处理图片重名的问题 byte[] bytes = new byte[1024]; int len = 0; while ((len = in.read(bytes)) != -1) { fos.write(bytes, 0, len); } socket.getOutputStream().write("上传成功".getBytes()); fos.close(); socket.close(); } catch (IOException ex) { System.out.println(ex); } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash