Instant Messaging liteip-II chatlist

Source: Internet
Author: User

The chatlist list lists all users in the current LAN. Double-click a user to communicate with the user.

Chatlist. Java:

Package chat; <br/>/** <br/> * @ (#) chatlist. java <br/> * @ author Tom <br/> * @ Version 1.00 2008/12/30 <br/> */<br/>/** <br/> * LAN chat tool liteip <br/> **/<br/> Import Java. AWT. list; <br/> Import Java. AWT. button; <br/> Import Java. AWT. label; <br/> Import Java. AWT. frame; <br/> Import Java. AWT. panel; <br/> Import Java. AWT. event. *; <br/> Import java.net. serversocket; <br/> Import java.net. socket; <br/> Import ja Va.net. datagramsocket; <br/> Import java.net. inetaddress; <br/> Import java.net. extends rampacket; <br/> Import Java. util. hashset; <br/> // import Java. util. hashtable; <br/> // import Java. io. inputstream; <br/> // import Java. io. outputstream; <br/> // import Java. io. bufferedreader; <br/> // import Java. io. bufferedwriter; <br/> // import Java. io. printwriter; <br/> // import Java. io. inputstreamreader; </P> <p> @ suppresswa Rnings ("serial") <br/> public class chatlist extends frame {<br/> Private Static int D = 0; // used for debugging <br/> private list friends; // for online users, check whether an address already exists in the list. Use equals to compare <br/> private button chat; // chat button, click to bring up the chat dialog box <br/> private label name; // display the selected user <br/> Private Static datagramsocket sender; // send a UDP packet to the LAN <br/> Private Static datagramsocket receiver; // accept UDP packets of other users <br/> Public static hashset <chatwith> HT; // open Dialog Box maintenance table <br/> // public static hashtable <inetaddress, socket> ht2; // socket maintenance list, (inetaddress, socket) <br/> Public final staticint senderport = 8888; // sending port <br/> Public final static int receiverport = 8080; // receiving port <br/> private inetaddress multicast; <br/>/** <br/> * before creating a cost object instance, call the setjavasramsocket method <br/> * to avoid generating two chatlist instances. <br/> * PS: if you want to achieve this through the "list mode", it is not feasible to tell you here. <Br/> * This method is only called externally and is not called in this instance <br/> */<br/> Public static Boolean setjavasramsocket () {<br/> Boolean flag = true; <br/> try {<br/> // allocate a port <br/> // system. out. println ("chatlist" + (d ++); // 0 <br/> sender = new datagramsocket (senderport); <br/> receiver ER = new datagramsocket (receiverport ); <br/> // system. out. println ("chatlist" + (d ++); // 1 <br/>}catch (exception e) {<br/> flag = false; <br/> system. out. println ("error I N chatlist port allocate. "); <br/> system. out. println (E); <br/>}< br/> return flag; <br/>}< br/>/* <br/> In the constructor, complete most of the Operations <br/> build an object, add listeners, and build the thread for sending UDP packets and the thread for receiving UDP packets <br/> */<br/> Public chatlist () {<br/> ht = new hashset <chatwith> (); // construct an online user maintenance table <br/> // ht2 = new hashtable <inetaddress, socket> (); // construct a socket table with online users <br/> friends = new Java. AWT. list (20, false); // build a container that displays online users. 20 rows are displayed at a time. You cannot select multiple <br/> friends. additemlisten Er (New listenitem (); // Add and click the listener. The label of the button will change. <br/> friends. addactionlistener (New listendoubleclick (); // Add a double-click listener <br/> chat = new button ("chat with"); <br/> name = new label (); <br/> chat. addactionlistener (New listendoubleclick (); // Add a listener to the button <br/> panel P = new Panel (); // Add each widget <br/> P. setlayout (null); <br/> P. add (Friends); <br/> P. add (CHAT); <br/> P. add (name); <br/> Add (p); <br/>/** <br/> * exit the listener in the window <br/> * Attention: When you exit the program, you need to send the checkout string to the LAN User <br/> **/<br/> addwindowlistener (<br/> New windowadapter () {<br/> Public void windowclosing (invalid wevent e) {<br/> try {<br/> string STR = new string ("checkout "); <br/> byte [] IP = Str. getbytes (); <br/> If (Multicast! = NULL & sender! = NULL) {<br/> // numeric report, the byte to be sent, the length of the byte, the sending Address (broadcast address ), receiving port <br/> datagrampacket dp = new datagrampacket (IP, IP. length, multicast, receiverport); <br/> sender. send (DP); <br/>}< br/>}catch (exception ex) {<br/> system. out. println ("error occurs when sending checkout message. "); <br/> system. out. println (Ex); <br/>}< br/> system. exit (0); <br/>}< br/>); <br/> // send a UDP packet to other users on the LAN, notify yourself that you have launched <br/> senditem (); <br/> // accept The UDP packet sent by other users is accepted by the thread. <Br/> New additem (). start (); <br/> // listen for connection requests sent by other users <br/> New listening (). start (); <br/> // system. out. println ("chatlist: chatlist ():" + (d ++ )); // 7 <br/>}< br/>/** <br/> * It is said that the layout is controlled, <br/> * only needs to be refined in the future <br/> **/<br/> Public void dolayout () {<br/> // int whoalw = (INT) This. getsize (). getwidth ()-2; <br/> // int whoalh = (INT) This. getsize (). getheight ()-2; <br/> int friendsw = (INT) (friends. getpreferredsize (). Getwidth (); <br/> int friendsh = (INT) (friends. getpreferredsize (). getheight (); <br/> int chatw = (INT) (chat. getpreferredsize (). getwidth (); <br/> int chath = (INT) (chat. getpreferredsize (). getheight (); <br/> int namew = (INT) (name. getpreferredsize (). getwidth (); <br/> int nameh = (INT) (name. getpreferredsize (). getheight (); <br/> friends. setbounds (0, 0, friendsw + 3, friendsh + 5); <br/> chat. setbounds (friendsw + 10, 4, chatw + 5, chath + 5); <br/> name. setbounds (friendsw + 10, 4 + chath + 5 + 4, 4 * namew + 20, nameh + 5); <br/> super. dolayout (); <br/> // system. out. println ("chatwith: dolayout:" + (d ++); <br/>}< br/>/** <br/> * click the listener, change the label of the button <br/> **/<br/> private class listenitem implements itemlistener {<br/> Public void itemstatechanged (itemevent E) {<br/> // system. out. println ("chatlist: listenitem: itemstatechanged:" + (D ++); <br/> // display the IP address of the selected item <br/> name. settext (friends. getselecteditem (); <br/>}< br/>/** <br/> * double-click the listener, create chat dialog box <br/> * only transmits the inetaddress constructed by the IP address of the Peer party as a parameter <br/> **/<br/> private class listendoubleclick implements actionlistener {<br/> inetaddress ADDR; <br/> string STR; <br/> Public void actionreceivmed (actionevent E) {<br/> try {<br/> // obtain the selected IP address <br/> STR = friends. getselecteditem (); <br/> // Build inetaddress Based on the selected items <br/> ADDR = inetaddress. getbyname (STR); <br/> // If the clicked address is not in the maintenance list, create a dialog box <br/> // If the clicked address is already in the maintenance list, indicates that the user clicked the address twice <br/> If (! Util. isthisipin (ADDR, HT) {<br/> chatwith CW = new chatwith (ADDR, (socket) null); <br/> CW. settitle ("chat with" + friends. getselecteditem (); <br/> CW. setsize (400,400); <br/> CW. setvisible (true); <br/> // Add to the maintenance list of the open dialog box <br/> ht. add (CW); <br/>}< br/>}catch (exception ex) {<br/> system. out. println ("error occurs when you want to chat with" + STR + (d ++); <br/> system. out. println (Ex); <br/>}< br/>/* <br /> Send UDP messages to other users in the LAN, checkin <br/> */<br/> private void senditem () {<br/> try {<br/> // construct the LAN broadcast address <br/> inetaddress MC = inetaddress. getbyname ("255.255.255.255"); <br/> // The local IP address is no longer sent here, because the receiver can identify the IP address of the sender through UDP packets <br/> // to work with this program, send the string checkin here <br/> // Of Course, when exiting this program, you need to send a checkout so that machines in the LAN can remove themselves from their list <br/> string STR = new string ("checkin "); <br/> byte [] IP = Str. getbytes (); <br/> // construct a datagram, the message to be sent (checkin), the length of the message to be sent, broadcast Address, receiving port <br/> datagrampacket dp = new datagrampacket (IP, IP. length, MC, receiverport); <br/> If (sender! = NULL) <br/> sender. send (DP); <br/>}catch (exception e) {<br/> system. out. println ("error occurs when senditem" + (d ++); <br/> system. out. println (E); <br/>}</P> <p>/* <br/> * listens to other users online, obtain UDP packets <br/> * obtain by thread <br/> */<br/> private class additem extends thread {<br/> Public void run () {<br/> while (true) {<br/> try {<br/> If (else er! = NULL) {<br/> byte [] B = new byte [1024]; <br/> // constructs a datagram to receive data, byte array, array length <br/> datagrampacket dp = new datagrampacket (B, B. length); <br/> // receives packets from the LAN <br/> receiver. receive (DP); <br/> // obtain the data <br/> B = DP. getdata (); <br/> // obtain the incoming data, checkin or checkout <br/> // note that, leading and trailing leading spaces should be removed <br/> string STR = new string (B ). trim (); <br/> // get the address in the package <br/> string ADDR = DP. getaddress (). gethostaddress (); <br/> If (ADDR! = NULL & Str. Equals ("checkin ")&&! Util. isinfriends (ADDR, Friends) {<br/> // The checkin data is received, and the address is not in the user list, add to friends user list <br/> friends. add (ADDR); <br/> // send a checkin message to the other user. <br/> STR = new string ("checkin"); <br/> B = Str. getbytes (); <br/> // construct the datagram, the message to be sent, the length of the message to be sent, the address to be sent, and the receiving port <br/> inetaddress iaddr = inetaddress. getbyname ("commandid 255.255"); <br/> dp = new datagrampacket (B, B. length, iaddr, receiverport); <br/> If (sender! = NULL) <br/> sender. send (DP); <br/>}< br/> // If the checkout message is sent, delete this address from the user list <br/> else if (util. isinfriends (STR, Friends) & Str. equals ("checkout") {<br/> // Delete this address from the display <br/> friends. remove (ADDR); <br/>}< br/> sleep (10); <br/>} catch (exception E) {<br/> system. out. println ("error occurs when additem to friends" + (d ++); <br/> system. out. println (E); <br/>}< br/>/* <br/> obtain external t CP request, and expand the dialog <br/> when a new external request is sent, open the dialog box <br/> */<br/> private class listening extends thread {<br/> serversocket Server = NULL; <br/> Public void run () {<br/> try {<br/> Server = new serversocket (receiverport); // build the socket listener <br/> while (true) {<br/> Socket socket = server. accept (); <br/> // After listening to a socket, construct the response address <br/> inetaddress iaddr = socket. getinetaddress (); <br/> // if you are talking to yourself <br/> If (util. isinetself (iaddr) {<br/> Chatwith CW = new chatwith (iaddr, socket); <br/> CW. settitle ("chat with" + (socket. getinetaddress ()). gethostaddress (); <br/> CW. setsize (400,400); <br/> CW. setvisible (true); <br/>}< br/> // if the address is already in the maintenance list, the local dialog box <br/> // is also double-clicked. <br/> else if (util. isthisipin (iaddr, HT) {<br/> util. getthatcw (iaddr, HT ). setsocket (socket); <br/>}< br/> // If the dialog box is not displayed locally, the else if (! Util. isthisipin (iaddr, HT) {<br/> chatwith CW = new chatwith (iaddr, socket); <br/> CW. settitle ("chat with" + (socket. getinetaddress ()). gethostaddress (); <br/> CW. setsize (400,400); <br/> CW. setvisible (true); <br/> ht. add (CW); <br/>}< br/> sleep (1000); <br/>}< br/>} catch (exception E) {<br/> system. out. println ("error occurs when listening" + (d ++); <br/> system. out. println (E); <br/> try {<br/> server. close (); <br/>}catch (exception ex) {<br/> system. out. println ("error occurs when closing server" + (d ++); <br/> system. out. println (Ex); <br/>}< br/>}

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.