Java网络编程之TCP(二)

一、客户端发送数据

package com.xiafan.study02;

import java.net.*;
import java.io.*;

public class TestTcpClient {
    public static void main(String[] args) {
        InetAddress serverip = null;
        int port;
        Socket socket = null;
        OutputStream output = null;
        try {
                //1.知道服务器的地址和端口
                serverip = InetAddress.getByName("127.0.0.1");
                port = 9999;
                //2.创建socket链接
                socket = new Socket(serverip,port);
                //3.发送消息的io流(output)
                output = socket.getOutputStream();
                output.write("helloworld".getBytes());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            if (output!=null)
            {
                try {
                    output.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if(socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

二、服务端接收数据

package com.xiafan.study02;

import java.net.*;
import java.io.*;

public class TestTcpServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket1 = null;
        InputStream input = null;
        ByteArrayOutputStream byteoutput = null;
        try {
                //1.创建一个地址
                serverSocket = new ServerSocket(9999);
                //2.等待客户端链接
                socket1 = serverSocket.accept();
                //3.读取客户端信息
                input = socket1.getInputStream();

                //使用管道流传输数据
                byteoutput = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while((len=input.read(buffer))!=-1)
                {
                    byteoutput.write(buffer,0,len);
                }
            System.out.println(byteoutput.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            if(byteoutput!=null)
            {
                try {
                    byteoutput.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(input!=null)
            {
                try {
                    input.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket1!=null)
            {
                try {
                    socket1.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (serverSocket!=null)
            {
                try {
                    serverSocket.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

三、客户端发送文件

package com.xiafan.study02;

import java.io.*;
import java.net.*;

public class TestFileClient {
    public static void main(String[] args) throws Exception{
        //1.创建socket链接
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
        //2.创建输出流
        OutputStream os = socket.getOutputStream();
        //3.用输入流读取文件
        //File类默认路径初始为项目路径
        FileInputStream fis = new FileInputStream(new File("shark.jpg"));
        //4.写出文件
        byte[] buffer = new byte[1024];
        int len;
        while((len = fis.read(buffer))!=-1)
        {
            os.write(buffer,0,len);
        }

        //通知服务端数据已输出完毕
        socket.shutdownOutput();

        InputStream is2 = socket.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer2 = new byte[1024];
        int len2;
        while ((len2 = is2.read(buffer2))!=-1)
        {
            baos.write(buffer2,0,len2);
        }

        System.out.println(baos.toString());

        //5.关闭资源(关闭原则:晚开先关闭,先开晚关闭)
        fis.close();
        os.close();
        socket.close();

    }
}

四、服务端接受文件

package com.xiafan.study02;

import java.io.*;
import java.net.*;

public class TestFileServer {
    public static void main(String[] args) throws Exception{
        //1.创建服务
        ServerSocket serverSocket = new ServerSocket(9090);
        //2.监听客户端的链接请求
        Socket socket1 = serverSocket.accept();
        //3.获取输入流
        InputStream is = socket1.getInputStream();
        //4.文件输出
        FileOutputStream fos = new FileOutputStream(new File("copy.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while ((len=is.read(buffer))!=-1)
        {
            fos.write(buffer,0,len);
        }

        //通知客户端已接受到数据
        OutputStream output = socket1.getOutputStream();
        output.write("已接受信息".getBytes());

        fos.close();
        is.close();
        socket1.close();
        serverSocket.close();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值