Before you start, let's begin by introducing the TCP and UDP protocols. For a friend who has done network development, these two protocols should be not unfamiliar, only excerpts of the online two of the introduction of the agreement to everyone, for reference only.
TCP and UDP differences
TCP---Transmission Control Protocol, which provides a connection-oriented, reliable byte throttling service. Before the client and server Exchange data, a TCP connection must be established between the two parties before the data can be transferred. TCP provides timeouts, discards duplicate data, verifies data, and controls traffic to ensure that data is transferred from one end to the other.
UDP---User Datagram Protocol is a simple transport layer protocol for datagram. UDP does not provide reliability, it simply sends the application to the IP layer, but it does not guarantee that it will reach its destination. Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as timeout, the transmission speed is fast
As you can see, the main difference between UDP and TCP is that UDP is connectionless, and this is the most important difference when using Netty for development.
First of all, on the choice of ChannelFactory, UDP communication chooses NIODATAGRAMCHANNELFACTORY,TCP communication we choose Nioserversocketchannelfactory In the choice of bootstrap, UDP chooses Connectionlessbootstrap, and TCP chooses Serverbootstrap. The following is a startup program class on a UDP communication server side:
Java code import java.net.inetsocketaddress; import java.net.socketaddress; import java.util.concurrent.executors; import org.apache.log4j.xml.domconfigurator; Import org.jboss.netty.bootstrap.connectionlessbootstrap ; import org.jboss.netty.channel.socket.datagramchannelfactory; import org.jboss.netty.channel.socket.nio.niodatagramchannelfactory; import org.springframework.context.applicationcontext; import org.springframework.context.support.filesystemxmlapplicationcontext; import com.game.netty.network.serverpipelinefactory; /** * Author: chenpeng * e-mail:46731706@qq.com * Creation time:2012-7-16 morning 10:52:30 * netty udp communication test */&Nbsp; public class servertest { public static void main (String[] args) { int port = 8888; Domconfigurator.configureandwatch ("Config/log4j.xml"); applicationcontext factory = new filesystemxmlapplicationcontext ( new string[] { "Config/propholder.xml"  }); Datagramchannelfactory udpchannelfactory = new niodatagramchannelfactory ( Executors.newcachedthreadpool ());  &NBsp ConnectionlessBootstrap bootstrap = new Connectionlessbootstrap (udpchannelfactory); serverpipelinefactory udpserverfactory = ( serverpipelinefactory) Factory.getbean ("Serverpipelinefactory"); bootstrap.setpipelinefactory (udpserverfactory); socketaddress serveraddress = new inetsocketaddress (port); bootstrap.bind (serveraddress); system.out.println (port+ " server is starting ..."); } }
For codecs decoder and encoder, and CHANNELPIPELINEFACTORY,UDP development is no different from TCP, this is not covered in detail.
For Channelhandler, it is the core of the difference between UDP and TCP. We all know that UDP is connectionless, which means that you get the current session connection through the Getchannel () method of the Messageevent parameter object, but its isconnected () always returns false. In UDP development, in the message Fetch event callback method, the current session connection channel object can be sent directly through the channel write method to send data to the End-to-end channel.write (messages, remoteaddress), The first argument is still the message object to send.
The second parameter is the End-to-end socketaddress address object to send. The most important thing to note here is socketaddress, which we can get through channel.getremoteaddress () in TCP communications, but in UDP communications,
We must obtain the socketaddress address from the messageevent by calling the Getremoteaddress () method。
Java code public void messagereceived (channelhandlercontext ctx, messageevent e) throws Exception { channelbuffer buffer = (Channelbuffer) e.getmessage (); Byte[] recbyte = buffer.copy (). Tobytebuffer (). Array (); String msg = new string (recbyte); system.out.println (" Server received message: " + msg"; random random = new random (); int rspword = random.nextint (10000);     SYSTEM.OUT.PRINTLN ("server writes message:" + rspword); channelbuffer response = new dynamicchannelbuffer (; ) &nbsP;response.readbytes (Rspword); e.getchannel (). Write (response, E.getremoteaddress ()); }
Choose Netty UDP for Network game development, can not ignore a problem is due to UDP instability caused by the loss of packets need to return. In other words, we need to establish a detection mechanism to confirm the need to resend the packet loss data in case of packet loss, and the mechanism is relatively simple.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.