java套接字编程

java socket programing

本博客转自网络,我做了一定的整理,便于以后的复习

Datagram communication:

The datagram communication protocol, known as UDP (user datagram protocol), is a connectionless protocol, meaning that each time you send datagrams, you also need to send the local socket descriptor and the receiving socket’s address. As you can tell, additional data must be sent each time a communication is made.
Key word : datagram(资料包), UDP(unreliable, packet-switched, packet data, no connection overhead, application-level protocols exchange information immediately, two-way communication)

Stream communication

The stream communication protocol is known as TCP (transfer control protocol). Unlike UDP, TCP is a connection-oriented protocol. In order to do communication over the TCP protocol, a connection must first be established between the pair of sockets. While one of the sockets listens for a connection request (server), the other asks for a connection (client). Once two sockets have been connected, they can be used to transmit data in both (or either one of the) directions.
Key word : connection-oriented, TCP(reliable, connection-oriented, byte-stream, connection established before application-level protocols exchange information, two-way communication.)

TCP or UDP?

  • UDP:

    Every time you send a datagram, you have to send the local descriptor and the socket address of the receiving socket along with it.In UDP, there is a size limit of 64 kilobytes on datagrams you can send to a specified location.UDP is an unreliable protocol – there is no guarantee that the datagrams you have sent will be received in the same order by the receiving socket. UDP is less complex and incurs fewer overheads. It is often used in implementing client/server applications in distributed systems built over local area networks.

  • TCP:

    TCP is a connection-oriented protocol, on the other hand, a connection must be established before communications between the pair of sockets start. So there is a connection setup time in TCP. Once a connection is established, the pair of sockets behaves like streams: All available data are read immediately in the same order in which they are received.TCP is a reliable protocol; it is guaranteed that the packets you send will be received in the order in which they were sent.TCP is useful for implementing network services – such as remote login (rlogin, telnet) and file transfer (FTP) – which require data of indefinite length to be transferred.

socket programming using TCP/IP method(只是记录关键的步骤)

  • client:

    Socket clientSocket = new Socket(server_ip, port_number); // open a socket  
    DataInputStream dis = new DataInputStream(clientSocket.getInputStream()); // get response from the server  
    DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream()); // write java primitive type to the output stream  
    dos.close(); dis.close(); clientSocket.close(); // should always close the output stream and input stream before close the socket  
    
  • server:

    SeverSocket serverSocket = new ServerSocket(port_number); // open a socket  
    Socket connection = serverSocket.accept(); // accept connections from the client  
    DataInputStream dis = new DataInputStream(connection.geiInputStream()); // get message from the client  
    DataOutputStream dos = new DataOutputstream(conncetion.getOutputStream()); // send infomation to the client  
    dos.close(); dis.close(); connection.close(); serverSocket.close(); // should always close the output stream and input stream before close the socket  
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值