编程经验:Socket编程Java(1)

TCP/IP四层网络模型:应用层、传输层、网络层和链路层。 

查看更多精彩图片

//**************************************Socket 半双工通信***************************************************

Server

import java.io.*;

import java.net.*;

 

/**

 *@authorAdministrator

 *

 */

//socket 服务端

publicclass MySocketServer {

 

         /**

          *@paramargs

          */

         publicstaticvoid main(String[] args)

         {

                   // TODO Auto-generated method stub

         try

         {

                   //创建 ServerSocket

                   ServerSocket ss=new ServerSocket(18888);

                   System.out.println("Listen port :18888 ...");

                   //监听端口(产生阻塞),直到某个客户端连接,一旦连接成功,就会得到Socket(管道)

                   //Socket 就就好像是一个管道

                   Socket s=ss.accept();

                  

                   //连接成功读取客户端发送过来的信息

                   InputStreamReader isr =new InputStreamReader(s.getInputStream());

                   //转换成Buffer,方便读取

                   BufferedReader bs =new BufferedReader(isr);

                   while(true)

                   {

                            String clientRequest=bs.readLine();//逐行读取消息

                            System.out.println("Client Send Data:"+clientRequest);

                            //---------------服务器响应---------------------------

                            OutputStreamWriter osr =new OutputStreamWriter(s.getOutputStream());

                            BufferedWriter bw=new BufferedWriter(osr);

                            PrintWriter pw =new PrintWriter(bw,true);

                            if(!clientRequest.equals("End"))

                            {

                                     pw.println("Server Receive Data:"+clientRequest);

                            }

                            else

                            {

                                     pw.println("Good bye!");

                                     s.close();

                                     break;

                            }

                   }

         }

         catch(Exception e)

         {

             e.printStackTrace();

         }

       }

}

Server 运行结果:

root@bt:~# javac MySocketServer.java

root@bt:~# java MySocketServer

Listen port :18888 ...

Client Send Data:1111111111111

Client Send Data:2222222222222

Client Send Data:AAAAAAAAAAAAA

Client Send Data:DKDKDKDKDKKDKSSSSS

Client Send Data:End

root@bt:~#                                              

//***********************************************************************************************

Client

import java.io.*;

import java.net.*;

import java.util.Scanner;

 

publicclass MySocketClient {

 

         /**

          *@paramargs

          */

         publicstaticvoid main(String[] args)

         {

                   // TODO Auto-generated method stub

       try

       {

             //通过IP和端口主动接服务器,连接成功产生一条管道

         Socket s=new Socket("192.168.1.187",18888);

             System.out.println("Connet ...");

             //连接成功发送消息

              OutputStreamWriter  osw= new OutputStreamWriter(s.getOutputStream());

              //转换成Buffer

             BufferedWriter bw= new BufferedWriter(osw);

             //创建自动刷新的新PrintWriter

             PrintWriter pw =new PrintWriter(bw,true);

             System.out.println("Connet Success!");

             //扫描控制台输入消息

             Scanner sc=new Scanner(System.in); 

             String serverReponse="";

              while(!serverReponse.equals("Good bye!"))

              {

                     String consolveStr=sc.next(); //读取控制台信息

                  pw.println(consolveStr);//发送信息

                        //-------------------服务响应-------------

                       InputStream iStream=s.getInputStream();

                        InputStreamReader isr =new InputStreamReader(iStream);

                        BufferedReader br=new BufferedReader(isr);

                        //接收消息

                             serverReponse= br.readLine();

                             System.out.println("Server Send data :" +serverReponse);

                       if(consolveStr.equals("End"))

                    {

                             s.close();

                         break;

                    }

                 }

       }

       catch(Exception e)

       {

        e.printStackTrace();       }

           }

}

Client 运行结果:

Connet ...

Connet Success!

1111111111111

Server Send data :Server Receive Data:1111111111111

2222222222222

Server Send data :Server Receive Data:2222222222222

AAAAAAAAAAAAA

Server Send data :Server Receive Data:AAAAAAAAAAAAA

DKDKDKDKDKKDKSSSSS

Server Send data :Server Receive Data:DKDKDKDKDKKDKSSSSS

End

Server Send data :Good bye!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值