Java UDP uses Socket for network chat (1), udpsocket

Source: Internet
Author: User

Java UDP uses Socket for network chat (1), udpsocket

Author: Qing Dujun

Address: http://blog.csdn.net/qingdujun/article/details/39312065


This article demonstrates how to use Socket for network chat to enable the client to send messages to the server. When "886" is sent, the client is closed.

1) the client sends data to the server. If "886" is sent, the client is disabled.

2) The server keeps listening to port 9527 and prints the data to the console.


I. Client: It mainly uses keyboard input and closes the client when sending "886" data packets.

Package udp. send2.qdj; import java. io. bufferedReader; import java. io. inputStreamReader; import java.net. datagramPacket; import java.net. datagramSocket; import java.net. inetAddress; public class CUdpSend2 {public static void main (String [] args) throws Exception {// send data from Port 1234 of the local machine using new DatagramSocket ds = ramsocket (1234 ); // input BufferedReader bufr = new BufferedReader (new InputStreamReader (System. in); Str Ing line = null; while (line = bufr. readLine ())! = Null) {if ("886 ". equals (line) break; byte [] buf = line. getBytes (); // package data and send it to ingress rampacket dp = new ingress rampacket (buf, buf. length, InetAddress. getByName ("10.100.56.210"), 9527); // send data ds. send (dp);} // close resource ds. close ();}}

Client display:


2. The server monitors port 9527 and prints the data to the console.

1) the console can be dragged and dragged out. If you need to put it in, click the bottom option on the left.

2) switch to the console. You can click the 2nd options on the left of "minimize.

Package udp. rece2.qdj; import java.net. datagramPacket; import java.net. datagramSocket; public class CUdpRece2 {public static void main (String [] args) throws Exception {// listen to port 9527 DatagramSocket ds = new DatagramSocket (9527); while (true) {byte [] buf = new byte [1024]; // defines a data packet: DatagramPacket dp = new DatagramPacket (buf, buf. length); // accept the data packet ds. receive (dp); // extract data String ip = dp. getAddress (). getHostAddress (); String data = new String (dp. getData (), 0, dp. getLength (); System. out. println ("ip:" + ip + "\ n data:" + data );}}}
Server, display effect:

References: Lecture on Java video Bi Xiangdong

Address: http://blog.csdn.net/qingdujun/article/details/39312065


Java UDP enables multi-user chat

UDP can also traverse the list like TCP and send it.
The client opens a UDP port to receive the datagram
Then the server can directly send the datagram packet if there is a message to be sent.
UDP does not need to maintain the connection, which is simpler than TCP.

You can also use multicast to search for java UDP multicast. You can find a lot of instance code and chat room code.

Why can't I send messages to both ports at the same time by using java Udp to write chat programs? How can this problem be solved?

Use DatagramSocket and DatagramPacket
The initramsocket uses a sending port number.
Parameter of DatagramPacket
DatagramPacket (bytes [] bytes, int bytes []. length, InetAddress ip, int port); the port in is the target port.
To send a message to two port numbers, you need to create two initrampacket addresses, fill in two IP addresses and port numbers respectively, and send the message using initramsocket.
The other two ports need to be monitored.
Note that the ip address must be unobstructed for the LAN, and the WAN may cause the network administrator to block it.
The following is an example. Send it twice.
Int server _port = 7000;
String severIp = "192.168.1.100 ";
DatagramSocket clientSocket;
Try
{
InetAddress local = InetAddress. getByName (severIp );
DatagramPacket dPacket;
ClientSocket = new DatagramSocket;
DPacket = new DatagramPacket ("Hello". getBytes ("UTF-8"), "Hello". getBytes ("UTF-8"). length,
Local, SERVER_PORT );
ClientSocket. send (dPacket); // here it is sent
Byte [] msg = new byte [1, 11246];
DatagramPacket rPacket = new DatagramPacket (msg, msg. length );
ClientSocket. receive (rPacket); // receives the listener.
}
Catch (IOException e)
{
E. printStackTrace ();
}
ClientSocket. close ();

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.