udpsender.java
来自「google公司的用Java写的一个聊天软件的原代码」· Java 代码 · 共 124 行
JAVA
124 行
/*************************************************************************** * Copyright 2006-2008 by Christian Ihle * * kontakt@usikkert.net * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/package net.usikkert.kouchat.net;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.util.logging.Level;import java.util.logging.Logger;import net.usikkert.kouchat.Constants;import net.usikkert.kouchat.misc.ErrorHandler;/** * Sends UDP packets directly to a user. Useful for private chat, * where not everyone should get the packets. * * @author Christian Ihle */public class UDPSender{ private static final Logger LOG = Logger.getLogger( UDPSender.class.getName() ); private DatagramSocket udpSocket; private boolean started; private final ErrorHandler errorHandler; /** * Default constructor. */ public UDPSender() { errorHandler = ErrorHandler.getErrorHandler(); } /** * Sends a packet with a message to a user. * * @param message The message to send. * @param ip The ip address of the user. * @param port The port to send the message to. */ public void send( final String message, final String ip, final int port ) { if ( started ) { try { InetAddress address = InetAddress.getByName( ip ); byte[] encodedMsg = message.getBytes( Constants.MESSAGE_CHARSET ); int size = encodedMsg.length; if ( size > Constants.NETWORK_PACKET_SIZE ) { LOG.log( Level.WARNING, "Message was " + size + " bytes, which is too large.\n" + " The receiver might not get the complete message.\n'" + message + "'" ); } DatagramPacket packet = new DatagramPacket( encodedMsg, size, address, port ); udpSocket.send( packet ); } catch ( final IOException e ) { LOG.log( Level.SEVERE, "Could not send message: " + message ); } } } /** * Closes the UDP socket. */ public void stopSender() { started = false; if ( udpSocket != null && !udpSocket.isClosed() ) { udpSocket.close(); } } /** * Creates a new UDP socket. */ public void startSender() { try { udpSocket = new DatagramSocket(); started = true; } catch ( final IOException e ) { LOG.log( Level.SEVERE, e.toString(), e ); errorHandler.showError( "Failed to initialize network:\n" + e + "\n\nYou will not be able to send private messages!" ); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?