日期:2014-05-17 浏览次数:20712 次
import java.net.*;
import java.io.*;
/**
* UDPSender is an implementation of the Sender interface, using UDP as the transport protocol.
* The object is bound to a specified receiver host and port when created, and is able to
* send the contents of a file to this receiver.
*
* @author Alex Andersen (alex@daimi.au.dk)
*/
public class TCPSender implements Sender{
private File theFile;
private FileInputStream fileReader;
private Socket s;
private int fileLength, currentPos, bytesRead, toPort, length;
private byte[] msg, buffer;
private String toHost,initReply;
private InetAddress toAddress;
private OutputStream theOutstream;
private InputStream theInstream;
/**
* Class constructor.
* Creates a new UDPSender object capable of sending a file to the specified address and port.
*
* @param address the address of the receiving host
* @param port the listening port on the receiving host
*/
public TCPSender(InetAddress address, int port) throws IOException{
toPort = port;
toAddress = address;
msg = new byte[8192];
buffer = new byte[8192];
s = new Socket(toAddress, toPort);
theOutstream =&