Java网络编程TCP


简单的TCP流程
如图:
TCP.PNG (22.97 KB)
2014-6-27 20:52


案例:
服务器端

01public class SimpleTcpServer {
02     publicstatic void main(String[] args) {
03        try{
04            //套接字服务器,相当于中心号码
05            ServerSocket ss =new ServerSocket(8001);
06            //等待被呼叫,建立一个Socket
07            Socket s = ss.accept();
08            //从建立的Socket中建立字节输入输出流,读取或发送数据
09            InputStream ips = s.getInputStream();
10            OutputStream ops = s.getOutputStream();
11              //以字节的形式接收数据
12            byte[] buf =new byte[1024];
13            intlength = ips.read(buf);//听
14            System.out.println("服务器接收:"+new      String(buf,0,length));
15//以字节的形式发送一条固定语句(写字节)
16            String sendStr ="服务器发送的数据";
17            ops.write(sendStr.getBytes());
18       //关闭
19            ips.close();
20            ops.close();
21            s.close();
22            ss.close();        
23          }catch(Exception ex){
24              ex.printStackTrace();
25        }
26         
27    }
28}

客户端

01public class SimpleTcpClient {
02    publicstatic void main(String[] args) {
03        try{
04         //创建客户端套接字指定连接的IP地址和端口号
05            Socket s =new             Socket(InetAddress.getByName("127.0.0.1"),8001);
06            //创建输入输出流
07            InputStream ips = s.getInputStream();
08            OutputStream ops = s.getOutputStream();
09             //往套接字中写数据(字节)
10            String sendStr ="客户端发送的数据";
11            ops.write(sendStr.getBytes());
12           //接收数据并显示出来
13            byte[] buf =new byte[1024];
14            intlength = ips.read(buf);
15            System.out.println("客户端接收:"+newString(buf,0,length));
16    //关闭
17            ips.close();
18            ops.close();
19            s.close();
20        }catch(Exception ex){
21            ex.printStackTrace();
22        }
23             
24    }
25}



TCP变成总结
服务器端必须先启动
服务器指定了端口号后,客户端创建套接字时必须使用相同的端口号
客户端需知道服务器的IP地址
客户端连接到服务器后,通过输入输出流进行信息交流,实际是在Socket中进行读写数据


演示服务器的连接请求队列的特性。

01import java.net.*;
02public class Client {
03  publicstatic void main(String args[])throws Exception{
04    finalint length=100;
05    String host="localhost";
06    intport=8000;
07    Socket[] sockets=newSocket[length];
08    for(inti=0;i<length;i++){                                //试图建立100次连接
09      sockets[i]=newSocket(host, port);
10      System.out.println("第"+(i+1)+"次连接成功");
11    }
12    Thread.sleep(3000);
13    for(inti=0;i<length;i++){
14      sockets[i].close();                                       //断开连接
15    }
16  }
17}




01import java.io.*;
02import java.net.*;
03public class Server {
04  privateint port=8000;
05  privateServerSocket serverSocket;
06  publicServer() throwsIOException {
07    serverSocket =new ServerSocket(port,3);                     //连接请求队列的长度为3
08    System.out.println("服务器启动");
09  }
10  publicvoid service() {
11    while(true) {
12      Socket socket=null;
13      try{
14        socket = serverSocket.accept();                              //从连接请求队列中取出一个连接
15        System.out.println("New connection accepted "+
16        socket.getInetAddress() +":" +socket.getPort());
17      }catch(IOException e) {
18         e.printStackTrace();
19      }finally{
20         try{
21           if(socket!=null)socket.close();
22         }catch(IOException e) {e.printStackTrace();}
23      }
24    }
25  }
26  publicstatic void main(String args[])throws Exception {
27    Server server=newServer();
28    Thread.sleep(60000*10);                                             //睡眠10分钟
29    //server.service();
30  }
31}
-                                                                    ------  android培训 java培训 IOS培训 .Net培训 期待与您交流! ----------



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值