⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpmessagesocket.java

📁 Java SIP
💻 JAVA
字号:
/* * This file was derived from libdissipate, an open source SIP library. The original file  * was modified on 1/23/2001.  Please see * http://www.div8.net/dissipate for more information. * * Copyright (c) 2000 Billy Biggs <bbiggs@div8.net> * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. *  * This library 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 Library General Public * License for more details. *  * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB.  If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * *//** * class TCPMessageSocket *  * This is the TCP implementation of the MessageSocket class.  It is used * for performing operations on a new or existing TCP socket. * * This code has been generated using C2J++ * C2J++ is based on Chris Laffra's C2J (laffra@watson.ibm.com) * Read general disclaimer distributed with C2J++ before using this code * For information about C2J++, send mail to Ilya_Tilevich@ibi.com */package org.mitre.jsip;import java.io.*;import java.net.*;public class TCPMessageSocket extends MessageSocket{  /**   * TCPMessageSocket   */  public TCPMessageSocket()  {    type = TCP;    try {        localSocket = new ServerSocket(localPort);     } catch (IOException ioe) {	System.err.println("Couldn't open localsocket: " + ioe);	localSocket = null;    }  }  /**   * TCPMessageSocket   * @param newfd   */  public TCPMessageSocket(Socket newSocket)  {	type = TCP;		remoteSocket = newSocket;	try {	    localSocket = new ServerSocket(localPort);	} catch (IOException ioe) {	    System.err.println("Couldn't open localsocket: " + ioe);	    localSocket = null;	}  }      /** * connect * @param portnum * @return int */  public int connect(String hostname, int portnum)  {      if (remoteSocket == null) {	  try {	      remoteSocket = new Socket(hostname, portnum);	  } catch (UnknownHostException uhe) {	      System.err.println("Unknown host: " + uhe);	      return -1;	  } catch (IOException ioe) {	      System.err.println("Error creating socket: " + ioe);	      return -1;	  }      }            try {	  bos = new BufferedOutputStream(remoteSocket.getOutputStream());	  bis = new BufferedInputStream(remoteSocket.getInputStream());      } catch (IOException ioe) {	  System.err.println("Error opening BufferedStrings");      }            return 0;  }    public int connect( InetAddress host, int portnum ) {	return 0;    }  /**   * send   * @param sendbuffer   * @param length   * @return int   */  public  int send(String sendbuffer, int length)  {    try {      bos.write(sendbuffer.getBytes(), length, 0);    } catch (IOException ioe) {      System.err.println( "TCPMessageSocket::send(): send() failed: " + ioe);      return -1;    }        return 0;  }    /**   * receive   * @param recvbuffer   * @param maxlength   * @return int   */  public  int receive(byte[] recvbuffer, int maxlength)  {    int numbytes;        try {      numbytes = bis.read(recvbuffer, 0, maxlength );    } catch (IOException ioe) {      System.err.println( "TCPMessageSocket::recieve(): recv() failed: " + ioe );      return -1;    }        return numbytes;  }    /**   * listen   * @param portnum   * @return int   */  public  int listen(MessageQueue queue)  {//      if ( bind( socketfd, (struct sockaddr  ) &socketaddress, sizeof( struct sockaddr ) ) == -1 ) {//        perror( "TCPMessageSocket::listen(): bind() failed" );//        return -1;//      }    //      if ( ::listen( socketfd, 10 ) == -1 ) {//        perror( "TCPMessageSocket::listen(): listen() failed" );//        return -1;//      }        return 0;  }    /**   * accept   * @return int   */  public  void accept()  {//      if ( ( connectfd = ::accept( socketfd, (struct sockaddr  ) &socketaddress, \//  				 &sockaddr_in_size ) ) == -1 ) {//        perror( "TCPMessageSocket::accept(): accept() failed" );//        return -1;//      }        //    return connectfd;    aSocket = null;    try {      aSocket = localSocket.accept();    } catch (IOException ioe) {      System.err.println("error acception connections: " + ioe);    }//    return returnSocket;  }    /**   * listenOnEvenPort   * @return int   */  public  int listenOnEvenPort()  {    return -1;  }      public void close() {	try {	    localSocket.close();	} catch (IOException ioe) {	    System.err.println( "Error closing TCP socket: " + ioe );	}    }    // class variables    private BufferedOutputStream bos;    private BufferedInputStream bis;    private Socket aSocket;    private Socket remoteSocket;    private ServerSocket localSocket;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -