栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
  • 客户端
  • 服务器
  • review:查询IP和端口
  • 发送文件

客户端
  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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318024.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号