Java implements Simple Chat Room instances based on socket

Source: Internet
Author: User

Java implements Simple Chat Room instances based on socket

This example describes how to implement a simple chat room in Java based on socket. Share it with you for your reference. The specific implementation method is as follows:

Chatroomdemo. java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Package com. socket. demo;

Import java. io. IOException;

Import java.net. DatagramSocket;

Public class ChatRoomDemo {

/**

* @ Param args

* @ Throws IOException

*/

Public static void main (String [] args) throws IOException {

System. out. println ("---- entering the chat room ----");

DatagramSocket send = new DatagramSocket ();

DatagramSocket rece = new DatagramSocket (10001 );

New Thread (new SendDemo (send). start (); // start the sending Thread

New Thread (new ReceiveDemo (rece). start (); // start the receiving end Thread

}

}

SendDemo. java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

Package com. socket. demo;

Import java. io. BufferedReader;

Import java. io. InputStreamReader;

Import java.net. DatagramPacket;

Import java.net. DatagramSocket;

Import java.net. InetAddress;

Public class SendDemo implements Runnable {

Private DatagramSocket ds;

// Parameter Constructor

Public SendDemo (DatagramSocket ds ){

This. ds = ds;

}

@ Override

Public void run (){

Try {

BufferedReader bufr = new BufferedReader (new InputStreamReader (

System. in ));

String line = null;

While (line = bufr. readLine ())! = Null ){

Byte [] buf = line. getBytes ();

/*

* // 192.168.1.255 is the ip address segment broadcast address. The information sent to this IP address is as follows,

* All ip addresses in the 192.168.1.1-192.168.1.255 ip segment can receive messages.

*/

Required rampacket dp = new DatagramPacket (buf, buf. length, InetAddress. getByName ("192.168.1.255"), 10001 );

Ds. send (dp );

If ("886". equals (line ))

Break;

}

Ds. close ();

} Catch (Exception e ){

}

}

}

ReceiveDemo. java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

Package com. socket. demo;

Import java.net. DatagramPacket;

Import java.net. DatagramSocket;

Public class ReceiveDemo implements Runnable {

Private DatagramSocket ds;

Public ReceiveDemo (DatagramSocket ds ){

This. ds = ds;

}

@ Override

Public void run (){

Try {

While (true ){

// 2. Create a data packet.

Byte [] buf = new byte [1, 1024];

DatagramPacket dp = new DatagramPacket (buf, buf. length );

// 3. Use the Receiving Method to store data in the data packet.

Ds. receive (dp); // blocked.

// 4. parse the data in the data packet object, such as the address, port, and data content.

String ip = dp. getAddress (). getHostAddress ();

Int port = dp. getPort ();

System. out. println ("---- port -----" + port );

String text = new String (dp. getData (), 0, dp. getLength ());

System. out. println (ip + ":" + text );

If (text. equals ("886 ")){

System. out. println (ip + "... quit chatting room ");

}

}

} Catch (Exception e ){

}

}

}

Run the following command:

I hope this article will help you with java programming.

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.