ClientApp.java
上传用户:caijun
上传日期:2022-08-05
资源大小:13k
文件大小:1k
- package ServerClientCommunication;
- import java.net.*;
- import java.io.*;
- import java.lang.*;
- public class ClientApp {
- @SuppressWarnings("deprecation")
- public static void main(String args[]) {
- try {
- // 创建通讯并且和主机Rock连接 create socket and connect with host
- Socket cSocket = new Socket("166.104.144.161", 8821); // 166.104.30.120
- // 5555
- // 打开这个Socket的输入/输出流 open input/output stream
- OutputStream os = cSocket.getOutputStream();
- DataInputStream is = new DataInputStream(cSocket.getInputStream());
- int c;
- boolean flag = true;
- String responseline;
- while (flag) {
- // 从标准输入输出接受字符并且写如系统 read from input
- while ((c = System.in.read()) != -1) {
- os.write((byte) c);
- if (c == 'n') {
- os.flush();
- // 将程式阻塞,直到回答信息被收到后将他们在标准输出上显示出来 block the thread and display the feedback message
- responseline = is.readLine();
- System.out.println("Message is:" + responseline);
- }
- }
- }
- os.close();
- is.close();
- cSocket.close();
- } catch (Exception e) {
- System.out.println("Exception :" + e.getMessage());
- }
- }
- }