0

我正在运行一个 xSocket 服务器,因此需要启动一个 chat.jar,它似乎没有调用该部分。我的代码有什么问题?

如果我创建一个 xSocketserver.jar,那么 exec 是否能够启动任何外部 jar?

import java.io.*;
import org.xsocket.connection.*;

public class xSocketServer
{
    protected static IServer srv = null;

    public static void main(String[] args)
    {
        try {
            srv = new Server("127.0.0.1",8090, new xSocketDataHandler());
            srv.run();
        }
        catch(Exception ex) {
            System.out.println(ex.getMessage());
        }

        try {
            System.out.println("setup exec");
            Process p = Runtime.getRuntime().exec("cmd java -jar D:\\chat.jar -n 0");
            p.waitFor();
            System.out.println("call exec");
        }
        catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    protected static void shutdownServer() {
        try {
            srv.close();
        }
        catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
4

4 回答 4

3

您应该阅读流程的输出流和错误流。您的命令可能失败并且您看不到错误,因为您没有读取错误流。

看看这篇文章

于 2010-11-10T14:04:36.260 回答
1
  1. srv.run(); --> 这个调用不会像 xSocket 文档所说的那样返回,所以其余的代码不会执行。
  2. 进程 p = Runtime.getRuntime().exec("cmd java -jar D:\chat.jar -n 0");
    您应该使用这样的参数数组调用 exec:

    进程 p = Runtime.getRuntime().exec(new String[]{"cmd","java -jar D:\chat.jar -n 0"});

它将清除要运行的程序和参数。

于 2010-11-10T14:07:38.523 回答
0
 import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;
import java.util.*;
import org.xsocket.*;
import org.xsocket.connection.*;

public class xSocketDataHandler implements IDataHandler, IConnectHandler, IDisconnectHandler
{
    private Set<INonBlockingConnection> sessions = Collections.synchronizedSet(new HashSet<INonBlockingConnection>());

    public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException
    {
        try
        {
            String data = nbc.readStringByDelimiter("\0");
            //if(data.trim().length() > 0)
            //{
                //System.out.println("Incoming data: " + data);

                //nbc.write("+A4\0");
                /*
                if(data.equalsIgnoreCase("<policy-file-request/>"))
                {
                    nbc.write("<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"8090\"/></cross-domain-policy>\0");
                    return true;
                }
                 */
                //String[] message = data.split("~");
                String message = data;
                sendMessageToAll(message);

                //if(message.equalsIgnoreCase("SHUTDOWN"))
                  //  xSocketServer.shutdownServer();
            //}
        }
        catch(Exception ex)
        {
         System.out.println("onData2: " + ex.getMessage());
        }

        return true;
    }

    private void sendMessageToAll(String message)
    {
        try
        {
            synchronized(sessions)
            {
                Iterator<INonBlockingConnection> iter = sessions.iterator();

                while(iter.hasNext())
                {
                    INonBlockingConnection nbConn = (INonBlockingConnection) iter.next();

                    if(nbConn.isOpen())
                     nbConn.write(message+"\0");
                }
            }

            //System.out.println("Outgoing data: " + message);
        }
        catch(Exception ex)
        {
            System.out.println("sendMessageToAll: " + ex.getMessage());
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////
    public boolean onConnect(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, MaxReadSizeExceededException
    {
        try
        {
            synchronized(sessions)
            {
                sessions.add(nbc);
            }

            //System.out.println("onConnect");
        }
        catch(Exception ex)
        {
            System.out.println("onConnect: " + ex.getMessage());
        }

        return true;
    }

    public boolean onDisconnect(INonBlockingConnection nbc) throws IOException
    {
        try
        {
            synchronized(sessions)
            {
                sessions.remove(nbc);
            }

            //System.out.println("onDisconnect");
        }
        catch(Exception ex)
        {
            System.out.println("onDisconnect: " + ex.getMessage());
        }

        return true;
    }
}
于 2010-11-10T15:01:38.000 回答
0

也许这就是你的意思?

public class XServer implements IDataHandler {

 /**
  * @param args
  * @throws IOException 
  * @throws UnknownHostException 
  */
 public static void main(String[] args) throws UnknownHostException, IOException {
  IServer server = new Server(8090,new XServer());
  server.start();
 }

 @Override
 public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException,
   ClosedChannelException, MaxReadSizeExceededException {
  String data = nbc.readStringByDelimiter("\r\n");
  nbc.write(data + "\r\n");

  System.out.println("setup exec");
        Process p = Runtime.getRuntime().exec(new String[]{"cmd","java -jar D:\\chat.jar -n 0"});
        try {
   p.waitFor();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
        System.out.println("call exec");


  return true;
 }

}

在端口 8090 上 telnet 到您的本地主机,输入一行文本,服务器将执行您的聊天程序。

于 2010-11-10T14:40:07.353 回答