2014-11-08 239 views
0

我试图从发送方发送文本文件到接收方,但是在发送方我得到连接被拒绝:连接。我在接收端使用本地主机地址,并在发送端提示时手动输入。发件人类中的sendChannel.connect(地址)发生错误。连接被拒绝:连接错误

发件人类:

public static void startProcess(){ 
    SocketChannel sendChannel = null; 
    RandomAccessFile f = null; 
    Scanner scan = new Scanner(System.in); 
    SocketAddress address = null; 
    try{ 

     sendChannel = SocketChannel.open(); // open the channel 
     //DatagramSocket socket = dChannel.socket(); 
     boolean validAddr = false; 
     while(validAddr != true){ 
      try{ 
       System.out.println("Enter in valid server IP Address"); 
       address = new InetSocketAddress(scan.nextLine(),7777); 
       validAddr = true; 
      } 
      catch(Exception e){ 
       System.out.println("Invalid!"); 
       System.out.println(e.getMessage()); 
      } 
     } 
     //System.out.println("Address: " + InetAddress.getLocalHost().getHostAddress()); 
     sendChannel.connect(address); 


     File i = new File("./data.txt"); 
     f = new RandomAccessFile(i,"r"); 
     FileChannel fChannel = f.getChannel(); 
     ByteBuffer bBuffer = ByteBuffer.allocate(1024); //set buffer capacity to 1024 bytes 
     while (fChannel.read(bBuffer) > 0) { 
      //SocketAddress client = dChannel.receive(bBuffer); //receive the datagram 

      bBuffer.flip(); //Set limit to current position 
      sendChannel.write(bBuffer); 
      //dChannel.send(bBuffer, client); //send the datagram using channel 
      bBuffer.clear(); //Get ready for new sequence of operations 
     } 
     Thread.sleep(1000); 
     System.out.println("End of file reached"); 
     sendChannel.close(); 
     f.close(); 


    } 
    catch(Exception e){ 
     System.out.println(e.getMessage()); 
    } 
} 

接收侧:

public static void startProcess(){ 
    Scanner scan = new Scanner(System.in); 
    ServerSocketChannel serverChannel = null; 
    SocketChannel chan = null; 
    RandomAccessFile file = null; 
    try{ 

     serverChannel = ServerSocketChannel.open(); 
     //Read in a valid IP Address 
     boolean val2 = false; 
     int tempNum = 0; 
     for (int portNUM = 7777 ;!val2; portNUM++){ 
      try { 
       serverChannel.socket().bind(new InetSocketAddress("localhost", portNUM)); 
       tempNum = portNUM; 
       val2 =true; 
      } catch (IOException e) { 
       System.out.println("Error!"); 
      } 
     } 

     System.out.println(InetAddress.getLocalHost().getHostAddress()); 
     System.out.println("Port Number: " + tempNum); 


     chan = serverChannel.accept(); 
     System.out.println("Connected!"); 
     chan.getRemoteAddress(); 

     file = new RandomAccessFile("./output.txt","rw"); 
     ByteBuffer buff = ByteBuffer.allocate(1024); 
     FileChannel receiveChannel = file.getChannel(); 
     while(chan.read(buff) > 0){ 
      buff.flip(); 
      receiveChannel.write(buff); 
      buff.clear(); 

     } 
     // buff.put((byte)65); 
     //buff.flip(); 
     Thread.sleep(1000); 
     receiveChannel.close(); 
     System.out.println("End of file"); 

     chan.close(); 
    } 
    catch(Exception e){ 
     System.out.println(e.getMessage()); 
    } 

} 
+0

该港口是开放的吗?你有通过浏览器访问它吗? http:// localhost:7777 – Soley 2014-11-09 00:11:38

+0

@Salivan我测试了一个旧程序的端口,它的工作,但我只是点击你的链接,它说该页面不能显示 – user3339242 2014-11-09 00:18:56

+0

是你的发件人与你的接收器在同一主机? – 2014-11-09 00:41:29

回答

0

我计算出来我使用本地主机错误地在接收器侧。我将其更改为serverChannel.socket()。bind(new InetSocketAddress(portNUM));

相关问题