//客户端
public class TCPClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",1025);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("hello server!");
dos.flush();
dos.close();
os.close();
}
}
import java.net.*;
import java.io.*;
//服务端
public class TCPServer
{
public static void main (String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(1025);
while (true)
{
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
System.out.println("bye-bye client");
dis.close();
s.close();
//System.out.println("a client has connected!");
}
}