Socket网络编程简单实例Demo,文件传输。

来源:互联网 发布:手机知乎怎么复制 编辑:程序博客网 时间:2024/05/22 16:48

tcp/ip传输协议方式:

package com.text.sgl;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import org.junit.Test;/**  * Socket编程简单demo  * @author shen.guoliang  * @version 1.0, 2017年8月28日  每次修改后更新版本号,日期和修改内容  * @see[相关类/方法]  * @since[产品/模块版本]  */public class SocketTest {//客户端@Testpublic void client(){Socket s=null;OutputStream out=null;FileInputStream pic=null;InputStream i=null; BufferedInputStream bfi=null;try {  //生成socket链接s = new Socket(InetAddress.getByName("127.0.0.1"), 9999);//通过socket生成输出到服务器端的输出流out = s.getOutputStream();//获取客户端项目路径文件psbpic = new FileInputStream (new File("psb.png"));    //使用缓冲流包装提速bfi= new BufferedInputStream(pic);byte [] b = new byte[1024];int len;while((len = bfi.read(b))!=-1){//将psb文件写出到输出流里面out.write(b, 0, len);}//应为客户端与服务器端用的原始InputStream和OutputStream传输的,具有阻塞性//所以上面文件输出完了需要,告诉socket关闭output方便后面接收服务器端传回的消息s.shutdownOutput();//使用socket接收服务器端返回的消息输出i = s.getInputStream();byte [] b2= new byte[1024];int len1;while((len1=i.read(b2))!=-1){System.out.println(new String(b2,0, len1));}}  catch (IOException e) {e.printStackTrace();}finally {if(bfi!=null){try {bfi.close();} catch (IOException e) {e.printStackTrace();}}if(i!=null){try {i.close();} catch (IOException e) {e.printStackTrace();}}if(pic!=null){try {pic.close();} catch (IOException e) {e.printStackTrace();}}if(out!=null){try {out.close();} catch (IOException e) {e.printStackTrace();}}if(s!=null){try {s.close();} catch (IOException e) {e.printStackTrace();}}}}//服务端@Testpublic void server(){ServerSocket ss=null;Socket s=null;InputStream is=null;FileOutputStream out=null;BufferedOutputStream bout=null;OutputStream o=null;try { //服务器端通过相同的端口号创建serversocket对象 ss = new ServerSocket(9999); //通过serversocket对象得到socket对象 s = ss.accept();     //通过socket获取客户端发送的流 is = s.getInputStream(); out = new FileOutputStream("psb2.png"); bout = new BufferedOutputStream(out);byte [] b = new byte[1024];int len;//读取客户端发送的流while((len=is.read(b))!=-1){//写入服务器端文件系统bout.write(b, 0, len);}//返回消息给客户端o = s.getOutputStream();o.write("我已经接收到图片了".getBytes());} catch (IOException e) {e.printStackTrace();}finally {if(o!=null){try {o.close();} catch (IOException e) {e.printStackTrace();}}if(bout!=null){try {bout.close();} catch (IOException e) {e.printStackTrace();}}if(out!=null){try {out.close();} catch (IOException e) {e.printStackTrace();}}if(is!=null){try {is.close();} catch (IOException e) {e.printStackTrace();}}if(s!=null){try {s.close();} catch (IOException e) {e.printStackTrace();}}if(ss!=null){try {ss.close();} catch (IOException e) {e.printStackTrace();}}}}}


注意:

1:inputstream和outputstream存在阻塞问题。需要使用shutdownOutput()方法,提示服务端本次传输完成
2:注意资源,finally里面关闭所有资源



udp传输协议方式:


/** * udp传输协议测试 *  * @author shen.guoliang * @version 1.0, 2017年8月28日 每次修改后更新版本号,日期和修改内容 * @see [相关类/方法] * @since [产品/模块版本] */public class UdpTest {// 发送端@Testpublic void send() {DatagramSocket ds = null;try {ds = new DatagramSocket();byte[] b = "你好,这是我发送的数据".getBytes();//创建一个数据报,每个数据报不大于64K,都记录这数据信息,发送端的ip,端口号,以及要发送到的接收端的ip,端口号DatagramPacket dp = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 9999);//使用DatagramSocket发数据报发送出去ds.send(dp);} catch (IOException e) {e.printStackTrace();} finally {if (ds != null) {ds.close();}}}// 接收端@Testpublic void rceive() {DatagramSocket ds = null;try {//使用相同的端口接收信息ds = new DatagramSocket(9999);//创建字节数组接收数据byte [] b = new byte[1024];DatagramPacket dp = new DatagramPacket(b, 0, b.length);ds.receive(dp);System.out.println(new String(b));}catch (IOException e) {e.printStackTrace();}finally {if(ds!=null){ds.close();}}}}





原创粉丝点击