1.TCP通信:
TCP类似于打电话,需要服务器端开启才能进行信息传输
客户端/发送端:
package TCP;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
public class Client {
public static void main(String[] args) {
//1.获取服务器地址和端口号
InetAddress serverIp = null;
int port =0;
Socket socket =null;
OutputStream out = null;
try {
serverIp = InetAddress.getByName("127.0.0.1");
port=9999;
//2.创建输出对象
socket = new Socket(serverIp,port);
//3.获取输出流
out = socket.getOutputStream();
//4.输出内容
out.write("你好呀".getBytes(StandardCharsets.UTF_8));//通过字节传输
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器端/接收端:
package TCP;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream in = null;
ByteArrayOutputStream baos = null;//管道输出流(有缓冲的作用)
try {
//1.创建一个端口
serverSocket = new ServerSocket(9999);
//2.等待用户连接
socket = serverSocket.accept();
//3.读取客户端消息
in = socket.getInputStream();
//使用管道流接收
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len ;
while((len = in.read(buffer))!=-1) {
baos.write(buffer,0,len);
}
System.out.println(baos.toString("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:服务器端和客户端是相对而言的,服务器端也可以是发送端
服务器端接收到信息并处理后就停止了,可以通过while+信号灯的方式一直开启,不过serverSocket.accept();方法会将开启的端口进行堵塞连接
2.TPC实现文件传输:
发送端:
package FileTransfer;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class Send {
public static void main(String[] args) {
Socket socket = null;
OutputStream out = null;
FileInputStream fin = null;
ByteArrayOutputStream baos = null;
InputStream in = null;
try {
//获取连接
socket = new Socket(InetAddress.getByName("127.0.0.1"),9999);
out = socket.getOutputStream();
//获取文件
fin = new FileInputStream("src/en.jpg");//文件路径
byte[] buffer = new byte[1024];
int len =0;
//写文件
while((len = fin.read(buffer))!=-1){
out.write(buffer,0,len);
}
//通知服务器我已经传输完了
socket.shutdownOutput();
//获取服务器的回复,确保服务器接收完成
in = socket.getInputStream();
//使用管道流
baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2 =0;
while((len2 = in.read(buffer2))!= -1){
baos.write(buffer2);
}
System.out.println(baos.toString("utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
finally{
//关闭连接
if (baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fin!=null){
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
接收端:
package FileTransfer;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class Receive {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream in = null;
FileOutputStream fos = null;
OutputStream out = null;
try {
//创建服务
serverSocket = new ServerSocket(9999);
//创建监听客户端的连接,接收消息
socket = serverSocket.accept();
//获取输入流
in = socket.getInputStream();
//文件传输
fos = new FileOutputStream("shoudaode.jpg");
byte[] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)) != -1){
fos.write(buffer,0,len);
}
//告诉客户端我接收完毕
out = socket.getOutputStream();
out.write("我已经接收完成了,你可以关闭了".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}finally{//关闭连接
if (out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}