TCP(发消息:简易代码实现)

该博客展示了如何使用Java实现客户端与服务器之间的TCP通信,包括查询IP、端口、发送字符串消息以及文件的传输。客户端通过Socket连接到服务器,发送消息并发送文件,而服务器端则监听指定端口,接收并打印接收到的消息,同时保存接收到的文件。此示例详细说明了网络编程中基本的文件传输操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端

  1. 链接服务器Socket
  2. 发送消息
package com.ayv.try02;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

//客户端
public class TcoClientDemo01 {
    public static void main(String[] args) {
        Socket socket =null;
        OutputStream os =   null;
        try {
                //知道服务器端口号
                InetAddress serverIP = InetAddress.getByName("127.0.0.1");
                int port=9999;
                //创建一个socket链接
                socket = new Socket(serverIP,port);
                //发送一个消息IO流
                 os = socket.getOutputStream();
                os.write("hello world".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {//异常
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

服务器

  1. 建立服务的端口 serverSocket
  2. 等待用户的链接 accept
  3. 接收用户的消息
package com.ayv.try02;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端
public class TcpServeDemo02 {
    public static void main(String[] args) {
        ServerSocket serverSocket=null;
        Socket socket =null;
        InputStream is =null;
        ByteArrayOutputStream baos =null;
        try {                 
                serverSocket = new ServerSocket(9999);
                //等待客户端连接
                socket = serverSocket.accept();
                //读取客户端消息
                is = socket.getInputStream();
                //管道流
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while((len=is.read(buffer))!=-1){
                    baos.write(buffer,0,len);
                }
                System.out.println(baos.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

review:查询IP和端口

package com.ayv.try01;

import java.net.InetAddress;
import java.net.UnknownHostException;
//测试IP
public class TestInetadderss {
    public static void main(String[] args) {
        //InetAddress intAddress1 = null;
        try {
            //查询本机地址
            InetAddress intAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println(intAddress1);
            InetAddress intAddress3 = InetAddress.getByName("localhost");
            System.out.println(intAddress3);
            InetAddress intAddress4 = InetAddress.getLocalHost();
            System.out.println(intAddress4);
            //查询网站IP地址
            InetAddress intAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(intAddress2);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

发送文件

//服务端
package com.ayv.try02;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDamo2 {
    public static void main(String[] args) throws Exception {
        //创建服务
        ServerSocket serverSocket = new ServerSocket(9000);
        //监听客户端的链接
        Socket socket = serverSocket.accept(); /**阻断式监听,会一直等待客户端链接*/
        //获取输入流
        InputStream is = socket.getInputStream();
        //文件输入
        FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }

}

//客户端
package com.ayv.try02;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPClientDamo2 {
    public static void main(String[] args) throws Exception {
        //创建一个Socket链接
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
        //创建一个输出流
        OutputStream os = socket.getOutputStream();
        //读取文件
        FileInputStream fis = new FileInputStream(new File("观望.jpg"));
        //写出文件
        byte[] buffer = new byte[1024];
        int len;
        while((len=fis.read(buffer))!=-1){
            os.write(buffer,0,len);
        }
        //关闭资源
        fis.close();
        os.close();
        socket.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值