Java Socket網絡編程Client端詳解

此類實現客戶端套接字(也能夠就叫「套接字」)。套接字是兩臺機器之間的通訊端點。java

 

Socket client = new Socket(ip,port);//建立一個流套接字並將其鏈接到指定 IP 地址的指定端口號。

如下爲完整的客戶端示例:socket

SocketClientDemo.javaide

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.javathis

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
相關文章
相關標籤/搜索