下面是客户端的启动代码
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
//获取配置文件
Properties pro = new Properties();
pro.load(new FileInputStream("freshbin/freshbinIP.properties"));
//通过配置文件中的ip与端口port连接server服务端
String ip = pro.getProperty("ip");
int port = Integer.parseInt(pro.getProperty("port"));
Socket s = new Socket(ip, port);
//创建一个客户端的读与写对象
ClientInputStream inputThread = new ClientInputStream(s);
ClientOutputStream outputThread = new ClientOutputStream(s);
//inputThread.setPriority(1);
//outputThread.setPriority(10);
//启动客户端读与写的线程
inputThread.start();
outputThread.start();
}
下面是客户端发送类中的线程run方法的代码
OutputStream os = null;
Scanner sc = null;
try {
os = this.socket.getOutputStream();
sc = new Scanner(System.in);
while (true) {
//发送请求信息(查询或者更新)
String message = sc.nextLine();
os.write(message.getBytes());
os.flush();
下面是客户端接收类的线程的run方法的代码
InputStream is = null;
try {
is = this.socket.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
String message = null;
while (-1 != (len = is.read(buffer))) {
//把从服务端接收到的信息打印在控制台
message = new String(buffer, 0, len);
String s = "服务器端:" + message;
System.out.println(s);
下面是服务端的启动代码
public static void main(String[] args) throws IOException {
//获取配置文件
Properties pro = new Properties();
pro.load(new FileInputStream("freshbin/freshbinIP.properties"));
//通过配置文件中的ip与端口port连接server服务端
int port = Integer.parseInt(pro.getProperty("port"));
ServerSocket ss = new ServerSocket(port);
//进行监听
Socket s = ss.accept();
//创建一个服务端的读与写对象
ServerInputStream inputThread = new ServerInputStream(s);
ServerOutputStream outputThread &#