所以我有一个服务器客户端程序,可以进行屏幕捕获并在屏幕上显示。 我想在主要部分验证我要连接的端口是否正在使用,并打印一些相关的消息..
服务器:
package test;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.sql.SQLException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Server extends Thread
{
private ServerSocket serverSocket;
Socket server;
public Server(int port) throws IOException, SQLException, ClassNotFoundException, Exception
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(180000);
}
public void run()
{
while(true)
{
try
{
server = serverSocket.accept();
BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(server.getInputStream()));
JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);
}
catch(SocketTimeoutException st)
{
System.out.println("Socket timed out!");
break;
}
catch(IOException e)
{
e.printStackTrace();
break;
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
// private static boolean isLocalPortInUse(int port) {
// try {
// // ServerSocket try to open a LOCAL port
// new ServerSocket(port).close();
// // local port can be opened, it's available
// return false;
// } catch(IOException e) {
// // local port cannot be opened, it's in use
// return true;
// }
// }
public static void main(String [] args) throws IOException, SQLException, ClassNotFoundException, Exception
{
int port = 9000;
Thread t = new Server(port);
t.start();
}
}
客户端:
package test;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.Socket;
import javax.imageio.ImageIO;
public class Client
{
static BufferedImage img;
byte[] bytes;
public static void main(String [] args)
{
String serverName = "localhost";
int port = 10001;
try
{
Socket client = new Socket(serverName, port);
Robot bot;
bot = new Robot();//clasa Robot ajuta la creare capturii de ecran
img = bot.createScreenCapture(new Rectangle(0, 0, 500, 500));//scalarea imaginii; schimba dimensiunea screen shotului
ImageIO.write(img,"JPG",client.getOutputStream());
System.out.println("The image is on the screen!Yey!");
client.close();
} catch(IOException | AWTException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
如果客户端和服务器位于同一个框中,则可以使用绑定到现有端口的策略并检查特定的异常。
public static void main(String...args) throws IOException {
int port = 31999; // for demonstration purposes only.
boolean serverRunning = isServerRunning(port);
System.out.printf("Server running: %s\n", serverRunning);
new ServerSocket(port); // start the server
serverRunning = isServerRunning(port);
System.out.printf("Server running: %s\n", serverRunning);
}
private static boolean isServerRunning(int port) throws IOException {
try(ServerSocket clientApp = new ServerSocket(port)) {
return false;
} catch (java.net.BindException e) {
return true;
}
}
如果服务器位于另一个方框(最有可能是这种情况),那么您可以遵循类似的策略,而不是寻找ConnectException
public static void main(String...args) throws IOException {
int port = 31999; // for demonstration purposes only.
boolean serverRunning = isServerRunning("localhost", port);
System.out.printf("Server running: %s\n", serverRunning);
new ServerSocket(port); // start the server
serverRunning = isServerRunning("localhost", port);
System.out.printf("Server running: %s\n", serverRunning);
}
private static boolean isServerRunning(String address, int port) throws IOException {
try(Socket clientApp = new Socket(address, port)) {
return true;
} catch (java.net.ConnectException e) {
return false;
}
}