Java中Socket的用法有以下幾種:
Socket clientSocket = new Socket();
clientSocket.connect(new InetSocketAddress("服務器IP地址", 端口號));
ServerSocket serverSocket = new ServerSocket(端口號);
Socket clientSocket = serverSocket.accept();
// 客戶端發送數據
OutputStream outputStream = clientSocket.getOutputStream();
outputStream.write(數據字節流);
// 服務器端接收數據
InputStream inputStream = clientSocket.getInputStream();
byte[] buffer = new byte[1024];
int length = inputStream.read(buffer);
clientSocket.close();
以上是Java Socket的基本用法,可以根據具體需求進行擴展和優化。