TCP Client例子

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例子)