我有这样的代码:
interface Employee
{
string getLastname();
};
#include "Employee.idl"
interface Work
{
Employee getEmployee(in short id);
};
服务器文件:
#include "Employee.hh"
class EmployeeImpl : public POA_Employee
{
private:
char* lastname;
int id;
public:
EmployeeImpl(const char* lastname, int id);
char* getLastname();
};
#include "EmployeeImpl.h"
EmployeeImpl::EmployeeImpl(const char* lastname, int id)
{
this->lastname = const_cast<char*>(lastname);
this->id = id;
}
char* EmployeeImpl::getLastname()
{
return this->lastname;
}
#include "Work.hh"
#include <vector>
#include "EmployeeImpl.h"
using namespace std;
class WorkImpl : public POA_Work
{
private:
vector<EmployeeImpl> employees;
public:
WorkImpl();
Employee_ptr getEmployee(::CORBA::Short id);
};
#include "WorkImpl.h"
WorkImpl::WorkImpl()
{
EmployeeImpl ei1("Doe", 1);
EmployeeImpl ei2("Smith", 2);
EmployeeImpl ei3("Brown", 3);
employees.push_back(ei1);
employees.push_back(ei2);
employees.push_back(ei3);
}
Employee_ptr WorkImpl::getEmployee(::CORBA::Short id)
{
return employees[id]._this();
}
客户端文件:
import java.util.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
public class Client
{
public static void main(String [] args)
{
try
{
org.omg.CORBA.ORB clientORB = org.omg.CORBA.ORB.init(args, null);
if (clientORB == null)
{
System.out.println("Problem while creating ORB");
System.exit(1);
}
org.omg.CORBA.Object objRef = clientORB.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
Work work = WorkHelper.narrow(ncRef.resolve_str("WorkService"));
Employee e = work.getEmployee((short)1);
System.out.println(e.getLastname());
e = work.getEmployee((short)2);
System.out.println(e.getLastname());
e = work.getEmployee((short)3);
System.out.println(e.getLastname());
}catch(Exception e){ System.out.println(e.getMessage()); }
}
}
当我在客户端运行服务器,然后是客户端时,我看到:
史密斯
代替:
Doe Smith Brown
当客户端收到消息时,在服务器端我看到:
分段错误(应付转储)
和服务器崩溃。我的代码有什么问题?我在 Kubuntu 上使用 omniORB 和 idlj,并使用 g++ 和 javac 来编译我的文件。