TCP编程


服务端:

1、创建ServerSocket对象serversocket。(指定服务端口port)

2、监听客户端Socket对象。(serversocket.accept(),返回客户端Socket对象socket)

3、从客户端Socket对象socket获取流对象。(socket.getInputStream()    |    socket.getOutputStream())

4、从输入流对象读取数据或往输出流写入数据。

5、释放资源。(监听的客户端Socket对象socket.close(),流会随socket关闭,serversocket不能关闭)

客户端:

1、创建Socket对象socket。(指定服务器和端口)

2、从Socket对象socket里获取流对象。(socket.getInputStream()    |    socket.getOutputStream())

3、往输出流写入数据或从输入流读取数据。

4、释放资源。(socket.close(),流会随socket关闭)

实例一:语法示例


服务端:

/*

                      * TCP协议:服务端

                      *                  1、创建ServerSocket对象

                      *                  2、用服务端Socket对象接收客户端Socket对象

                      *                  3、用客户端Socket对象获取输入流对象

                      *                  4、从输入流对象读取数据

                      *                  5、释放资源(服务端ServerSocket不需要关闭,客户端Socket需要关闭,输入流资源随之关闭)

                      *                  6、把读取的数据打印到控制台

                      */

                     //创建服务段Socket

                     ServerSocket ss = new ServerSocket(3333);

                     

                     //接收客户端套接字对象

                     Socket ac = ss.accept();

                     

                     //从客户端套接字对象里获取输入流

                     InputStream in = ac.getInputStream();

                     

                     //       从输入流读取数据

                     byte[] bys = new byte[1024];

                     int len = in.read(bys);                //返回读取数据的长度

                     

                     //把接收的数据打印到控制台

                     System.out.println(new String(bys,0,len));

                     

                     //关闭资源

                     ac.close();

客户端:

/*

                      * TCP协议:客户端

                      *                  1、创建Socket对象,指定要通信的服务器地址和端口

                      *                  2、从Socket对象获取输出流对象

                      *                  3、往输出流写入数据

                      *                  4、释放资源(释放Socket资源即可,因为输出流会资源随之释放)

                      */

                     //创建客户端Socket

                     Socket s = new Socket(InetAddress.getByName("127.0.0.1"), 3333);

                     

                     //创建输出流

                     OutputStream out = s.getOutputStream();

                     

                     //往输出流里写数据

                     out.write("Hello World,欢迎来到中国。".getBytes());

                     

                     //关闭资源

                     s.close();

实例二:从客户端键盘录入数据,保存到服务器文件中


服务端:

/*

                      * 从客户端键盘录入数据,保存到服务器文件中

                      */

                     // 创建服务端Socket对象,指定端口号

                     ServerSocket ss = new ServerSocket(3335);

                     // 接收客户端Socket对象

                     Socket ac = ss.accept();

                     InputStream in = ac.getInputStream();

                     // 利用客户端Socket对象创建输入流

                     BufferedReader br = new BufferedReader(new InputStreamReader(in));

                     

                     BufferedWriter bw = new BufferedWriter(new FileWriter("files/bw.txt"));

                     String data = null;

                     while ((data = br.readLine()) != null) {

                               bw.write(data);

                               bw.newLine();

                               bw.flush();

                     }

                     ac.close();

客户端:

/*

                      * 从客户端键盘录入数据,保存到服务器文件中

                      */

                     // 创建Socket对象,指定服务端地址和端口

                     Socket s = new Socket("localhost", 3335);

                     // 创建输出流对象

                     // 创建扫描器对象

                     Scanner sc = new Scanner(System.in);

                     BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

                     

                     while (true) {

                               String data = sc.nextLine();

                               if("886".equals(data)){

                                         break;

                               }

                               bw.write(data);

                               bw.newLine();

                               bw.flush();

                     }

                     s.close();

                     // 如果主动关闭输出流,则会自动刷新缓冲区

                     // bw.close();

实例三:客户端上传文本文件到服务器端


服务端:

/*

                      * 上传客户端文本文件到服务器端

                      */

                     // 创建服务端Socket对象,指定端口号

                     ServerSocket ss = new ServerSocket(3336);

                     // 接收客户端Socket对象

                     Socket ac = ss.accept();

                     // 利用客户端Socket对象创建输入流

                     BufferedReader br = new BufferedReader(new InputStreamReader(ac.getInputStream()));

                     

                     BufferedWriter bw = new BufferedWriter(new FileWriter("files/bw1.txt"));

                     

                     String data = null;

                     while ((data = br.readLine()) != null) {

                               bw.write(data);

                               bw.newLine();

                               bw.flush();

//                            System.out.println(data);

                     }

