Socket、ServerSocket

1、服务器端程序

 1 package demo12.net;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 import java.net.ServerSocket;
 7 import java.net.Socket;
 8 
 9 /*
10     TCP通信的服务器端:接收客户端的请求,读取客户端发送的数据,给客户端回写数据
11     
12     表示服务器的类:
13         java.net.ServerSocket:此类实现服务器套接字。
14 
15     构造方法:
16         ServerSocket(int port) 创建绑定到特定端口的服务器套接字。
17 
18     服务器端必须明确一件事情,必须的知道是哪个客户端请求的服务器
19     所以可以使用accept方法获取到请求的客户端对象Socket
20     成员方法:
21         Socket accept() 侦听并接受到此套接字的连接。
22 
23     服务器的实现步骤:
24         1.创建服务器ServerSocket对象和系统要指定的端口号
25         2.使用ServerSocket对象中的方法accept,获取到请求的客户端对象Socket
26         3.使用Socket对象中的方法getInputStream()获取网络字节输入流InputStream对象
27         4.使用网络字节输入流InputStream对象中的方法read,读取客户端发送的数据
28         5.使用Socket对象中的方法getOutputStream()获取网络字节输出流OutputStream对象
29         6.使用网络字节输出流OutputStream对象中的方法write,给客户端回写数据
30         7.释放资源(Socket,ServerSocket)
31  */
32 
33 public class TCPserver {
34     public static void main(String[] args) throws IOException {
35         ServerSocket serverSocket = new ServerSocket(9090);
36         System.out.println("服务器端已启动...");
37         Socket socket = serverSocket.accept();
38         InputStream is = socket.getInputStream();
39         byte[] bytes = new byte[1024];
40         int len = is.read(bytes);
41         System.out.println("服务器收到:" + new String(bytes, 0, len));
42         OutputStream os = socket.getOutputStream();
43         os.write("我收到来自您的消息".getBytes());
44         socket.close();
45     }
46 }

2、客户端程序

 1 package demo12.net;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 import java.net.Socket;
 7 
 8 /*
 9     TCP通信的客户端:向服务器发送连接请求,给服务器发送数据,读取服务器回写的数据
10 
11     表示客户端的类:
12         java.net.Socket:此类实现客户端套接字(也可以就叫“套接字”)。套接字是两台机器间通信的端点。
13         套接字:包含了IP地址和端口号的网络单位
14 
15     构造方法:
16         Socket(String host, int port) 创建一个流套接字并将其连接到指定主机上的指定端口号。
17         参数:
18             String host:服务器主机的名称/服务器的IP地址
19             int port:服务器的端口号
20 
21     成员方法:
22         OutputStream getOutputStream() 返回此套接字的输出流。
23         InputStream getInputStream() 返回此套接字的输入流。
24         close() 关闭此套接字。
25 
26     实现步骤:
27         1.创建一个客户端对象Socket,构造方法绑定服务器的IP地址和端口号
28         2.使用Socket对象中的方法getOutputStream()获取网络字节输出流OutputStream对象
29         3.使用网络字节输出流OutputStream对象中的方法write,给服务器发送数据
30         4.使用Socket对象中的方法getInputStream()获取网络字节输入流InputStream对象
31         5.使用网络字节输入流InputStream对象中的方法read,读取服务器回写的数据
32         6.释放资源(Socket)
33      注意:
34         1.客户端和服务器端进行交互,必须使用Socket中提供的网络流,不能使用自己创建的流对象
35         2.当我们创建客户端对象Socket的时候,就会去请求服务器和服务器经过3次握手建立连接通路
36             这时如果服务器没有启动,那么就会抛出异常ConnectException: Connection refused: connect
37             如果服务器已经启动,那么就可以进行交互了
38  */
39 
40 public class TCPclient {
41     public static void main(String[] args) throws IOException {
42         Socket socket = new Socket("127.0.0.1", 9090);
43         OutputStream os = socket.getOutputStream();
44         os.write("你好,我是客户端.".getBytes());
45         InputStream is = socket.getInputStream();
46         byte[] bytes = new byte[1024];
47         int len = is.read(bytes);
48         System.out.println(new String(bytes, 0, len));
49         socket.close();
50     }
51 }

3、上传文件案例

1、服务器端

 1 package demo12.net;
 2 
 3 import java.io.*;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 import java.util.Random;
 7 
 8 public class uploadServer {
 9     public static void main(String[] args) throws IOException {
10         ServerSocket serverSocket = new ServerSocket(9090);
11         System.out.println("服务器端已启动...");
12 
13         while (true) {
14             Socket socket = serverSocket.accept();
15             new Thread(new Runnable() {
16                 @Override
17                 public void run() {
18                     FileOutputStream fos = null;
19                     try {
20                         File file = new File("d:\upload");
21                         if (!file.exists()) {
22                             file.mkdirs();
23                         }
24                         String filename = "upload_file" + System.currentTimeMillis() + new Random().nextInt(9999) + ".jpg";
25                         fos = new FileOutputStream(file + "\" + filename);
26                         InputStream is = socket.getInputStream();
27                         int len = 0;
28                         byte[] bytes = new byte[1024];
29                         while ((len = is.read(bytes)) != -1) {
30                             fos.write(bytes, 0, len);
31                         }
32                         OutputStream ops = socket.getOutputStream();
33                         ops.write("文件上传完毕".getBytes());
34                         System.out.println("文件上传完毕...");
35                     } catch (IOException e) {
36                         System.out.println(e);
37                     } finally {
38                         try {
39                             fos.close();
40                             if (socket != null) {
41                                 socket.close();
42                             }
43                         } catch (IOException e) {
44                             e.printStackTrace();
45                         }
46                     }
47                 }
48             }).start();
49         }
50     }
51 }

2、客户端

 1 package demo12.net;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.OutputStream;
 7 import java.net.Socket;
 8 
 9 public class uploadClient {
10     public static void main(String[] args) throws IOException {
11         Socket socket = new Socket("127.0.0.1", 9090);
12         FileInputStream fis = new FileInputStream("c:\1.jpg");
13         OutputStream ops = socket.getOutputStream();
14         int len = 0;
15         byte[] bytes = new byte[1024];
16         while ((len = fis.read(bytes)) != -1) {
17             ops.write(bytes, 0, len);
18         }
19         socket.shutdownOutput();
20         InputStream is = socket.getInputStream();
21         byte[] bytes1 = new byte[1024];
22         int len1 = is.read(bytes1);
23         System.out.println(new String(bytes1, 0, len1));
24         fis.close();
25         socket.close();
26 
27     }
28 }
# 注:该服务器端实现了多线程上传
原文地址:https://www.cnblogs.com/sun-10387834/p/13523165.html