zoukankan      html  css  js  c++  java
  • java socket使用例子

    SocketServer.java
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class SocketServer {
        public static void main(String[] args) {
            try {
                //建立一个服务器socket端口
                ServerSocket serverSocket = new ServerSocket(9999);
                Socket socket = serverSocket.accept();
                String strSend = "hello, you've connected to the server";
                OutputStream outputStream = socket.getOutputStream();
                InputStream inputStream = socket.getInputStream();
                byte[] bytes = strSend.getBytes();
                outputStream.write(bytes);
                outputStream.flush();
                if(inputStream.available() > 0) {
                    int len = inputStream.available();
                    byte[] bytesRead = new byte[len];
                    inputStream.read(bytesRead);
                    String str = new String(bytesRead);
                    System.out.println(str);
                }
                inputStream.close();
                outputStream.close();
                socket.close();
            } catch (Exception ex) {
    
            }
        }
    }
    SocketClient.java
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    
    public class SocketClient {
    
        public static void main(String[] args) {
            try {
                String strHost = "127.0.0.1";
                InetSocketAddress socketAddress = new InetSocketAddress(strHost, 9999);
                Socket socket = new Socket();
                socket.connect(socketAddress);
                String strSend = "hello, i'm client";
                OutputStream outputStream = socket.getOutputStream();
                InputStream inputStream = socket.getInputStream();
                byte[] bytes = strSend.getBytes();
                outputStream.write(bytes);
                outputStream.flush();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String line;
                while((line = bufferedReader.readLine()) != null) {
                    System.out.println(line + "
    ");
                }
                //要关闭各种
                bufferedReader.close();
                inputStreamReader.close();
                inputStream.close();
                outputStream.close();
                socket.close();
            }
            catch (Exception ex) {
    
            }
        }
    }

    例子下载

  • 相关阅读:
    Asp.net HttpContext 简介
    cxf 调用 .net webservice
    Dapper.Rainbow 简单使用
    Aspose.words 书签定位
    修改WCF的默认序列化格式
    mysql8.0.18忘记密码后重置密码
    聊一聊c#中的MATH.ROUND方法
    Windows上安装MySql
    win7搭建本地SonarQube环境进行c#代码分析
    使用dapper遇到的问题及解决方法
  • 原文地址:https://www.cnblogs.com/barrysgy/p/13030675.html
Copyright © 2011-2022 走看看