【问题标题】:What is the port used by java? [closed]java使用的端口是什么? [关闭]
【发布时间】:2011-12-30 10:38:30
【问题描述】:

我写了这段 java J2SE 代码:

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TwoWaySerialComm 
{
    public TwoWaySerialComm()
    {
        super();
    }
    void connect ( String portName ) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();

                (new Thread(new SerialWriter(out))).start();

                serialPort.addEventListener(new SerialReader(in));
                serialPort.notifyOnDataAvailable(true);

            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }
    public static class SerialReader implements SerialPortEventListener 
    {
        private InputStream in;
        private byte[] buffer = new byte[1024];

        public SerialReader ( InputStream in )
        {
            this.in = in;
        }

        public void serialEvent(SerialPortEvent arg0) {
            int data;

            try
            {
                int len = 0;
                while ( ( data = in.read()) > -1 )
                {
                    if ( data == '\n' ) {
                        break;
                    }
                    buffer[len++] = (byte) data;
                }
                System.out.print(new String(buffer,0,len));
            }
            catch ( IOException e )
            {
                e.printStackTrace();
                System.exit(-1);
            }             
        }
    }
    public static class SerialWriter implements Runnable 
    {
        OutputStream out;

        public SerialWriter ( OutputStream out )
        {
            this.out = out;
        }

        public void run ()
        {
            try
            {                
                int c = 0;
                while ( ( c = System.in.read()) > -1 )
                {
                    this.out.write(c);
                }                
            }
            catch ( IOException e )
            {
                e.printStackTrace();
                System.exit(-1);
            }            
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try
        {
            (new TwoWaySerialComm()).connect("COM4");
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我创建了一个与系统同时启动的批处理文件:

java TwoWaySerialComm

control panelscheduled tasks 中,我创建了一个任务以在每次系统启动时启动批处理文件。

那么在这种情况下这个java程序使用的socketport是什么?

【问题讨论】:

  • 这个Java程序没有使用socket端口。

标签: java windows sockets scheduled-tasks


【解决方案1】:

您的示例使用 JavaComm API(gnu.io 命名空间)来访问串行端口(在您的示例中,您正在访问 COM4)。

这与 TCP/IP 套接字 API 无关。您的示例中没有涉及 TCP/IP 网络。

【讨论】:

  • 我不太擅长套接字:使用套接字是否意味着 TCP/IP 网络?
  • 大部分是的。套接字用于任何类型的进程间通信(unix 套接字、TCP 套接字......)。实际上,在 Java 中,您不使用“套接字”,而是使用 java.net API(本文中的术语 Socket 指的是 TCP 套接字)。在您的示例中,您根本没有使用java.net。您正在使用与“套接字”无关的不同通信库。
猜你喜欢
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 2016-09-02
  • 2022-01-18
  • 2010-09-11
  • 2011-09-10
  • 1970-01-01
  • 2016-09-29
相关资源
最近更新 更多