起初TCP基本通信
public class SocketServer01 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端正在9999端口监听,等待连接...");
Socket accept = serverSocket.accept();
System.out.println("服务端 socket返回 = " + accept.getClass());
InputStream inputStream = accept.getInputStream();
byte[] b = new byte[1024];
int readLine = 0;
while((readLine = inputStream.read(b)) != -1) {
System.out.println(new String(b, 0, readLine));
}
OutputStream outputStream = accept.getOutputStream();
outputStream.write("hello,client".getBytes(StandardCharsets.UTF_8));
accept.shutdownOutput();
inputStream.close();
outputStream.close();
accept.close();
serverSocket.close();
}
}
public class SocketClient01 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端 socket返回 = " + socket.getClass());
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello,server".getBytes(StandardCharsets.UTF_8));
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int readLen = 0;
while((readLen = inputStream.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, readLen));
}
inputStream.close();
outputStream.close();
socket.close();
}
}
UDP基本通信
public class SenderUdp {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8889);
byte[] data = "四大名著是哪些?".getBytes(StandardCharsets.UTF_8);
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName("192.168.80.1"), 8888);
socket.send(packet);
byte[] res = new byte[1024];
packet = new DatagramPacket(res, res.length);
socket.receive(packet);
System.out.println(new String(packet.getData(), 0, packet.getLength()));
socket.close();
}
}
public class ReceiveUdp {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String res = new String(packet.getData(), 0, packet.getLength());
System.out.println("收到了消息:" + res);
if(res.equals("四大名著是哪些?")) {
res = "<<红楼梦>,<>,<>,<>>";
}
else res = "what?";
packet = new DatagramPacket(res.getBytes(StandardCharsets.UTF_8), res.getBytes(StandardCharsets.UTF_8).length, InetAddress.getByName("192.168.80.1"), 8889);
socket.send(packet);
socket.close();
}
}
TCP双端通信简单聊天回复
public class TcpClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while(loop){
String info = scanner.next();
if(info.equals("exit")) {
loop = false;
}
bufferedWriter.write(info);
bufferedWriter.newLine();
bufferedWriter.flush();
System.out.println(bufferedReader.readLine());
}
bufferedReader.close();
bufferedWriter.close();
socket.close();
}
}
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
boolean loop = true;
while(loop){
String s = bufferedReader.readLine();
System.out.println("收到了来自客户端的消息:" + s);
String res = "";
if (s.equals("name")) {
res = "我是nova";
} else if(s.equals("hobby")){
res = "编写java程序";
}
else if(s.equals("exit")) {
res = "拜拜~";
loop = false;
}
else {
res = "你说啥呢";
}
bufferedWriter.write(res);
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedWriter.close();
bufferedReader.close();
socket.close();
serverSocket.close();
}
}
TCP客户端上传文件到服务器
public class TCPFileClient02 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
System.out.println("socket端口是:" + socket.getPort());
String srcPath = "e:/background-pic/beau.jpg";
FileInputStream fileInputStream = new FileInputStream(srcPath);
System.out.println("读取图片的二进制数组...");
byte[] bytesArray = FileUtils.getBytesArray(fileInputStream);
System.out.println("字节数组长度为:" + bytesArray.length);
System.out.println("图片信息读取完毕...,向服务器传输图片...");
OutputStream outputStream = socket.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
bos.write(bytesArray);
socket.shutdownOutput();
System.out.println("图片传输完成...");
InputStream inputStream = socket.getInputStream();
System.out.println("等待服务器回复...");
String info = FileUtils.getStringInfo(inputStream);
System.out.println(info);
System.out.println("完成,退出程序...");
inputStream.close();
fileInputStream.close();
bos.close();
socket.close();
}
}
public class TCPFileServer02 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务端正在8888端口监听...");
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
System.out.println("连接上了... 开始获取客户端输入...");
byte[] res = FileUtils.getInfo(inputStream);
System.out.println("字节数组长度为:" + res.length);
System.out.println("客户端输入读取完毕...");
OutputStream outputStream = accept.getOutputStream();
String dstPath = "e:/background-pic/server.jpg";
System.out.println("开始保存图片文件...");
FileUtils.writeJpgToFile(res, dstPath);
System.out.println("保存完成...");
System.out.println("向客户端传输保存完成的消息...");
FileUtils.transportInfo("I get it", outputStream);
accept.shutdownOutput();
System.out.println("整个过程任务结束...退出程序...");
outputStream.close();
inputStream.close();
accept.close();
serverSocket.close();
}
}
public class FileUtils {
public static byte[] getBytesArray(InputStream inputStream) throws IOException {
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte[] bt = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int readLen = 0;
while((readLen = bufferedInputStream.read(bt)) != -1) {
bos.write(bt, 0 , readLen);
}
byte[] array = bos.toByteArray();
bufferedInputStream.close();
bos.close();
return array;
}
public static byte[] getInfo(InputStream inputStream) throws IOException {
int readLen = 0;
byte[] bt = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((readLen = inputStream.read(bt)) != -1) {
bos.write(bt, 0, readLen);
}
byte[] array = bos.toByteArray();
bos.close();
return array;
}
public static String getStringInfo(InputStream inputStream) throws IOException {
int readLen = 0;
byte[] bt = new byte[1024];
String res = "";
while((readLen = inputStream.read(bt)) != -1) {
res += new String(bt, 0, readLen);
}
return res;
}
public static void writeJpgToFile(byte[] res, String dstPath) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(dstPath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bos.write(res);
bos.flush();
bos.close();
}
public static void transportInfo(String info, OutputStream os) throws IOException {
os.write(info.getBytes());
}
}
客户端从服务器下载文件
public class ClientSocket {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
System.out.println("请输入你要下载的文件名:");
Scanner scanner = new Scanner(System.in);
String fileName = scanner.next();
System.out.println("向服务器传输要下载的文件名...");
outputStream.write(fileName.getBytes());
socket.shutdownOutput();
System.out.println("已告知服务器要下载的文件名...");
System.out.println("等待下载文件中...");
byte[] bytesArray = FileUtils.getBytesArray(inputStream);
System.out.println("已经下载到了文件...开始保存到本地");
String dstPath = "E:\\background-pic\\res\\123.jpg";
FileUtils.writeJpgToFile(bytesArray, dstPath);
System.out.println("保存完成,请在对应的目录下查看,即将退出...");
scanner.close();
inputStream.close();
outputStream.close();
socket.close();
}
}
public class ServerSocket0 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("8888端口等待客户端连接...");
Socket socket = serverSocket.accept();
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
System.out.println("已连接...正在从客户端获取需要下载的文件名...");
byte[] info = FileUtils.getInfo(inputStream);
String fileName = new String(info);
System.out.println("需要下载的文件名为:" + fileName);
File file = new File(fileName);
FileInputStream fileInputStream = null;
if (file.exists()) {
fileInputStream = new FileInputStream(fileName);
System.out.println("需要下载的文件存在...");
}
else {
fileInputStream = new FileInputStream("e:/background-pic/beau.jpg");
System.out.println("需要下载的文件不存在...即将为您下载默认的jpg文件");
}
System.out.println("开始读取数据库中的文件...");
byte[] bytesArray = FileUtils.getBytesArray(fileInputStream);
System.out.println("读取完毕,开始传输给客户端...");
outputStream.write(bytesArray);
socket.shutdownOutput();
System.out.println("传输完毕,服务器即将关闭...");
inputStream.close();
outputStream.close();
socket.close();
serverSocket.close();
}
}
结束~~撒花,常复习~
|