package test_teacher;

import java.io.*;
import java.net.*;

public class SingleTalkClient
{
    public static void main(String[] args) throws IOException
    {

        Socket client = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            client = new Socket("127.0.0.1", 4444);
            out = new PrintWriter(client.getOutputStream(), true); //auto flush
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: 127.0.0.1.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: 127.0.0.1.");
            System.exit(1);
        }

        //从标准输入流(键盘)中获取信息
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        
    String fromServer, fromUser;
    boolean sbye = false;
    boolean ubye = false;

    System.out.print("Client input:");
    fromUser = stdIn.readLine();
        while( true ){
        if( ubye == false )
        {
            out.println(fromUser);
            out.flush();
            //System.out.println("Client: " + fromUser);
            if (fromUser.equals("Bye."))
                ubye = true;
        }

        if( sbye == false )
        {
            fromServer = in.readLine();
            System.out.println("from Server: " + fromServer);
            if (fromServer.equals("Bye."))
                sbye = true;
        }

        if( ubye == false )
        {
            System.out.print("Client input:");
            fromUser = stdIn.readLine();
        }

        if( ubye == true && sbye == true )
            break;
        }

        out.close();
        in.close();
        stdIn.close();
        client.close();
    }
}

QQ_SingleTalkClient的更多相关文章

随机推荐

  1. 【转】Nutch的Hadoop方式爬取效率优化

    原文地址:http://my.oschina.net/junfrank/blog/290404

  2. js本地预览图片

    废话不说  直接上代码 <script type="text/javascript" src="http://code.jquery.com/jquery-late ...

  3. 一个基于Behave框架的http接口测试实例

    前言:本人没怎么做过http接口测试,只是最近学习了一下,Behave框架也是最近学习的,如果有不对的请各位大神指点,感谢! 1.1       接口准备 本次get请求的接口用的是百度接口:wd=搜 ...

  4. 简单却又复杂的FizzBuzz面试编程问题

    写这篇文章主要是因为偶然看到一篇关于stackoverflow公司的面经中提到了一个有趣的面试编程问题,如题所述:FizzBuzz问题.原文引用如下: “在一些公平的考验之后,我发现那些因为代码而抓狂 ...

  5. Cocos2d-x shader学习2: 模糊(Blur)

    模糊效果在游戏中经常会用到,有的为了突出前景会把背景给模糊化,有的是因为一些技能需要模糊效果.模糊是shader中较为简单的一种应用.cocos2dx 3.x给的demo中,就有sprite的模糊的效 ...

  6. Android N安装apk报错:android.os.FileUriExposedException

    StackOverflow: http://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-s ...

  7. iOS程序进入后台,延迟指定时间退出

    程序进入后台,延迟指定时间退出 正常程序退出后,会在几秒内停止工作:要想申请更长的时间,需要用到beginBackgroundTaskWithExpirationHandlerendBackgroun ...

  8. virtualBox,webstorm,开虚拟机传代码

    一起git一个新技能 利用virtualBOX在本地开一个虚拟机,然后设置webstorm连接到虚拟机,将代码传到虚拟机里. 以下详细讲解: 第一步: 第二步:管理虚拟机的设置(我是用的是Xshell ...

  9. 用tp框架来对数据库进行增删改

    先来看添加 使用tp框架,对数据库进行添加操作,都有哪些方法 先在Main控制器中,做个方法 运行一下,注意地址,就要输tianjia了 然后再看一下数据库,有没有添加上数据 添加成功 再来看一下这个 ...

  10. Cannot use ImageField because Pillow is not installed.

    现象描述: 使用 ImageField ,创建数据库表时,提示如下: Cannot use ImageField because Pillow is not installed. HINT: Get ...