美文网首页
TCP Client例子

TCP Client例子

作者: 招风小妖怪 | 来源:发表于2019-07-15 10:24 被阅读0次
    import java.net.*;
    import java.io.*;
    class Client
    {
        Client()
        {
            try
            {
                //服务器的ip地址,
                InetAddress    ip     = InetAddress.getByName("172.20.139.130");
                //发出连接
                Socket         socket = new Socket(ip,8888);
                InputStream    is     = socket.getInputStream();
                OutputStream   os     = socket.getOutputStream();
                
                BufferedReader br     = new BufferedReader(new InputStreamReader(is));
                PrintWriter    pw     = new PrintWriter(os,true);
                
                for(int i=0;i<10;i++)
                {
                    pw.println("我便去漂泊流浪.....");
                    String     info   = br.readLine();
                    System.out.println("from client:"+info);
                    Thread.sleep(1000);
                }
                
                br.close();
                pw.close();
                is.close();
                os.close();
                socket.close();
                
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        
        public static void main(String s[])
        {
            new Client();
        }
    }
    
    /*
    1、预习下容器
    2、swing
    3、用io技术+awt技术做出文件的读取、复制、删除、修改
    */
    
    
    

    相关文章

      网友评论

          本文标题:TCP Client例子

          本文链接:https://www.haomeiwen.com/subject/bvpgkctx.html