客户端:TestTCPClient.java
package net;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class TestTCPClient 
{
    
public static void main(String[] args) 
    
{
        
try
        
{
            Socket s 
= new Socket("127.0.0.1",6767);
            DataOutputStream dos 
= new DataOutputStream(s.getOutputStream());
            dos.writeUTF(
"Hello I am bulktree");
            dos.flush();
            dos.close();
            s.close();
        }

        
catch(UnknownHostException ue)
        
{
            ue.printStackTrace();
        }

        
catch(IOException ie)
        
{
            ie.printStackTrace();
        }

        
    }

}


服务端TestTCPServet.java
package net;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TestTCPServer 
{
    
public static void main(String[] args) 
    
{
        
try
        
{
            ServerSocket ss 
= new ServerSocket(6767);
            Socket s 
= ss.accept();
            System.out.println(
"A Client Connection!");
            DataInputStream dis 
= new DataInputStream(s.getInputStream());
            System.out.println(dis.readUTF());
            dis.close();
            s.close();
        }

        
catch(IOException e)
        
{
            e.printStackTrace();
        }

        
    }

}