socket java

《Java Network Programming》学习笔记之Socket篇:

Socket概念:

Sockets shield the programmer from low-level details of the network, such as error detection, packet sizes, packet retransmission, network addresses, and more.

Socket的一个重大作用是它帮我们屏蔽(搞定)了下层诸如错误检测,包的长度,包的重传,网络地址,(三次/四次)握手等诸多细节。


A socket is a connection between two hosts. It can perform seven basic operations:
• Connect to a remote machine
• Send data
• Receive data
• Close a connection
• Bind to a port
• Listen for incoming data
• Accept connections from remote machines on the bound port

Socket的几大操作: ####
  1. 与远程机器建立连接
  2. 发送数据
  3. 接收数据
  4. 关闭连接
  5. 绑定端口号
  6. 监听数据管道
  7. 从绑定的端口号获悉及允许来自远程机器的连接请求

其中前四个为客户端socket和服务器端socket共有的,后三个为服务器端socket专有。


Java程序中的socket:

Java programs normally use client sockets in the following fashion:
1. The program creates a new socket with a constructor.
2. The socket attempts to connect to the remote host.
3. Once the connection is established, the local and remote hosts get input and output streams from the socket and use those streams to send data to each other.This connection is full-duplex; both hosts can send and receive data simultaneously. What the data means depends on the protocol; different commands are sent to an FTP server than to an HTTP server. There will normally be some
agreed-upon hand-shaking followed by the transmission of data from one to the other.
4. When the transmission of data is complete, one or both sides close the connection. Some protocols, such as HTTP 1.0, require the connection to be closed after each request is serviced. Others, such as FTP, allow multiple requests to be processed in a single connection.

  1. 通过构造函数创建新的socket
  2. socket应该去连接远程主机
  3. 一旦连接被建立,本地和远程主机通过输入输出流进行数据的发送和接收。并且这个连接时全双工的–任意一方都可以同时进行发送和接收
  4. 数据传输完成后,某一方或者双方关闭该socket连接

socket通信路径

另外:附几个讲解Socket的博客链接:
http://www.cnblogs.com/dolphinX/p/3460545.html
http://goodcandle.cnblogs.com/archive/2005/12/10/294652.aspx
http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值