?? listener.java
字號:
/*
* Listener.java
*
* Created on 2007年7月4日, 上午12:12
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package trans;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import javax.swing.JOptionPane;
/**
*
* @author 鵬飛
*/
public class Listener extends Thread
{
/** Creates a new instance of Listener */
public Listener ()
{
}
public void run()
{
byte [] recData = new byte [1024];
try
{
DatagramPacket recName=new DatagramPacket(recData,recData.length);
DatagramSocket recSocket=new DatagramSocket(2007);
while ( true )
{
try
{
recSocket.receive (recName);
String data = new String (recName.getData() , 0 ,recName.getLength (),"UTF-8");
String userName = data.substring ( 1 );
char flag = data.charAt (0);
if ( flag == 'S' )
{
MainFrame.detect('R' ,recName.getAddress (),MainFrame.message);
MainFrame.m.addToTable(recName.getAddress (),userName );
MainFrame.m.setCount ("在線人數: "+ MainFrame.m.tableModel.getRowCount ());
}
else if ( flag == 'R')
{
MainFrame.m.addToTable(recName.getAddress (),userName);
MainFrame.m.setCount ("在線人數: "+ MainFrame.m.tableModel.getRowCount ());
}
else if(flag == 'M')
{
MainFrame.detect('A' ,recName.getAddress (),"return");
String msg = userName.substring ( userName.indexOf ( "|") + 1 );
userName = userName.substring ( 0 , userName.indexOf ( "|") );
JOptionPane.showMessageDialog(null, msg ,"來自" + userName + "的消息", JOptionPane.DEFAULT_OPTION);
MainFrame.append ("log.txt","來自" + userName + "(" + recName.getAddress ().getHostAddress () +")的消息:" + msg);
}
else if (flag == 'A' && MainFrame.m.accRe)
JOptionPane.showMessageDialog(null, "消息已發送到!","提示", JOptionPane.DEFAULT_OPTION);
recName.setLength ( recData.length );
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, this.getClass ()+e.toString () ,"錯誤", JOptionPane.DEFAULT_OPTION);
}
}
}
catch ( SocketException e)
{
JOptionPane.showMessageDialog(null,e.toString (),"錯誤", JOptionPane.DEFAULT_OPTION);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -