服务端接收发送消息:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServerTest {
public static void main(String[] args) throws IOException {
//9999为server端端口号
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
byte[] bytes = new byte[1024];
InputStream inputStream = socket.getInputStream();
int line = 0;
while ((line = inputStream.read(bytes))!= -1){
System.out.println("服务端收到消息: "+new String(bytes,0,line));
}
//关闭输入流,但socket仍然是连接状态,相当于给流中加入一个结束标记
socket.shutdownInput();
OutputStream outputStream = socket.getOutputStream();
//发送消息
outputStream.write("hello,client".getBytes());
socket.shutdownOutput();
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
客户端发送接收消息:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class SocketClientTest {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello,server".getBytes());
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int read = 0;
while ((read = inputStream.read(bytes))!=-1){
System.out.println("客户端收到消息: "+new String(bytes,0,read));
}
inputStream.close();
outputStream.close();
socket.close();
}
}
注意:启动时,需要先启动服务端,在启动客户端
接收发送文件服务端接收文件并保存:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServerTest1 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
//接收的文件路径
String path = "D:/1234.csv";
File file = new File(path);
if(!file.exists()){
file.createNewFile();
}
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
while ((len = inputStream.read(bytes))!=-1){
bufferedOutputStream.write(bytes,0,len);
}
System.out.println("文件已保存。。。");
bufferedOutputStream.close();
inputStream.close();
socket.close();
}
}
客户端发送文件:
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class SocketClientTest1 {
public static void main(String[] args) throws IOException {
//要发送的文件路径
String path = "D:/tmp/202108041124.csv";
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
File file = new File(path);
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[1024];
int len = 0;
OutputStream outputStream = socket.getOutputStream();
while ((len = bufferedInputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
System.out.println("发送完成。。。");
outputStream.close();
bufferedInputStream.close();
socket.close();
}
}
注意:启动时,需要先启动服务端,在启动客户端



