Java使用TCP实现数据传输实例详解
程序员文章站
2023-12-17 22:06:46
java使用tcp实现数据传输实例详解
tcp所提供服务的主要特点:
1.面向连接的传输;
2.端到端的通信;
3.高可靠性,确保传输数据的正确性,不出...
java使用tcp实现数据传输实例详解
tcp所提供服务的主要特点:
1.面向连接的传输;
2.端到端的通信;
3.高可靠性,确保传输数据的正确性,不出现丢失或乱序;
4.全双工方式传输;
5.采用字节流方式,即以字节为单位传输字节序列;
6.紧急数据传送功能。
tcp传输需要建立客户端和服务器端,即socket和server socket , 建立连接后,通过socket中的io流进行数据的传输 。传输结束后关闭socket。
客户端和服务器端是两个独立的应用程序。
以下是实现基本的tcp数据传输的demo code:
package javase.day18; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.net.serversocket; import java.net.socket; public class transtextdemo { public static void main(string[] args) { } } class tcpclient2{ public static void main(string[] args) throws exception{ system.out.println("client start..."); socket s = new socket("192.168.1.2",10005); bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); bufferedwriter bw = new bufferedwriter(new outputstreamwriter(s.getoutputstream())); string senddatastr=null; while((senddatastr=br.readline())!=null){ if(senddatastr.equals("bye")){ break; } bw.write(senddatastr); bw.newline(); bw.flush(); } bw.close(); } } class tcpserver2{ public static void main(string[] args) throws exception{ system.out.println("server start..."); serversocket ss = new serversocket(10005); socket s = ss.accept(); bufferedreader br = new bufferedreader(new inputstreamreader(s.getinputstream())); bufferedwriter bw = new bufferedwriter(new outputstreamwriter(s.getoutputstream())); string receivedatastr=null; while((receivedatastr=br.readline())!=null){ if(receivedatastr.equals("bye")){ break; } system.out.println("receive data:"+receivedatastr); } } }
使用tcp实现文本文件上传的demo code:
package javase.day18; import java.io.bufferedreader; import java.io.filereader; import java.io.filewriter; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.io.printwriter; import java.net.serversocket; import java.net.socket; public class uploadtext { public static void main(string[] args) { // todo auto-generated method stub } } class textserver{ public static void main(string[] args) throws exception{ system.out.println("server start..."); serversocket ss = new serversocket(10008); socket s = ss.accept(); bufferedreader br = new bufferedreader(new inputstreamreader(s.getinputstream())); printwriter out = new printwriter(new filewriter("c:\\java_test\\server.txt"),true); string line=null; while((line=br.readline())!=null){ system.out.println(line); out.println(line); } printwriter pw = new printwriter(new outputstreamwriter(s.getoutputstream()),true); pw.println("upload successful"); pw.close(); s.close(); ss.close(); } } class textclient{ public static void main(string[] args) throws exception{ system.out.println("client start..."); socket clientsocket = new socket("192.168.1.2",10008); bufferedreader br = new bufferedreader(new filereader("c:\\java_test\\systemdemo.java")); printwriter out = new printwriter(clientsocket.getoutputstream(),true); string line=null; while((line=br.readline())!=null){ system.out.println(line); out.println(line); } clientsocket.shutdownoutput(); bufferedreader br2 = new bufferedreader(new inputstreamreader(clientsocket.getinputstream())); string confirmmsgstr=br2.readline(); system.out.println(confirmmsgstr); br2.close(); clientsocket.close(); } }
实现图片上传的demo code:
package javase.day18; import java.io.bufferedinputstream; import java.io.bufferedoutputstream; import java.io.fileinputstream; import java.io.fileoutputstream; import java.net.serversocket; import java.net.socket; public class uploadimage { public static void main(string[] args) { } } class imageclient{ public static void main(string[] args) throws exception{ socket imageclientsocket = new socket("192.168.1.2",10010); bufferedinputstream bis = new bufferedinputstream(new fileinputstream("c:\\java_test\\client\\生活用品01.jpg")); bufferedoutputstream bos = new bufferedoutputstream(imageclientsocket.getoutputstream()); byte[] buf = new byte[1024*100]; int len = 0 ; while((len=bis.read(buf))!=-1){ bos.write(buf,0,len); } imageclientsocket.shutdownoutput(); bufferedinputstream bis2 = new bufferedinputstream(imageclientsocket.getinputstream()); byte[] buf2 = new byte[1024]; int len2 = 0 ; while((len2=bis2.read(buf2))!=-1){ system.out.println(new string(buf2,0,len2)); } bis.close(); imageclientsocket.close(); } } class imageserver{ public static void main(string[] args) throws exception{ serversocket ss = new serversocket(10010); socket s = ss.accept(); bufferedinputstream bis = new bufferedinputstream(s.getinputstream()); bufferedoutputstream bos = new bufferedoutputstream(new fileoutputstream("c:\\java_test\\server\\生活用品01.jpg")); byte[] buf = new byte[1024*100]; int len = 0 ; while((len=bis.read(buf))!=-1){ bos.write(buf, 0, len); } bufferedoutputstream bos2 = new bufferedoutputstream(s.getoutputstream()); bos2.write("upload successful".getbytes()); bos2.close(); s.close(); ss.close(); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!