zoukankan      html  css  js  c++  java
  • TCP网络编程例子

    //客户端
    package socket;


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

    public class Client001 {
    public void client() {
    Socket socket = null;
    OutputStream os = null;
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    try {
    //创建IP对象,指明服务器端的IP地址
    InetAddress inet = InetAddress.getByName("127.0.0.1");
    //1.创建Socket对象,指明服务器的ip和端口号
    socket = new Socket(inet, 43333);
    //2.获取一个输出流,用于输出数据
    os = socket.getOutputStream();
    //3.写出数据操作
    os.write("你好,我是客户端".getBytes());
    //关闭数据输出流
    socket.shutdownOutput();
    is = socket.getInputStream();
    baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[20];
    int len1 = 0;
    while ((len1 = is.read(buffer)) != -1) {
    baos.write(buffer, 0, len1);

    }

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

    } catch (IOException e) {
    e.printStackTrace();

    } finally {
    //4.资源关闭
    if (is != null) {
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (baos != null) {
    try {
    baos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    if (os != null) {
    try {
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    if (socket != null) {
    try {
    socket.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    }
    }


    public static void main(String[] args) {
    Client001 tcp = new Client001();
    tcp.client();

    }
    }

    //服务端
    package socket;

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

    public class ServiceDem001 {
    public void server() {
    ServerSocket ss = null;
    Socket socket = null;
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    OutputStream os = null;
    try {
    //1.创建服务器端的ServeSocket,指明自己的端口号
    ss = new ServerSocket(43333);
    //2.调用accept()表示接受来自于客户端的Socket;
    socket = ss.accept();
    //3.创建一个输入流
    is = socket.getInputStream();
    //读取输入流中的数据
    baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[20];
    int len;
    while ((len = is.read(buffer)) != -1) {
    baos.write(buffer, 0, len);
    }
    System.out.println(baos.toString());
    System.out.println("收到了来自于:" + socket.getInetAddress().getHostName());
    os = socket.getOutputStream();
    os.write("你好,已收到你的信息".getBytes());


    } catch (IOException e)

    {
    e.printStackTrace();
    } finally

    {
    if (os != null) {
    try {
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    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 (ss != null) {
    try {
    ss.close();
    } catch (IOException e) {
    e.printStackTrace();

    }

    }


    }
    }

    }

    public static void main(String[] args) {
    ServiceDem001 service = new ServiceDem001();
    service.server();

    }
    }
  • 相关阅读:
    python中关于操作时间的方法(一):使用time模块
    size_t类型
    批量下载网络图片并zip打包
    遇到的java面试题
    jsp中button与submit的用法
    springmvc json字符串转化成json对象
    Cas 介绍及使用
    java中post时中文乱码
    mybatis使用generator生成对应的model、mapping配置文件、dao
    移动端接口:java写get方式访问数据(springmvc+spring。。。)
  • 原文地址:https://www.cnblogs.com/kukai/p/10814119.html
Copyright © 2011-2022 走看看