Hi, >>>> I want to use a COM object from java, >>>> but I don't know how?You can find J-Integra which is a bi-directional pure Java-COM bridge from http://www.linar.com/ Using these bridges you can access COM object from java and java from COM objects.Hope this helps.Good Luck.Gayam.Srinivasa Reddy Developer Technical Support Sun Micro Systems http://www.sun.com/developers/support/ ----------------digest from forum.java.sun.com
Hi, >>>> I want to use a COM object from java, >>>> but I don't know how?You can find J-Integra which is a bi-directional pure Java-COM bridge from http://www.linar.com/ Using these bridges you can access COM object from java and java from COM objects.Hope this helps.Good Luck. ---------------------摘自forum.java.sun.com
我比较喜欢weblogic的jcom,功能非常强大。1。这个无能为力,我一直觉得什么事可以问别人但不能抄袭。
2、这方面我就无能为力了,我对COM一点都不了解的说Java中的网络编程是一个很重要的部分,也是其编程优越性的地方之一。在Java中有一个专门的Java.net类库来管理网络编程的有关方法。
下面先介绍在Java中怎样用socket进行客户与服务器通信。最后再介绍一个一个最简单的通话程序。
一.怎样用socket进行客户与服务器通信
在Java中用socket进行客户/服务器之间的通信编程。Socket是两个实体之间进行通信的有效端点。通过socket可以获得源IP地址和源端口、终点IP地址和终点端口。用户可以将多个socket连入同一个端口,以便对于单个端口可以有多个连接。通过socket客户/服务器编程可以创建一个能被许多人使用的分布式程序,并且所有客户均可以用统一的前端进行工作,并与服务器进行通信。
要想与服务器通信必须具备三个条件:服务器程序、客户程序和连接它们的socket程序。这三个部分缺一不可。但是,客户与服务器之间的通信有很多的方式,其中另一个方法是把客户作为索取者,把服务器作为给予者。下面我们看一看Java的服务器编程。
在Java中,服务器有3个主要的功能:
1.在Java.net类库中通过构造一个ServerSocket类的实例使服务器能够检测到指定端口的信息。用ServerSocke中的accept()方法可以使服务器检测到指定端口的活动。另外,服务器还负责检测要求与它连接的客户。
·Socket类的实例代表客户与服务器连接成功。通过编程可以使多个用户通过同一个端口与服务器相连,其中都是Socket 类的实例。
2.可以分别用Socket类的getInputStream()和getOutStream()方法来发送和捕捉数据。其使用方法如下: try{
ServerSocket myServerSocket=new ServerSocket(100);
Socket my100Socket=myServerSocket.accept();
}catch(Exception e){} 在上面的代码中,首先构造一个ServerSocket类的实例,并传递给它一个整数作为服务器指定可以使用的给定端口,如下: ServerSocket myServerSocket=new ServerSocket(100); 在Java程序中如果每次构造ServerSocket时都能保持捕捉异常事件,则就随时指定了准备使用的端口。下面的代码使用accept()方法来检测端口的活动。 Socket my100Socket=myServerSocket.accept(); Accept()方法直到接收到用户的连接请求,才继续执行中断的执行程序。一旦客户的连接成功,my100Socket就代表该连接,并且可以发送和接收数据。
最后,我们看一看客户是怎样请求连接的。其连接方法如下: try{
Socket mySocket=new Socket("www.cpcw.com",100);
}catch(Exception e ){} 通过上面的代码可能看出,也是通过Socket类来实现的。下面我们通过一个网络编程的实例来说明如何进行网络通信。 二.一个最简单的通话程序 通话器服务器:
import java.net.*;
import java.io.*;
import java.lang.*; public class myserver{
public static void main(String args[]){
ServerSocket server;
Socket socket;
String s;
InputStream Is;
OutputStream Os;
DataInputStream DIS;
PrintStream PS; try{
//在端口4321注册服务
server=new ServerSocket(4321);
socket=server.accept();//监听窗口,等待连接 System.out.println("server ok");
System.out.println("************************************************");
System.out.println(""); //获得对应Socket的输入/输出流
Is=socket.getInputStream();
Os=socket.getOutputStream();
//建立数据流
DIS=new DataInputStream(Is);
PS=new PrintStream(Os);
DataInputStream in=new DataInputStream(System.in);
while(true){
System.out.println("");
System.out.println("please wait client's message...");
System.out.println("");
s=DIS.readLine(); //读入从client传来的字符串
System.out.println("client said:"+s); //打印字符串
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出
System.out.print("you say:");
s=in.readLine(); //读取用户输入的字符串
PS.println(s); //将读取得字符串传给client
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 } //关闭连接
DIS.close(); //关闭数据输入流
PS.close(); //关闭数据输出流
Is.close(); //关闭输入流
Os.close(); //关闭输出流
socket.close(); //关闭sockey
}
catch(Exception e){
System.out.println("Error:"+e);
}
}
}
通话器客户端
import java.net.*;
import java.io.*;
import java.lang.*; public class myclient{
public static void main(String args[]){
if (args.length<1){ //判断命令加参数没有
System.out.println("you forget the name of the server!");
System.out.println("see also: myclient yxf");
System.exit(1); //如果没加参数就退出
} Socket socket;
String s="[email protected]";
String len;
InputStream Is;
OutputStream Os;
DataInputStream DIS;
PrintStream PS;
try{
//向主机名为args[0]的服务器申请连接
//注意端口号要与服务器保持一致:4321
socket=new Socket(args[0],4321); System.out.println("client ok");
System.out.println("************************************************");
System.out.println(""); //获得对应socket的输入/输出流
Is=socket.getInputStream();
Os=socket.getOutputStream();
//建立数据流
DIS=new DataInputStream(Is);
PS=new PrintStream(Os);
DataInputStream in=new DataInputStream(System.in); while(true){
System.out.print("you say:");
s=in.readLine(); //读取用户输入的字符串
PS.println(s); //将读取得字符串传给server
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出
else
{
System.out.println("");
System.out.println("please wait server's message...");
System.out.println("");
}
s=DIS.readLine(); //从服务器获得字符串
System.out.println("server said:"+s); //打印字符串
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 } //关闭连接
DIS.close(); //关闭数据输入流
PS.close(); //关闭数据输出流
Is.close(); //关闭输入流
Os.close(); //关闭输出流
socket.close(); //关闭socket
}
catch(Exception e){
System.out.println("Error:"+e);
}
}
}
则是一个C\C++和java的接口,可以放你任何代码。
至于,COM我不熟。
可以看一看java tutorial,内有详细说明。
>>>> but I don't know how?You can find J-Integra which is a bi-directional
pure Java-COM bridge from http://www.linar.com/
Using these bridges you can access COM object from
java and java from COM objects.Hope this helps.Good Luck.Gayam.Srinivasa Reddy
Developer Technical Support
Sun Micro Systems
http://www.sun.com/developers/support/
----------------digest from forum.java.sun.com
ver 1.0 ddxxkk */
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
class server8888
{
public static void main(String[] args)
{
System.out.println("now start server!");
new serverMain(8888).start();
}
} class serverMain extends Thread //服务器主线程
{
public int openPort;
// Socket clinet; public serverMain (int portn)
{
this.openPort=portn;
}
public void run()
{
OutputStream out=null;
InputStream in=null;
ServerSocket servet=null;
Socket client=null;
serverMin cmin=null;
try
{
servet=new ServerSocket(openPort);
System.out.println("now server start at port:"+openPort);
}
catch ( IOException e) {System.out.println("server make error");return; } try
{
System.out.println("now server start accect");
while (true)
{ client=servet.accept();
cmin=new serverMin(client);
if (cmin.err==0)
{
cmin.start();
}
} }
catch ( IOException e) {System.out.println("server accept error");return; } }
}
class serverMin extends Thread //服务器客户线程
{
public int err=0;
String mm="ISO8859_1";
private Socket client;
private OutputStream out=null;
private InputStream in=null;
public serverMin(Socket inforMax)
{
try {
this.client=inforMax;
this.in=this.client.getInputStream();
this.out=this.client.getOutputStream();
}
catch ( IOException e) {this.err=1;}
}
public void run()
{
boolean todo=true;
int inb;
StringBuffer inputs=new StringBuffer();
String cmds;
String cmdss;
int inyesno;
try {
System.out.println("client serverMin ip="+client.getInetAddress()+" port="+client.getPort());
String sss=" server8888 为你服务 v1.0";
if (File.separator.equals("\\") ){out.write(" notice: 系统服务是 windows".getBytes());mm="GB2312";}
else {out.write(" notice: 系统服务是 unix".getBytes());}
out.write(13); //回车
out.write(10); //换行
out.write(sss.getBytes());
out.write(13); //回车
out.write(10); //换行
out.flush();
while (todo)
{ inyesno=in.read();
if (inyesno!=-1)
{
// inputs.setLength(0);
byte inc[]=new byte[in.available()+1];
int n=1;
inc[0]=(byte)inyesno; while (in.available()>0)
{ inb=in.read();
inc[n]=(byte)inb;
n++; }
inputs.setLength(0);
inputs.append(new String(inc));
cmds=inputs.toString().toLowerCase().trim();
/* System.out.println(cmds);
out.write(inc);
out.write("输入OK".getBytes());
out.write(13); //回车
out.write(10); //换行
*/
if (cmds.equals("exit") )
{
System.out.println("clent is input exit");
todo=false;
break; }
if (cmds.length()>4) //dir
{
if (cmds.substring(0,4).equals("dir ") )
{
cmddir(cmds.substring(4,cmds.length()) );
continue;
} }
if (cmds.length()>5) //view
{
if (cmds.substring(0,5).equals("view ") )
{ out.write(cmdview(cmds.substring(5,cmds.length())).getBytes());
out.write(13); //回车
out.write(10); //换行
out.flush();
continue;
} } if ( cmds.equals("help") || cmds.equals("?"))
{
out.write("?".getBytes());
out.write(13); //回车
out.write(10); //换行 out.write("help".getBytes());
out.write(13); //回车
out.write(10); //换行
out.write("dir path name ".getBytes());
out.write(13); //回车
out.write(10); //换行
out.write("view path and file name".getBytes());
out.write(13); //回车
out.write(10); //换行
out.flush();
continue;
}
out.write("Bad command ".getBytes());
out.write(13); //回车
out.write(10); //换行
}
else {
todo=false;
System.out.println("clent is err close");
}
} in.close();
out.close();
client.close();
// System.out.println(in.read());
}
catch ( IOException e) {
System.out.println("server accept error");
try
{
in.close();
out.close();
client.close();
}
catch (IOException ee)
{
} }
}
private int cmddir (String cmdsin) // dir 命令的处理 -1是对方关服务器,0为正常,1 为IOException e
{ String dirs=cmdsin;
try{
try
{
File dir=new File(dirs);
String listing[]=dir.list();
for( int i=0;i {
out.write(listing[i].getBytes());
out.write(13); //回车
out.write(10); //换行
}
}
catch (NullPointerException e)
{
out.write("this no".getBytes());
out.write(13); //回车
out.write(10); //换行
} }
catch ( IOException e) {return 1; }
return 0; } private String cmdview(String cmdsin)
// view 命令的处理 -1是对方关服务器,0为正常,1 为IOException e
{
String filenames=cmdsin;
FileInputStream outfi;
File file=new File(filenames);
if (!file.isFile())
{ return "this is file can`t find ";
}
if (!file.canRead())
{ return "this is file can`t read ";
}
try {outfi=new FileInputStream(file); }
catch (FileNotFoundException e) {return "file open erro";}
try
{
byte outfib[]=new byte[outfi.available()];
outfi.read(outfib); String conns=new String(outfib);
return conns;
}
catch (IOException e)
{
return "Io error";
} }
}
try
{
String command = "notepad.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch(IOExceptin ex){}java里的com其实应该是javabean形势的
>>>> but I don't know how?You can find J-Integra which is a bi-directional
pure Java-COM bridge from http://www.linar.com/
Using these bridges you can access COM object from
java and java from COM objects.Hope this helps.Good Luck. ---------------------摘自forum.java.sun.com