Recently, I have been taking the time to review some Java technology applications.Nothing to do today, based on the UDP protocol, wrote a very simple chat room program.Now work, rarely use socket, also is a Java network programming aspects of a simple memory.
First look at the effect:
The effect of implementation can be said to be very very simple, but still
a report, UDP does not depend on the client or server. Both parties are equal. at the micro level, only one packet is sent to the client and the listener to the server. Sending a datagram to a vro at the sending end is like sending a telegram to the post office. What follows is that the sender cannot control or know it. Therefore, it is not reliable, and there may be loss of packets that are unknown. Just as each telegram has a recipient, each datag
receive the datagram packet6 D: Parse packet (byte converted to string) and print in console7 E: Freeing Resources8 */9 Public classnewreceive {Ten One Public Static voidMain (string[] args)throwsIOException { A - //A: Creating A Socket Receive-side object -Datagramsocket ds =NewDatagramsocket (10010); the - //B: Create a datagram package to receive data (Create Container) - byte[] Bys =New byte[1000]; -Datagrampacket DP =NewDatagrampacket (bys,b
Learned computer network communications are aware that the transfer of data between computers by two kinds, that is, TCP communication and UDP communication. TCP is a reliable connection-oriented communication protocol, and two UDP is unreliable for connectionless communication protocols.
In Java, there is TCP based network socket communication, but also
= null;
While ((Line=bufr.readline ()) =null)
{
if ("886". Equals (line))
Break ;
byte[] buf = Line.getbytes ();
//Package The data and send it to the 9527 port on the machine
Datagrampacket DP = new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("10.100.56.210"),9527);
//Send data
Ds.send (DP);
}
//Close Resources
Ds.close ();
}
}
Client Display effect:Second, the server side, the main is always listening to 9527 ports, and the monitoring of the data
receive-side programPackage client;Import Java.net.DatagramPacket;Import Java.net.DatagramSocket;Import java.net.InetAddress;public class Client_test {public static void Main (string[] str){Try{Determine portint port = 8080;Creates a Datagramsocket object and specifies that the program's communication port is 8080Datagramsocket receivesocket = new Datagramsocket (port);while (true){Create a Receive BufferByte[] by = new byte[1024];Create a Receive packetDatagrampacket receivepacket = new Datagr
Java socket based on UDP/IP protocol applicationServer side: 1, create Datagramsocket, specify port number2. Create Datagrampacket3, receive the data sent by the client4. Read dataClient:1. Define send message: The IP port number of the server to send the content2. Create a datagrampacket that contains the information that will be sent3. Create Datagramsocket4. Send dataServer-sideImport java.io.IOException
1.UDP IntroductionSend a message using UDP, the other party does not necessarily receive, because all the information is sent in the form of a datagram, which requires the client to always wait for the message sent by the receiving server, Use the Datagramsocket class and the Datagrampacket class in Java to complete the development of a
(String remoteurl,string localfilepath) {
InputStream in = null;
OutputStream out = null;
try {
File LocalFile = new file (Localfilepath);
String fileName = Localfile.getname ();
Smbfile remotefile = new Smbfile (remoteurl+ "/" +filename);
in = new Bufferedinputstream (new FileInputStream (LocalFile));
out = new Bufferedoutputstream (new Smbfileoutputstream (RemoteFile));
byte []buffer = new byte[1024];
while ((In.read (buffer))! =-1) {
Out.write (buffer);
Buffer = new b
transmitted by the UDP protocol and processes * * 1, defining the Uspsocket service. Usually listen to a port, in fact, to this network application to define the digital ID, * Easy to identify what data come over the application can process. * 2, define a packet, because the received byte data is to be stored. Because there are more features in the packet object * You can extract different data information from the byte data. * 3, through the socket
• Socket facilitates applications to access the communication protocol TCP/IP.• Socket is the end point of the communication link. We can think of socket as a telephone. With Socket, we have a communication tool. We can think of the IP address as the phone number and the port number as the extension number.1. TCP-based socket programming.• Java.net. ServerSocket is used to create a socket on the server.• Java.net. Socket is used to create the socket Socket of the client.• InetAddress (java.net.
The Java server and the Linux C client communicate with each other using UDP protocol, and the most critical point is the unification of data structure. For example, in C a char type is 8bit, in Java, Char is 16bit, so C and Java char types cannot be converted directly to each other. In this routine, the char type of C
UDP programmingIn the TCP index operation must establish a reliable connection, this will certainly waste a lot of system performance, in order to reduce this overhead, in the network also provides another transport protocol---UDP, unreliable connection, the protocol is widely used in various chat tools.I use Datagrampacket in UDP development to wrap a message to
UDP is an unreliable protocol that establishes a socket on both ends of the communication, which does not establish a persistent communication connection and only sends data unilaterally to the other party without checking the results of the transmission.UDP protocol-based communication in Java uses the receive and send methods of the Datagramsocket class, but the message needs to be encapsulated by a speci
1. Basic concepts:
A. zoorampacket and initramsocket are located in the java.net package.
B. DatagramPacket indicates the data datagram that stores the data, and DatagramSocket indicates the socket that accepts or sends the data datagram.
C. The network link composed of all these two types is based on the UDP protocol and is an unreliable protocol.2. Basic usage of DatagramSocket
DatagramSocket class: Creates a Socket instance for receiving and sendin
Communicate using a non-connected datagram (UDP)What is datagram?The datagram is an independent packet transmitted on the Internet, whether the datagram can reach the destination correctly, the time of arrival, the order and the correctness of the content are not guaranteed.Datagram and datagrampacket classes are used in Java.The Datagramsocket class uses the UDP protocol to implement a client-server socket
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.