a- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class TcpDemo1 {
- public static void main(String[] args) throws Exception, IOException {
- Socket s = new Socket("192.168.220.1", 8888);
-
- System.out.println(s.getLocalAddress() + ".." + s.getLocalPort());
- System.out.println("isConnected : " + s.isConnected());
- System.out.println("isInputShutdown : " + s.isInputShutdown()
- + ".. isOutputShutdown : " + s.isOutputShutdown());
- InputStream ins = s.getInputStream();
- OutputStream outs = s.getOutputStream();
- s.shutdownInput();
- s.shutdownOutput();
- s.close();
- System.out.println("-------------------------");
- System.out.println(s.getLocalAddress() + ".." + s.getLocalPort());
- System.out.println("isConnected : " + s.isConnected());
- System.out.println("isInputShutdown : " + s.isInputShutdown()
- + ".. isOutputShutdown : " + s.isOutputShutdown());
- }
- }
- class TcpSer{
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(8888);
- while (true) {
- Socket s = ss.accept();
- System.out.println("有客户端接入...");
- InetAddress add = s.getInetAddress();
- String ip = add.getHostAddress();
- String name = add.getHostName();
- System.out.println(ip + ".." + name);
- InputStream ins = s.getInputStream();
- OutputStream outs = s.getOutputStream();
- System.out.println(s.getLocalAddress() + ".." + s.getLocalPort());
-
- Thread.sleep(200);
- System.out.println("isConnected: " + s.isConnected());
- System.out.println("isInputShutdown : " + s.isInputShutdown()
- + ".. isOutputShutdown : " + s.isOutputShutdown());
- s.close();
- Thread.sleep(200);
- System.out.println("isConnected: " + s.isConnected());
- System.out.println("isInputShutdown : " + s.isInputShutdown()
- + ".. isOutputShutdown : " + s.isOutputShutdown());
- }
- }
- }
复制代码- /192.168.220.1..1731
- isConnected : true
- isInputShutdown : false.. isOutputShutdown : false
- -------------------------
- 0.0.0.0/0.0.0.0..1731
- isConnected : true
- isInputShutdown : true.. isOutputShutdown : true
复制代码- 有客户端接入...
- 192.168.220.1..hyning-PC
- /192.168.220.1..8888
- isConnected: true
- isInputShutdown : false.. isOutputShutdown : false
- isConnected: true
- isInputShutdown : false.. isOutputShutdown : false
复制代码
|
|