码迷,mamicode.com
首页 > 其他好文 > 详细

TCP通信小实例

时间:2014-10-24 16:08:08      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   sp   div   

服务端:

package com.socket.tcp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 基于TCP协议通信,服务端
 */
public class Server
{
    public static void main(String[] args)
    {
        ServerSocket serverSocket = null;
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try
        {
            serverSocket = new ServerSocket(10000);
            socket = serverSocket.accept();
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream());
            while (true)
            {
                String str = in.readLine();
                System.out.println("来自客户端的:" + str);
                out.println("服务端收到了来自客户端的:" + str);
                out.flush();
                if ("end".equals(str))
                {
                    break;
                }
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                in.close();
                out.close();
                socket.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

}

客户端:

package com.socket.tcp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client
{
    /**
     * 基于TCP协议通信,客户端
     */
    public static void main(String[] args)
    {

        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        BufferedReader wt = null;
        try
        {
            socket = new Socket("127.0.0.1", 10000);//InetAddress.getLocalHost()
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//收到的内容
            wt = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(socket.getOutputStream());//获取输出流,发送的
            while (true)
            {
                String str = wt.readLine();
                out.println(str);
                out.flush();
                if ("end".equals(str))
                {
                    break;
                }
                System.out.println(in.readLine());//收到的
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                in.close();
                out.close();
                socket.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

}

 

TCP通信小实例

标签:style   blog   color   io   os   ar   java   sp   div   

原文地址:http://www.cnblogs.com/chrono/p/4048226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!