//                  bw.close();

                     ac.close();

客户端:

/*

                      * 上传文本文件到服务器中

                      */

                     // 创建Socket对象,指定服务端地址和端口

                     Socket s = new Socket("localhost", 3336);

                     //创建输入流

                     BufferedReader br = new BufferedReader(new FileReader("files/br.txt"));

                     

                     //创建输出流

                     BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

                     

                     String data = null;

                     while ((data = br.readLine()) != null) {

                               bw.write(data);

                               bw.newLine();

                               bw.flush();

                     }

                     br.close();

                     s.close();

实例四:客户端上传文件到服务器,给出回执信息


服务端:

/*

                      * 客户端上传文件,保存到服务器文件中,给出提示

                      */

                     // 创建服务端Socket对象,指定端口号

                     ServerSocket ss = new ServerSocket(3336);

                     // 接收客户端Socket对象

                     Socket ac = ss.accept();

                     // 利用客户端Socket对象创建输入流

                     BufferedReader br = new BufferedReader(new InputStreamReader(ac.getInputStream()));

                     

                     BufferedWriter bw = new BufferedWriter(new FileWriter("files/bw2.txt"));

                     

                     String data = null;

                     while ((data = br.readLine()) != null) {           //注意:此处一直读不到null(如果客户端关闭流,则此处不再等待,继续下面语句)

                               bw.write(data);

                               bw.newLine();

                               bw.flush();

                     }

                     bw.close();

                     

                     BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(ac.getOutputStream()));

                     bw1.write("文件已收到");

//                  bw1.newLine();

                     bw1.flush();            //使用带缓冲区的流时,一定要注意,不刷新缓冲区,则数据一直保存在缓冲区中,不会到流中

                     

//                  ac.getOutputStream().write("文件已收到".getBytes());

                     

                     ac.close();

客户端:

/*

                      * 客户端上传文件,保存到服务器文件中,给出提示

                      */

                     // 创建Socket对象,指定服务端地址和端口

                     Socket s = new Socket("localhost", 3336);

                     //创建输入流

                     BufferedReader br = new BufferedReader(new FileReader("files/br.txt"));

                     

                     //创建输出流

                     BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

                     

                     String data = null;

                     while ((data = br.readLine()) != null) {

                               bw.write(data);

                               bw.newLine();

                               bw.flush();

                     }

                     br.close();

                     

                     //半关闭套接字(关闭此流,然后服务端语句则不停留在流的语句上,而是我)

                     s.shutdownOutput();

                     

                     BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));

//                  char[] bys = new char[1024];

//                  int len = br1.read(bys);

//                  System.out.println(new String(bys, 0, len));

                     System.out.println(br1.readLine());

                     

//                  System.out.println(br1.readLine());

                     

//                  InputStream input = s.getInputStream();

//                  byte[] bys = new byte[1024];

//                  int len = input.read(bys);

//                  System.out.println(new String(bys,0,len));

                     

                     s.close();

实例五:客户端上传图片到服务器,给出回执信息


客户端:

/*

                      * 客户端上传图片,保存到服务器文件中,给出提示

                      */

                     // 创建服务端Socket对象,指定端口号

                     ServerSocket ss = new ServerSocket(3333);

                     // 接收客户端Socket对象

                     Socket ac = ss.accept();

                     //封装输入流

                     BufferedInputStream bis = new BufferedInputStream(ac.getInputStream());

                     

                     //封装输出流

                     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("files/copy.jpg"));

                     

                     byte[] bys = new byte[1024];

                     int len = -1 ;

                     while((len = bis.read(bys)) != -1){        

                               bos.write(bys,0,len);

                               bos.flush();

                     }

                     

                     ac.getOutputStream().write("图片已收到".getBytes());

                     

                     ac.close();

                     bos.close();

服务端:

/*

                      * 客户端上传图片,保存到服务器文件中,给出提示

                      */

                     // 创建Socket对象

                     Socket s = new Socket("localhost", 3333);

                     // 创建输入流

                     BufferedInputStream bis = new BufferedInputStream(new FileInputStream("files/111.jpg"));

                     // 封装输出流

                     BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());

                     byte[] bys = new byte[1024];

                     int len = -1;

                     while ((len = bis.read(bys)) != -1) {

                               bos.write(bys,0,len);

                               bos.flush();

                     }

                     

                     //半关闭客户端套接字

                     s.shutdownOutput();

                     InputStream in = s.getInputStream();

                     byte[] buf = new byte[1024];

                     int i = -1 ;

                     while((i = in.read(buf)) != -1){

                               System.out.println(new String(buf,0,i));

                     }

                     

                     s.close();

                     bis.close();