JAVA 프로그램 호출 비 JAVA 프로그램-C 소스 코드(조작 목록),JAVA(서버)

4637 단어 자바
//    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,'
');// 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
",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();
	}
}

좋은 웹페이지 즐겨찾기