宋体客户端客户端通过tcp和服务端进行交互

文章结束给大家来个程序员笑话:[M]

    

    TCPTransmission Control Protocol 传输控制议协TCP是一种面向连接(连接导向)的、牢靠的、基于字节流的运输层(Transport layer)信通议协,由IETFRFC 793明说(specified)。在简化的计算机网络OSI型模中,它成完第四层传输层所指定的功能,UDP是一同层内另一个主要的传输议协。

    本文模拟客户端通过TCP向服务端发起登录请求来实现这一功能。

    一、服务端

    package com.figo.helloworld.action;

     

    import java.io.BufferedReader;

    import java.io.BufferedWriter;

    import java.io.InputStreamReader;

    import java.io.OutputStreamWriter;

    import java.io.PrintWriter;

    import java.net.ServerSocket;

    import java.net.Socket;

     

    /**

     * 服务端Tcp登录接口,收接客户端登录请求

     *

     * @author zhuzhifei 版权所有

     *

     */

    public class TcpAction implements Runnable {

     

        /**

         * @param args

         */

        public static void main(String[] args) {

           // TODO Auto-generated method stub

           System.out.println("start");

           Thread desktopServerThread = new Thread(new TcpAction());

           desktopServerThread.start();

        }

     

        public void run() {

           try {

               // 创立ServerSocket

               ServerSocket serverSocket = new ServerSocket(8088);// 设置8088端口

               while (true) {

                  // 接受客户端请求

                  Socket client = serverSocket.accept();

                  System.out.println("accept");

                  try {

                      // 收接客户端息消

                      BufferedReader in = new BufferedReader(

                             new InputStreamReader(client.getInputStream()));

                      String str = in.readLine();

                      System.out.println("read:" + str);

                      // 向服务器发送息消

                      PrintWriter out = new PrintWriter(new BufferedWriter(

                             new OutputStreamWriter(client.getOutputStream())),

                             true);

    每日一道理
坚持的昨天叫立足,坚持的今天叫进取,坚持的明天叫成功。

                      if ("123|abc".equals(str)) {

                         out.println("server message:" + "login success");

                      } else {

                         out.println("server message:"

                                + "userName or password is error");

                      }

                      // 关闭流

                      out.close();

                      in.close();

                  } catch (Exception e) {

                      System.out.println(e.getMessage());

                      e.printStackTrace();

                  } finally {

                      // 关闭

                      client.close();

                      System.out.println("close");

                  }

               }

           }

     

           catch (Exception e) {

               System.out.println(e.getMessage());

           }

        }

    }

    二、客户端

     

    package com.figo.helloworld;

     

    import java.io.BufferedReader;

    import java.io.BufferedWriter;

    import java.io.InputStreamReader;

    import java.io.OutputStreamWriter;

    import java.io.PrintWriter;

    import java.net.InetSocketAddress;

    import java.net.Socket;

    import java.net.SocketTimeoutException;

     

    import org.apache.http.conn.ConnectTimeoutException;

     

    import android.app.Activity;

    import android.os.Bundle;

    import android.util.Log;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.widget.Button;

    import android.widget.EditText;

    import android.widget.TextView;

     

    /**

     * 这个Activity模拟用使Socket信通来调用服务端的登录接口

     *

     * @author zhuzhifei 版权所有

     *

     */

    public class TcpActivity extends Activity {

     

             Button buttonLogin;

             EditText etUserName;

             EditText etPwd;

             TextView tvResult;

     

             @Override

             protected void onCreate(Bundle savedInstanceState) {

                       super.onCreate(savedInstanceState);

                       setContentView(R.layout.tcptest);

                       buttonLogin = (Button) findViewById(R.id.buttonLogin);

                       etUserName = (EditText) findViewById(R.id.etUserName);

                       etPwd = (EditText) findViewById(R.id.etPwd);

                       tvResult = (TextView) findViewById(R.id.tvResult);

                       buttonLogin.setOnClickListener(new OnClickListener() {

     

                                @Override

                                public void onClick(View v) {

                                         Socket socket = null;

                                         String message = etUserName.getText().toString() + "|"

                                                            + etPwd.getText().toString();

                                         try {

                                                   // 创立Socket

                                                   socket = new Socket();

                                                   socket.connect(new InetSocketAddress("10.241.72.35", 8088),

                                                                     30000);// 30秒钟连接时超

                                                   socket.setSoTimeout(30000);// 30秒钟信通时超

                                                   socket.setTcpNoDelay(true);

                                                   // 向服务器发送息消

                                                   PrintWriter out = new PrintWriter(new BufferedWriter(

                                                                     new OutputStreamWriter(socket.getOutputStream())),

                                                                     true);

                                                   out.println(message);

                                                   // 收接来自服务器的息消

                                                   BufferedReader br = new BufferedReader(

                                                                     new InputStreamReader(socket.getInputStream()));

                                                   String msg = br.readLine();

                                                   if (msg != null) {

                                                            tvResult.setText(msg);

                                                   } else {

                                                            tvResult.setText("据数错误!");

                                                   }

                                                   // 关闭流

                                                   out.close();

                                                   br.close();

                                                   // 关闭Socket

                                                   socket.close();

                                         } catch (ConnectTimeoutException e) {

                                                   Log.e("tcp", e.toString());

                                         } catch (SocketTimeoutException e) {

                                                   Log.e("tcp", e.toString());

                                         } catch (Exception e) {

                                                   Log.e("tcp", e.toString());

                                         }

                                }

                       });

             }

    }

文章结束给大家分享下程序员的一些笑话语录: 看到有人回帖“不顶不是中国人”,他的本意是想让帖子沉了。

你可能感兴趣的:(tcp)