gpt4 book ai didi

java - "BindException: Address already in use"在同一台电脑上使用java套接字传输文件时出现异常

转载 作者:行者123 更新时间:2023-12-01 09:48:50 25 4
gpt4 key购买 nike

我对java网络编程非常陌生。我正在尝试编写一个可以通过网络传输文件的程序。我为发送者和接收者编写了两个单独的程序。并在我的电脑的两个单独的 Intellij 窗口中运行这两个程序。我在服务器套接字和接收器套接字中放置了相同的端口号。 IP 地址是本地主机。当我运行这两个程序时,这两个程序中的任何一个都会抛出异常。

经过一番谷歌搜索后,我发现 Java sockets: multiple client threads on same port on same machine?问题是人们说在同一端口运行两个程序是合法的。我还没有在两台不同的 PC 上测试此代码。

所以我的问题是我错过了什么?我可以在我的电脑上同时运行这两个程序吗?

这是我编写的代码 -

发件人--

public class Sender {
public static void main(String[] args) throws IOException
{
String fileLocation;
int portNo;
portNo = 6000;
fileLocation = "/files/A.cpp";
Sender.send(portNo,fileLocation);
}
public static void send(int portNo,String fileLocation) throws IOException
{

FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;

OutputStream outputStream = null;
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(portNo);
System.out.println("Waiting for receiver...");
try {
socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);

File file = new File (fileLocation);
byte [] byteArray = new byte [(int)file.length()];
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(byteArray,0,byteArray.length); // copied file into byteArray

//sending file through socket
outputStream = socket.getOutputStream();
System.out.println("Sending " + fileLocation + "( size: " + byteArray.length + " bytes)");
outputStream.write(byteArray,0,byteArray.length);
outputStream.flush();
System.out.println("Done.");
}
finally {
if (bufferedInputStream != null) bufferedInputStream.close();
if (outputStream != null) bufferedInputStream.close();
if (socket!=null) socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (serverSocket != null) serverSocket.close();
}
}
}

对于接收者 --

public class Receiver {

public static void main (String [] args ) throws IOException {


String fileLocation,ipAddress;
int portNo;
ipAddress = "localhost";
portNo = 6000;
fileLocation = "/files/A.cpp";
Receiver.receiveFile(ipAddress, portNo, fileLocation);


}
public static void receiveFile(String ipAddress,int portNo,String fileLocation) throws IOException
{
int bytesRead=0;
int current = 0;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
Socket socket = null;
try {
socket = new Socket(ipAddress,portNo);
System.out.println("connected.");

byte [] byteArray = new byte [6022386];
System.out.println("Please wait downloading file");

InputStream inputStream = socket.getInputStream();
fileOutputStream = new FileOutputStream(fileLocation);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bytesRead = inputStream.read(byteArray,0,byteArray.length);

current = bytesRead;
do {
bytesRead =inputStream.read(byteArray, current, (byteArray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bufferedOutputStream.write(byteArray, 0 , current);

System.out.println("File " + fileLocation + " downloaded ( size: " + current + " bytes read)");
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (fileOutputStream != null) fileOutputStream.close();
if (bufferedOutputStream != null) bufferedOutputStream.close();
if (socket != null) socket.close();
}
}
}

谢谢。

最佳答案

您的接收器不需要指定要使用的端口,将其留给操作系统(它会自动分配一个空闲临时端口号;端口号并不重要)。

而是使用 Socket.connect(SocketAddress) 方法将客户端套接字连接到服务器。使用服务器的 IP/名称加上所使用的端口号来指定套接字地址,例如:

InetAddress iadr = InetAddress.getByName("localhost");
SocketAddress sadr = new InetSocketAddress(iadr, portNo);
Socket socket = new Socket();
socket.connect(sadr);

这是您通常通过指定端口连接到服务器的方式。未明确指定本地(客户端)端点。

当然,服务器必须已经运行才能成功。

编辑:由于客户端端点是自动分配的,操作系统将为每个客户端分配一个空闲端点,因此您可以在同一台计算机上运行多个客户端(接收器)而不会发生任何冲突。

关于java - "BindException: Address already in use"在同一台电脑上使用java套接字传输文件时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37752623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com