客户端
package socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/*
## DataInputStream 常用方法
read(byte b[])---从数据输入流读取数据存储到字节数组b中.
read(byte b[],int off,in len)---从数据输入流中读取数据存储到数组b里面,位置从off开始,长度为len个字节.
readFully(byte b[])---从数据输入流中循环读取b.length个字节到数组b中.
readFully(byte b[],int off,in len )---从数据输入流中循环读取len个字节到字节数组b中.从b的off位置开始
skipBytes(int b)---跳过n个字节.
readBoolean()---从数据输入流读取布尔类型的值.
readByte()---从数据输入流中读取一个字节.
readUnsignedByte()---从数据输入流中读取一个无符号的字节,返回值转换成int类型.
readShort()---从数据输入流读取一个short类型数据.
readUnsignedShort()---从数据输入流读取一个无符号的short类型数据.
readChar()---从数据输入流中读取一个字符数据
readInt()---从数据输入流中读取一个int类型数据.
readLong()---从数据输入流中读取一个long类型的数据.
readFloat()---从数据输入流中读取一个float类型的数据.
readDouble()---从数据输入流中读取一个double类型的数据.
readUTF()---从数据输入流中读取用UTF-8格式编码的UniCode字符格式的字符串.
=============================================================================================
## DataInputStream 常用方法
intCount(int value)---数据输出流增加的字节数.
write(int b)---将int类型的b写到数据输出流中.
write(byte b[],int off, int len)---将字节数组b中off位置开始,len个长度写到数据输出流中.
flush()---刷新数据输出流.
writeBoolean()---将布尔类型的数据写到数据输出流中,底层是转化成一个字节写到基础输出流中.
writeByte(int v)---将一个字节写到数据输出流中(实际是基础输出流).
writeShort(int v)---将一个short类型的数据写到数据输出流中,底层将v转换2个字节写到基础输出流中.
writeChar(int v)---将一个charl类型的数据写到数据输出流中,底层是将v转换成2个字节写到基础输出流中.
writeInt(int v)---将一个int类型的数据写到数据输出流中,底层将4个字节写到基础输出流中.
writeLong(long v)---将一个long类型的数据写到数据输出流中,底层将8个字节写到基础输出流中.
writeFloat(flloat v)---将一个float类型的数据写到数据输出流中,底层会将float转换成int类型,写到基础输出流中.
writeDouble(double v)---将一个double类型的数据写到数据输出流中,底层会将double转换成long类型,写到基础输出流中.
writeBytes(String s)---将字符串按照字节顺序写到基础输出流中.
writeChars(String s)---将字符串按照字符顺序写到基础输出流中.
writeUTF(String str)---以机器无关的方式使用utf-8编码方式将字符串写到基础输出流中.
size()---写到数据输出流中的字节数.
————————————————
注释链接:https://blog.csdn.net/lili13897741554/article/details/81876443
*/
public class Client {
public static Socket clientSocket = null;
//初始化
public static void init() {
try {
clientSocket = new Socket("127.0.0.1",2019); //服务器IP地址及端口号
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
init();
try {
String[] mess = {"在吗","我喜欢你","好吧"};
//用 DataOutputStream、DataInputStream做接收和发送
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
for (int i = 0; i < mess.length; i++) {
out.writeUTF(mess[i]);
System.out.println("客户发出服务器的提问:" + mess[i]);
String s = in.readUTF();//读取信息, 线程阻塞
System.out.println("客户收到服务器的回答:" + s);
Thread.sleep(500);
}
}catch (Exception e){
System.out.println("服务器已断开");
}
}
}
服务器
package socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static ServerSocket serverSocket = null;
public static Socket sc = null;
public static void init() {
try {
serverSocket = new ServerSocket(2019);//设置服务器端口号
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String[] answer = {"嗯嗯","我不喜欢你","再见"};
System.out.println("等待客户呼叫");
//初始化
init();
try {
//建立连接
sc = serverSocket.accept();//线程进入阻塞状态,除非有客户呼叫
//用 DataOutputStream、DataInputStream做接收和发送
DataOutputStream out = new DataOutputStream(sc.getOutputStream());
DataInputStream in = new DataInputStream(sc.getInputStream());
for (int i = 0; i < answer.length; i++) {
String s = in.readUTF(); //in读取信息,阻塞状态
System.out.println("服务器收到客户的提问:" + s);
out.writeUTF(answer[i]);
System.out.println("服务器回复客户的提问:" + answer[i]);
Thread.sleep(500);
}
} catch (Exception e){
System.out.println("客户端已断开");
}
}
}