Java 连接被拒绝:连接错误
我正试图将一个文本文件从发送者发送到接收者,但是在发送者端,我的连接被拒绝:connect。我在接收方使用本地主机地址,在发送方提示时手动输入。此错误发生在发件人类中的sendChannel.connectaddress处 发件人类别:Java 连接被拒绝:连接错误,java,sockets,networking,serversocket,socketchannel,Java,Sockets,Networking,Serversocket,Socketchannel,我正试图将一个文本文件从发送者发送到接收者,但是在发送者端,我的连接被拒绝:connect。我在接收方使用本地主机地址,在发送方提示时手动输入。此错误发生在发件人类中的sendChannel.connectaddress处 发件人类别: public static void startProcess(){ SocketChannel sendChannel = null; RandomAccessFile f = null; Scanner scan = new Scan
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());
}
}
我发现我在接收方错误地使用了本地主机。我将其更改为serverChannel.socket.bindnew InetSocketAddressportNUM 那个港口是开放的,对吗?您是否可以通过浏览器访问它@Slaven我用一个旧程序测试了这个端口,它工作了,但我只是点击了你的链接,它说这个页面不能显示。你的发送者和接收者在同一台主机上吗?