• Java Socket网络编程Client端详解


    此类实现客户端套接字(也可以就叫“套接字”)。套接字是两台机器之间的通信端点。

    Socket client = new Socket(ip,port);//创建一个流套接字并将其连接到指定 IP 地址的指定端口号。

    以下为完整的客户端示例:

    SocketClientDemo.java

    public class SocketClientDemo 
    {
        /**
         * 向指定的地址发送请求数据
         * @param ipAddr
         * @param reqData
         */
        public void sendReq(String ipAddr,String reqData)
        {
            Socket client = null;
            BufferedReader br = null;
            BufferedOutputStream bos = null;
            String respStr = "";
            
            //设置字符集编码格式
            String characterCoding = "GBK";
            
            //将ip:port 类型的字符串拆分
            int dotPos = ipAddr.indexOf(":");
            String ip = ipAddr.substring(0, dotPos).trim();
            int port = Integer.parseInt(ipAddr.substring(dotPos+1).trim());
            
            try 
            {
                client = new Socket(ip,port);
                //设置发送等待时间(单位:s)
                client.setSoLinger(true, 5);
                
                //设置超时时间(单位:ms)
                client.setSoTimeout(5000);
                
                //从client端获取输出流
                bos = new BufferedOutputStream(client.getOutputStream());
                bos.write(reqData.getBytes(characterCoding));
                bos.flush();
                
                br = new BufferedReader(new InputStreamReader(client.getInputStream()));
                respStr = br.readLine();
                System.out.println("respStr is:>>>>>>"+respStr);
            }
            catch (UnknownHostException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally
            {
                if(br != null)
                {
                    try 
                    {
                        br.close();
                    }
                    catch (IOException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(bos!=null)
                {
                    try 
                    {
                        bos.close();
                    }
                    catch (IOException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(client!=null)
                {
                    try 
                    {
                        client.close();
                    }
                    catch (IOException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    View Code

    测试类代码:

    SocketClientTest.java

    public class SocketClientTest 
    {
    
        /**
         * @param args
         */
        public static void main(String[] args)
        {
            // TODO Auto-generated method stub
            SocketClientDemo client = new SocketClientDemo();
            client.sendReq("192.168.1.136:9997", "this is socket client!");
        }
    }
    View Code
  • 相关阅读:
    在Perl里 对象就是hash引用
    第十二章 对象(上):
    连锁百货企业数据分析系统建设方案
    连锁百货企业数据分析系统建设方案
    java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException
    通过Net::Ping 理解Perl的面向对象
    org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
    Attempt to refer to a unregistered pool by its alias 'dbpool'
    Error:[$parse:lexerr]
    keepalived 监测域名解析脚本
  • 原文地址:https://www.cnblogs.com/lltse/p/3614284.html
Copyright © 2020-2023  润新知