- import java.io.*;
- import java.net.*;
- class TcpClient{
- public static void main(String[] args) throws Exception
- {
-
- //创建客户端的socket服务,指定目的主机和端口
- Socket s = new Socket("192.168.1.9",10008);
- //为了发送数据,应该获取socket流中的输出流
- OutputStream out = s.getOutputStream();
- //想服务器发送数据
- out.write("tcp lai le".getBytes());
-
- s.close();
-
- }
- }
- class TcpServer
- {
- public static void main(String [] args) throws Exception
- {
-
- ServerSocket ss = new ServerSocket(10008);
-
-
- //通过accept方法获取连接过来的客户端对象。
- Socket s = ss.accept();
-
- String ip = s.getInetAddress().getHostAddress();
-
- System.out.println(ip+".....connectioned");
-
- InputStream in = s.getInputStream();
-
- byte[] buf = new byte[1024];
-
- int len = in.read(buf);
-
- System.out.println(new String(buf,0,len));
-
-
- s.close();//关闭客户端
-
- ss.close();
-
- }
- }
复制代码
对所学的知识点开始复习了 |
|