⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hylafaxserver.java

📁 tapi的java封装
💻 JAVA
字号:
/*
*	JTAPI library copyright 1998 by Web4Groups consortium (http://Web4Groups.at)
*/

import java.util.*;
import java.io.*;
import java.net.*;

public class HylafaxServer extends Thread{

	int port; // the port number on which the server is listenning for connection
	int id=0; // id for connection

	
	public HylafaxServer(int port){
		super();
		this.port=port;
	}

	synchronized int nextId(){
		id++;
		return id;
	}

/**
Thread method
*/	
	public void run(){
		try{
		ServerSocket s=new ServerSocket(port);
		while(true){
			Socket client=s.accept();
			ClientHandler h=new ClientHandler(client,nextId());
		
		
		}
		}catch(Exception e){
			System.out.println("Exception catched");
			e.printStackTrace();
		}
	}
	
	public static void main(String args[]){
		if(args.length!=1){
			System.out.println("Usage: java HylafaxServer <port>");
			System.exit(0);
		}
	
		int p=ClientHandler.getInt(args[0]);
		
		HylafaxServer server=new HylafaxServer(p);
		server.start();
	}
	
}


class ClientHandler extends Thread{
	Socket s; // communication socket
	int id; // id for the file
	ClientHandler(Socket s,int id){
		super();
		this.s=s;
		this.id=id;
		start(); // start the thread
	}

	native void sendToHyla(int sendCmd, String dest, String fileName); 
	
	public void run(){

		String dest = null;
		String cmd = null;

		try{
		InputStream in=s.getInputStream();
		String header=readLine(in);
		int l=getInt(header);
		System.out.println("Should read ("+id+"): "+l+" Bytes");
		cmd = readLine(in);	
		dest = readLine(in);
		int i;
		FileOutputStream out=new FileOutputStream("hylafax_data_"+id);
		byte buf[]=new byte[16584];
		int summe=0;
		int num;
		while(summe<l){
			if((l-summe)>16584)
				i=16584;
			else
				i=l-summe;
				
			num=in.read(buf,0,i);
			summe+=num;
			out.write(buf,0,num);
		}
		out.close();
		s.close();
		}catch(Exception exc){
			System.out.println("Exception catched");
			exc.printStackTrace();
		}
		System.out.println("Done ("+id+")");

		int sendCmd;
		if (cmd.equals("sendfax"))
			sendCmd = 0;
		else sendCmd = 1;
		sendToHyla(sendCmd, dest, "hylafax_data_"+id);
	}
	
	public static int getInt(String value){
		if(value==null)
			return -1;
		int res=-1;
		try{
			res=Integer.parseInt(value);
		
		}catch(NumberFormatException e){
			// ignore it
		}
	
		return res;
	}
	
	public static String readLine(InputStream stream){
		StringBuffer buffer=new StringBuffer();
		int temp;
		//System.out.println("Read Line");
		do{
			try {
				temp=stream.read();
			} catch(Exception e) {
				return(null);
			}
			if(temp==-1){
				if(buffer.length()>0)
					return(buffer.toString());
				else
					return null;
			}
			if(temp!='\n')
					buffer.append((char)temp);
				
		} while(temp!='\n');
		
		
		return(buffer.toString());
	}

	static {
		System.loadLibrary("fax");
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -