------期待与您交流! -------
java网络编程总结
TCP/IP协议,他把网络分为四层:应用层,传输层,网络层,网络接口层。
TCP在传输层,IP在网络层。TCP协议是用于传输的,而IP是用于寻址的。
对Java.net包,括重要的类有InetAddress类,ServerSocket类,Socket类,DatagramSocket类,DatagramPacket类,URL类,和URLConnection类。
InetAddress类,是用来封装计算机的IP地址和DNS的。
DatagramPacket是起到数据窗口作用的一个类,DatagramSocket是用于发送和接收DatagramPacket的机制。
TCP/IP套接了是最可靠的双向流协议,通过三次握手来确认传输的可靠性,等待客户端的服务器使用ServerSocket类,而要连接到服务器的客户端则使用Socket类。
URL使用地址来帮助查找Internet上的文件,URLConnection类是一个通用类,提供用于进一步了解远程资源的方法。
利用套接字可以传输对象,为了解决性能产常用的办法是通过套接字发送流。
具体的实例代码如下:
文本文件传输
/*客户端*/public class FileClient { public static void main(String[] args) throws Exception, IOException { Socket s = new Socket("localhost" , 10001); BufferedReader bufr = new BufferedReader(new FileReader( "FileServer.java")); PrintWriter out = new PrintWriter(s.getOutputStream(),true); String line = null; while ((line = bufr.readLine()) != null) { out.println(line); } s.shutdownOutput(); // 关闭客户端输出流,相当于在流中加入结束标记 -1 BufferedReader bufIn = new BufferedReader(new InputStreamReader(s .getInputStream())); String serverResult = bufIn.readLine(); System. out.println(serverResult); s.close(); } }
//服务端public class FileServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10001); Socket client = ss.accept(); BufferedReader bufIn = new BufferedReader(new InputStreamReader(client .getInputStream())); System. out.println("ip:" +client.getInetAddress().getHostAddress()); PrintWriter pw = new PrintWriter(new FileWriter("save.txt"), true); String line = null; while ((line = bufIn.readLine()) != null) { System. out.println(line); pw.println(line); } PrintWriter out = new PrintWriter(client.getOutputStream()); out.println( "上传成功" ); out.close(); pw.close(); client.close(); ss.close(); } }
-------------------------------------------------
服务端并发接收图片文件上传
-------------------------------------------------
/* * 客户端 * 功能:发送图片文件 * */ public class ConcurrentPicClient { public static void main(String[] args) throws Exception { Socket s = new Socket("localhost" , 10001); InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); //读文件 FileInputStream is = new FileInputStream(new File("test.gif" )); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) != -1) { out.write(buf, 0, len); } s.shutdownOutput(); // 标记文件传递结束 len = in.read(buf); System. out.println(new String(buf, 0, len)); is.close(); s.close(); } }/* * 服务端 * 功能:并发接收图片上传 * 图片文件使用Stream进行读写,不适用Reader/Writer * */ public class ConcurrentServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10001); while (true ) { new Thread(new ServerThread(ss.accept())).start(); } } } class ServerThread implements Runnable { private Socket s ; public ServerThread(Socket s) { this.s = s; } public void run() { int count = 1; String ip = s.getInetAddress().getHostAddress(); System. out.println(ip + "...connected" ); try { InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); // 保证file不重名 File file = new File(ip + "(" + count + ").jpg"); while (file.exists()) { count++; file = new File(ip + "(" + count + ").jpg"); } // 写文件 FileOutputStream fo = new FileOutputStream(file); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { fo.write(buf, 0, len); } out.write( "上传成功" .getBytes()); fo.close(); s.close(); } catch (Exception e) { } } }