如何将字符串从Netty服务器发送到套接字客户端

时间:2015-07-01 11:19:00

标签: java sockets netty

我有一个简单的网络套接字服务器和带有标准套接字实现的简单客户端(没有网络)。从客户端到服务器我可以通过PrintWriter toServer = new PrintWriter(client.getOutputStream(), true);发送字符串 在客户端我有BufferedReader fromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));。在服务器端我有这个代码

@Override
protected void channelRead0(ChannelHandlerContext ctx, String message) throws Exception {
  String msg = message;
  String response = "Response from server";
  try {
   System.out.println(msg);
   System.out.flush();
   ctx.writeAndFlush(response);
  } finally {
      ReferenceCountUtil.retain(msg);
  }
}

但我无法从服务器读取客户端的任何字符串。我怎么能以正确的方式从netty发送字符串到客户端? 客户方:

toServer = new PrintWriter(client.getOutputStream(), true);
fromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));

for (int i = 0; i < 6; i++) {
    toServer.println("simple string: " + i);
    if ((line = fromServer.readLine()) != null) {
        System.out.println("Responce from server: " + line);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

$ourteam_category_check = '191';

    $niche_ourteam_args = array(
            'post_type' => 'ourteam',
           // 'cat'   => $ourteam_category_check,                 
            'orderby' => 'post_date',               
            'order' => 'DESC',
            'post_status' => 'publish',
            'meta_query' => array(
                array(
                    'key' => '_thumbnail_id',
                    'compare' => 'EXISTS'
                ),
            ),
       'tax_query' => array(
            array(
                'taxonomy' => 'member-category',
                'field' => 'term_id',
                'terms'    => $ourteam_category_check
            ),
       ),
   );          

        $niche_ourteam = new WP_Query($niche_ourteam_args);

        while ($niche_ourteam->have_posts()) : $niche_ourteam->the_post();

            /*** My loop code ***/

        endwhile;

Letme知道是否有效。