import java.io.*;
import java.net.*;
class TCPDemo
{
}
class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(9527);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
//String name = s.getInetAddress().getHostName(); //解析过程较-慢
InputStream in = s.getInputStream();
byte [] b = new byte[1024];
int len = in.read(b);
System.out.print(ip + ".." + new String(b, 0, len));
OutputStream out = s.getOutputStream();
out.write("收到请求".getBytes());
s.close();
ss.close();
}
}
class Client
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.101", 9527);
OutputStream out = s.getOutputStream();
out.write("Tcp demo -> run".getBytes());
InputStream in = s.getInputStream();
byte [] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
}
}