简单的通信(三)----使用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,不能使用默认的,否则会出错。

猜你喜欢

转载自www.cnblogs.com/SnailsRunning/p/10124192.html