【发布时间】:2014-08-12 15:35:34
【问题描述】:
我正在尝试创建一个程序来确定服务器是连接、关闭还是断开连接。 我需要让客户端在打开时自动连接到服务器。
问题:
1. How can I continuously ping the server, to determine if the server is up?
2. Why is it when I click the button the server can only receive once.
3. Determine when the Server is disconnected
这是我的代码:
服务器
public Server()
{
super("Server");
server.setLayout(new BorderLayout());
main.setLayout(new BorderLayout());
top.setLayout(new BorderLayout());
bot.setLayout(new BorderLayout());
console.setEditable(false);
console.setFont(new Font("Courier New",Font.PLAIN,14));
console.setBackground(Color.BLACK);
console.setForeground(Color.WHITE);
bot.add(console);
top.add(btnReply,BorderLayout.EAST);
top.add(queryline,BorderLayout.CENTER);
main.add(top,BorderLayout.NORTH);
main.add(bot,BorderLayout.CENTER);
add(main);
}
private void runServer() throws IOException
{
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
console.setText("Server is Up and listening to the port: "+port+"\n");
System.out.println("Server is Up and listening to the port: "+port+"\n");
while(true)
{
Thread thread = new Thread(new Handler(socket));
thread.start();
}
}
public static void main(String args[])
{
Server f=new Server();
f.setVisible(true);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
f.runServer();
}catch(IOException ex)
{
//JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
@Override
public void actionPerformed(ActionEvent e)
{
}
}
class Handler implements Runnable
{
private Socket socket;
public Handler(Socket socket){
this.socket = socket;
}
public void run()
{
// You may need to add a repeat and exit clause here...
Server f=new Server();
try
{
InputStreamReader ir = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String message = br.readLine();
System.out.println(message);
String[] received=message.split("~");
f.console.append(received[1]+": requesting for "+received[0]+"\n");
System.out.println(received[1]+": requesting for "+received[0]+"\n");
}catch(IOException ex)
{
ex.printStackTrace();
}
}
}
客户
public class Branch extends JFrame
{
private static Socket socket;
JPanel main=new JPanel();
JPanel top=new JPanel();
JPanel bot=new JPanel();
JButton btnItem=new JButton("item");
JButton btnGlstock=new JButton("glstock");
JTextArea console=new JTextArea();
JScrollPane scrollv=new JScrollPane(console);
ActionListener item=new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String ip="";
String send="";
try
{
ip=InetAddress.getLocalHost().getHostAddress();
send="item~"+ip;
request(send);
}catch(Exception ex)
{
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
};
ActionListener glstock=new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String ip="";
String send="";
try
{
ip=InetAddress.getLocalHost().getHostAddress();
send="glstock~"+ip;
request(send);
}catch(Exception ex)
{
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
};
private void request(String send)
{
try
{
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(send+"\n");
bw.flush();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public Branch()
{
super("Branch");
scrollv.setAutoscrolls(true);
main.setLayout(new BorderLayout());
top.setLayout(new FlowLayout());
top.add(btnItem);
top.add(btnGlstock);
btnItem.addActionListener(item);
btnGlstock.addActionListener(glstock);
bot.setLayout(new BorderLayout());
console.setEditable(false);
console.setForeground(Color.white);
console.setBackground(Color.black);
bot.add(scrollv,BorderLayout.CENTER);
main.add(top,BorderLayout.NORTH);
main.add(bot,BorderLayout.CENTER);
add(main);
}
private void connect2Server() throws IOException
{
Timer timer=new Timer(5000,checkPing);
try
{
socket = new Socket(IP,port);
console.append("You are now Connected to the Server\r\n");
//timer.start();
socket.setSoTimeout(5000);
InputStreamReader isr=new InputStreamReader(socket.getInputStream());
isr.read();
}
catch(SocketTimeoutException ex)
{
ex.printStackTrace();
}
catch(IOException ex)
{
console.append("Server is offline\r\n");
ex.printStackTrace();
}
connect2Server();
}
public static void main(String args[])
{
Branch frame=new Branch();
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.connect2Server();
frame.connectDatabase();
}
}
【问题讨论】:
标签: java sockets network-programming