1. 基本通信框架
数据报文长度不受限制。
- Client端
System.out.println("-----Client-----");
//1、建立连接: 使用Socket创建客户端 + 服务的地址和端口
Socket client =new Socket("localhost",8888); //目标地址及端口
//2、操作: 输入输出流操作
DataOutputStream dos =new DataOutputStream(client.getOutputStream());
String data ="hello"; //无需\r\n,自动加上
dos.writeUTF(data);
dos.flush();
//3、释放资源
dos.close();
client.close();
- Server端
System.out.println("-----Server-----");
// 1、指定端口 使用ServerSocket创建服务器
ServerSocket server =new ServerSocket(8888); //指定服务器侦听的端口
// 2、阻塞式等待连接 accept
Socket client =server.accept(); //客户端一连上就取消阻塞
System.out.println("一个客户端建立了连接");
// 3、操作: 输入输出流操作
DataInputStream dis =new DataInputStream(client.getInputStream());
String data =dis.readUTF();
System.out.println(data);
// 4、释放资源
dis.close();
client.close();
server.close();
2. 双向通信
TCP是请求响应式:有request就有response
- 客户端
public class LoginTwoWayClient {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("-----Client-----");
BufferedReader console =new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入用户名:");
String uname =console.readLine();
System.out.print("请输入密码:");
String upwd =console.readLine();
//1、建立连接: 使用Socket创建客户端 +服务的地址和端口
Socket client =new Socket("localhost",8888);
//2、操作: 输入输出流操作
DataOutputStream dos =new DataOutputStream(client.getOutputStream());
dos.writeUTF("uname="+uname+"&"+"upwd="+upwd);
dos.flush();
DataInputStream dis =new DataInputStream(client.getInputStream());
String result =dis.readUTF();
System.out.println(result);
//3、释放资源
dos.close();
client.close();
}
}
- 服务器端
public class LoginTwoWayServer {
public static void main(String[] args) throws IOException {
System.out.println("-----Server-----");
// 1、指定端口 使用ServerSocket创建服务器
ServerSocket server =new ServerSocket(8888);
// 2、阻塞式等待连接 accept
Socket client =server.accept(); //客户端一连上就取消阻塞
System.out.println("一个客户端建立了连接");
// 3、操作: 输入输出流操作
DataInputStream dis =new DataInputStream(client.getInputStream());
String datas =dis.readUTF();
String uname ="";
String upwd ="";
//分析
String[] dataArray = datas.split("&");
for(String info:dataArray) {
String[] userInfo =info.split("=");
if(userInfo[0].equals("uname")) {
System.out.println("你的用户名为:"+userInfo[1]);
uname = userInfo[1];
}else if(userInfo[0].equals("upwd")) {
System.out.println("你的密码为:"+userInfo[1]);
upwd = userInfo[1];
}
}
//输出
DataOutputStream dos =new DataOutputStream(client.getOutputStream());
if(uname.equals("bee") && upwd.equals("123456")) { //成功
dos.writeUTF("登录成功,欢迎回来");
}else { //失败
dos.writeUTF("用户名或密码错误");
}
dos.flush();
// 4、释放资源
dis.close();
client.close();
server.close();
}
}
3. 文件传输(对文件大小没有限制)
- 客户端上传文件
System.out.println("-----Client-----");
//1、建立连接: 使用Socket创建客户端 +服务的地址和端口
Socket client =new Socket("localhost",8888); //目标地址及目标端口
//2、操作: 拷贝 上传
InputStream is =new BufferedInputStream(new FileInputStream("src/foo.png"));
OutputStream os =new BufferedOutputStream(client.getOutputStream());
byte[] flush =new byte[1024];
int len = -1;
while((len=is.read(flush))!=-1) {
os.write(flush,0,len);
}
os.flush();
//3、释放资源
os.close();
is.close();
client.close();
- 服务端接收文件
System.out.println("-----Server-----");
// 1、指定端口 使用ServerSocket创建服务器
ServerSocket server =new ServerSocket(8888);
// 2、阻塞式等待连接 accept
Socket client =server.accept(); //客户端一连上就取消阻塞
System.out.println("一个客户端建立了连接");
// 3、操作: 文件拷贝 存储
InputStream is =new BufferedInputStream(client.getInputStream());
OutputStream os =new BufferedOutputStream(new FileOutputStream("src/oof.png"));
byte[] flush =new byte[1024];
int len = -1;
while((len=is.read(flush))!=-1) {
os.write(flush,0,len);
}
os.flush();
//3、释放资源
os.close();
is.close();
// 4、释放资源
client.close();
server.close();
4. TCP多重连接
引入多线程
- 服务器端
public class LoginMultiServer {
public static void main(String[] args) throws IOException {
System.out.println("-----Server-----");
// 1、指定端口 使用ServerSocket创建服务器
ServerSocket server =new ServerSocket(8888);
boolean isRunning =true;
// 2、阻塞式等待连接 accept
while(isRunning) {
Socket client =server.accept(); //客户端一连上就取消阻塞
System.out.println("一个客户端建立了连接");
new Thread(new Channel(client)).start();
}
server.close();
}
//一个channel就代表一个客户端
static class Channel implements Runnable{
private Socket client;
//输入流
private DataInputStream dis;
//输出流
private DataOutputStream dos;
public Channel(Socket client) {
this.client = client;
try {
//输入
dis = new DataInputStream(client.getInputStream());
//输出
dos =new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
release();
}
}
//接收数据
private String receive() {
String datas ="";
try {
datas = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
return datas;
}
//释放资源
private void release() {
// 4、释放资源
try {
if(null != dos) {
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(null != dis) {
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(null != client) {
client.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//发送数据
private void send(String msg) {
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
// 3、操作: 输入输出流操作
String uname ="";
String upwd ="";
//分析
String[] dataArray = receive().split("&");
for(String info:dataArray) {
String[] userInfo =info.split("=");
if(userInfo[0].equals("uname")) {
System.out.println("你的用户名为:"+userInfo[1]);
uname = userInfo[1];
}else if(userInfo[0].equals("upwd")) {
System.out.println("你的密码为:"+userInfo[1]);
upwd = userInfo[1];
}
}
if(uname.equals("bee") && upwd.equals("123456")) { //成功
send("登录成功,欢迎回来");
}else { //失败
send("用户名或密码错误");
}
release();
}
}
}
- 客户端
public class LoginMultiClient {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("-----Client-----");
//1、建立连接: 使用Socket创建客户端 +服务的地址和端口
Socket client =new Socket("localhost",8888);
//2、操作: 输入输出流操作 先请求后响应
new Send(client).send();
new Receive(client).receive();
client.close();
}
//发送
static class Send{
private Socket client;
private DataOutputStream dos;
private BufferedReader console ;
private String msg;
public Send(Socket client) {
console=new BufferedReader(new InputStreamReader(System.in));
this.msg =init();
this.client = client;
try {
dos=new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
private String init() {
try {
System.out.print("请输入用户名:");
String uname =console.readLine();
System.out.print("请输入密码:");
String upwd =console.readLine();
return "uname="+uname+"&"+"upwd="+upwd;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public void send() {
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//接收
static class Receive{
private Socket client;
private DataInputStream dis;
public Receive(Socket client) {
this.client = client;
try {
dis=new DataInputStream(client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void receive() {
String result;
try {
result = dis.readUTF();
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}