The Java implementation of chatting with UDP mode.

zhaozj2021-02-08  236

I have recently studied Java to implement OICQ. This is a simple programming of my client. Mainly through the UDP mode, the user's dialogue is implemented. A method of serialization is used. The following is the source code, please add more comments.

// Send message class message.java

Import java.io.serializable;

Public Class Message IMPLEments Serializable {

Private string msg; private string deestid;

Public Message (String Msg, String ID, String Destid {this.msg = msg; this.id = id; this.destid = destID;} // get message content public string getMessage () {return msg;} // The sender's IP public string getId () {return id;} // gets the acceptor's IP public string getDestid () {return destid;}}

// Acceptance party receiver.javaimport java.net. *; Import java.io. *;

public class Receiver extends Thread {private Message msg; private DatagramPacket packet; private DatagramSocket socket; private byte [] recvBuf = new byte [500]; public Receiver () {try {// Create udp to receive the data packet. Packet = New DataGrampacke (Recvbuf, Recvbuf.Length); // Create an acceptor's UDP port to receive data socket = new DataGramsocket (7002);} catch (Exception E) {}}}}}}}} catches

Public void run () {TRY {while (true) {// accepts data socket.receive (packet);

ByteArrayInputStream byteStream = new ByteArrayInputStream (recvBuf); ObjectInputStream is = new ObjectInputStream (new BufferedInputStream (byteStram)); Object o = is.readObject (); msg = (Message) o; is.close ();

System.out.println (Msg.getMessage ());}} catch (throwable t) {t.printStackTrace ();}}}

// Sender sender.javaimport java.Net. *; Import java.io. *;

Public class sender extends thread {

Private Message Msg; Private DataGramsocket Socket; Private DataGrampacket Packet; Public Sender (Message MSG) {Try {this.msg = msg; // Get the IP address of the destination String destID = msg.getDestid (); // Getting to send message content String destID = msg.getDestIDString message = msg.getMessage (); ByteArrayOutputStream byteStream = new ByteArrayOutputStream (500); ObjectOutputStream os = new ObjectOutputStream (new BufferedOutputStream (byteStream)); os.writeObject (msg); os.flush () Byte [] sendbuf = bytestream.tobyteaRray (); // Create a UDP packet to send data packet = new data, inetdress.getbyname (destID), 7002); // creation send UDP socket Specify the socket. Socket = New DataGramsocket (7000); Os.close ();} catch (throwable t) {T.PrintStackTrace ();}} public void run () {Try {// Send message socket.send (Packet); // Send it to close the socket. Socket.close ();} catch (throwable t) {t.printStackTrace ();}}}

// Test program udp_chat.javaimport java.io. *; import java.net. *;

Public class udp_chat {public udp_chat (string s) {}

Public static void main (string args []) throws exception {Message A = new Message, "211.92.155.200", "211.92.155.200"); new sender (a) .start (); New Receiver () .start ();}}

Reference article: Java Tips 40: Data Package Transfer Objects Use Data Pack to Convection Java Objects Bootcool@263.net

转载请注明原文地址:https://www.9cbs.com/read-2626.html

New Post(0)