简单的通信(三)----使用Socket实现TCP协议

功能

客户端向服务器端发送一张文件(这里以图片为例),服务器发反馈消息给客户端。

代码

package com.demo;

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;

/**
 * 功能:客户端向服务器端发送一张文件(这里以图片为例),服务器发反馈消息给客户端。
 * 
 * @author Lynn
 *
 */
public class Demo05 {
    // 客户端;
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;
        // 先将文件读入;
        FileInputStream fis = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 6868);
            os = socket.getOutputStream();
            is = socket.getInputStream();
            fis = new FileInputStream(new File("1.jpg"));
            int len;
            byte[] content = new byte[156704];
            while ((len = fis.read(content)) != -1) {
                // 再将文件写出;
                os.write(content, 0, len);
            }
            // 关闭客户端的输出流;
            socket.shutdownOutput();

            int len1;
            byte[] content1 = new byte[20];
            while ((len1 = is.read(content1)) != -1) {
                String str = new String(content1, 0, len1);
                System.out.println(str);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

    }

    // 服务器端;
    @Test
    public void server() {
        ServerSocket serverSocket =null;
        Socket socket = null;
        InputStream is = null;
        OutputStream os = null;
        FileOutputStream  fos = null;
        try {
            serverSocket = new ServerSocket(6868);
            socket = serverSocket.accept();
            is = socket.getInputStream();
            os = socket.getOutputStream();
            fos = new FileOutputStream("2.jpg");
            int len;
            byte[] content = new byte[156704];
            while((len=is.read(content))!=-1) {
                fos.write(content,0,len);
            }
            
            os.write("成功接收文件!".getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(os!=null) {
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(serverSocket!=null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
    }

}

注意

1、这里的路径是整个工程的路径,不是当前文件包的路径。
2、使用JUit测试的时候需要将方法的修饰符设为public,不能使用默认的,否则会出错。

转载于:https://www.cnblogs.com/SnailsRunning/p/10124192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值