JAVA程序调用非JAVA程序 - C源码(操作list),JAVA(服务器)
JAVA程序调用非JAVA程序 --- C源码(操作list),JAVA(服务器)
//此程序被NameCollector.java在服务器端管理邮件列表时所调用 #include <stdio.h> #include <stdlib.h> #include <string.h> #define BSIZE 250 //定义全局变量 //检查email地址是否已经存在 int alreadyInList(FILE* list ,char* name){ char buf[BSIZE];//定义局部变量buf fseek(list , 0 , SEEK_SET);//从文件开头开始重定位文件流的指针 //将文件流中已经接收到的数据读取到buf中直到文件结尾 while(fgets(buf , BSIZE , list)){ char * newline = strchr(buf,'\n');//找出换行符 if(newline != 0){ *newline = '\0';//给字符串添加结束符号 } if(strcmp(buf,name) == 0){ return 1; } } return 0; } //主函数 int main(){ char buf[BSIZE]; FILE* list = fopen("emlist.txt","a+t");//追加并打开指定文件(如果不存在该文件则创建文件) if(list == 0){ perror("could not open emlist.txt");//提示错误信息 exit(1); } while(1){ gets(buf);//从标准流中获取输入 (重点) if(alreadyInList(list,buf)){ //检查输入信息是否已经存在 printf("Already in list:%s",buf); fflush(stdout); //清除文件缓冲区 }else{ fseek(list , 0 ,SEEK_END);//重定向流指针 fprintf(list,"%s\n",buf);//将email地址以指定格式添加到list中 fflush(list);//清除缓冲区 printf("%s added to list",buf);//打印信息 fflush(stdout); //清除缓冲 } } }
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.PrintStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class NameCollector { public static final int COLLECTOR_PORT = 8080;//服务器端口号 public static final int BUFFER_SIZE = 1000;//数据包大小 byte[] buf = new byte[BUFFER_SIZE]; DatagramPacket dbPacket = new DatagramPacket(buf, buf.length);//构造一个能够接收长度为1000字节的数据包 DatagramSocket socket;//套接字 Process listmgr; //进程 PrintStream nameList; DataInputStream addResult; public NameCollector(){ /** * 初始化流 */ try { listmgr = Runtime.getRuntime().exec("listmgr.exe");//java允许我们访问任何执行模块,只要其能从标准输入中读写 ---- 调用c程序 nameList = new PrintStream(new BufferedOutputStream(listmgr.getOutputStream()));//获得子进程的输入流 addResult = new DataInputStream(new BufferedInputStream(listmgr.getInputStream())); } catch (Exception e) { System.out.println("Can not start listmgr.exe"); e.printStackTrace(); System.exit(1); } /** * 初始化套接字 */ try { socket = new DatagramSocket(COLLECTOR_PORT);//打开套接字 System.out.println("NameCollector Server started"); while(true){ socket.receive(dbPacket);//开始接收数据包 String addStr = new String(dbPacket.getData(),0,dbPacket.getLength()); nameList.println(addStr);//将数据发送到标准输入 nameList.flush();//清空缓存(很重要) byte[] resultBuf = new byte[BUFFER_SIZE]; int byteCount = addResult.read(resultBuf);//将输入流中的信息保存到缓冲区 if(byteCount != -1){ String result = new String(resultBuf,0,resultBuf.length).trim();//收集数据包信息 InetAddress senderAddress = dbPacket.getAddress();//得到数据包地址 int senderPort = dbPacket.getPort();//得到端口 byte[] echoBuf = new byte[BUFFER_SIZE]; echoBuf = result.getBytes();//将数据包信息保存到新的字节数组中 DatagramPacket echo = new DatagramPacket(echoBuf, echoBuf.length,senderAddress,senderPort);//重新封装数据包准备返回给请求客户端 socket.send(echo);//返回给客户端 }else{ System.out.println("Unexpected lack of result from" + "listmgr.exe"); } } } catch (SocketException e) { System.out.println("Can not open socket"); System.exit(1); }catch (IOException e) { System.out.println("Coummunication error"); e.printStackTrace(); } } /** * 启动服务器 * @param args */ public static void main(String[] args) { new NameCollector(); } }