tcp添加客户端

 1 package com.bjsxt;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.net.Socket;
 6 
 7 public class BasicSocketClient {
 8     public static void main(String[] args) {
 9         Socket socket =null;
10         PrintWriter pw = null;
11         //创建Socket对象,两个参数;ip地址,服务端所监听的端口
12         try {
13              socket =new Socket("192.168.2.102",8888);
14              pw = new PrintWriter(socket.getOutputStream());
15              pw.println("服务端你好");
16              pw.flush();
17         } catch (IOException e) {
18             e.printStackTrace();
19         }finally {
20             if (pw!=null){
21                 pw.close();
22             }
23             if (socket!=null){
24                 try {
25                     socket.close();
26                 } catch (IOException e) {
27                     e.printStackTrace();
28                 }
29             }
30         }
31     }
32 }

 

posted @ 2021-05-06 21:29  马士怡  阅读(68)  评论(0)    收藏  举报