首页 技术 正文
技术 2022年11月9日
0 收藏 985 点赞 4,954 浏览 1936 个字

一个实例通过client端和server端通讯

客户端通过TCP/IP传输资源文件,比如图片,文字,音频,视频等…..

服务端接受到文件存入本地磁盘,返回接受到:“收到来自于”+s.getInetAddress().getHostName()+”的信息”

TestTCP3:

附源码:

package com.ykw.net;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

//TCP编程例三:从客户端发送文件给服务端,服务端保存到本地。并返回”发送成功”给客户端。并关闭相应的连接
public class TestTCP3 {

@Test
    public void client()throws Exception{
        //1.创建Socket的对象
        Socket socket = new Socket(InetAddress.getByName(“127.0.0.1”),9898);
        //2.从本地获取一个文件发送给服务端
        OutputStream os = socket.getOutputStream();
        FileInputStream fis = new FileInputStream(new File(“1.jpg”));
        byte[] b = new byte[1024];
        int len;
        while((len=fis.read(b))!=-1){
            os.write(b,0,len);
        }
        socket.shutdownOutput();
        //3.接受来自于服务端的信息
        InputStream is = socket.getInputStream();
        byte[] b1 = new byte[1024];
        int len1;
        while((len1 = is.read(b1))!=-1){
            String str = new String(b1,0,len1);
            System.out.println(str);
        }
        //4.关闭相应的流和Socket对象
        is.close();
        os.close();
        fis.close();
        socket.close();
    }
    
    @Test
    public void server()throws Exception{
        //1.创建一个ServerSocket对象
        ServerSocket ss = new ServerSocket(9898);
        //2.调用其accept()方法,返回一个Socket对象
        Socket s = ss.accept();
        //3.将从客户端发送过来的信息保存到本地
        InputStream is = s.getInputStream();
        FileOutputStream fos = new FileOutputStream(new File(“D://5.jpg”));
        byte[] b = new byte[1024];
        int len;
        while((len = is.read(b))!=-1){
            fos.write(b,0,len);
        }
        System.out.println(“收到来自于”+s.getInetAddress().getHostAddress()+”的文件”);
        //4.发送“接收成功”的信息反馈给客户端
        OutputStream os = s.getOutputStream();
        os.write(“您发送的图片我已接收成功”.getBytes());
        //5.关闭相应的流和Socket及ServerSocket的对象
        os.close();
        fos.close();
        is.close();
        s.close();
        ss.close();
    }
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290