大家看一下我的代码
package server;import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
public class Name_Server extends JFrame implements ActionListener{
private Vector input_nodes = new Vector();
private JButton server_start;
private JButton server_quit;
private JLabel copy;
private JLabel ip;
private JLabel port;
private JTextField i_type;
private JTextField p_type;
Container contentpane;
private ServerSocket serv_soc;
public Name_Server()
{
this.setTitle("Name Server...");
this.setSize(350,300);
this.setVisible(true);
contentpane = this.getContentPane();
contentpane.setLayout(null);
copy = new JLabel("By Peng Li");
copy.setBounds(20,20,200,15);
contentpane.add(copy);
ip = new JLabel("IP:");
ip.setBounds(20,50,50,20);
contentpane.add(ip);
i_type = new JTextField();
i_type.setBounds(50,50,100,20);
contentpane.add(i_type);
port = new JLabel("PORT:");
port.setBounds(180,50,50,20);
contentpane.add(port);
p_type = new JTextField();
p_type.setBounds(220,50,100,20);
contentpane.add(p_type);
server_start = new JButton("Connect"); // Initializing the GUI Component
server_start.setBounds(20,90,80,35); // Positioning the GUI Component.
server_start.addActionListener(this);
contentpane.add(server_start);
server_quit = new JButton("Quit");
server_quit.setBounds(150,90,80,35); // Positioning the GUI Component.
server_quit.addActionListener(this);
contentpane.add(server_quit);
i_type.setText("127.0.0.1");
p_type.setText("6666");
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == server_start)
{
this.server_start.setEnabled(false);
this.listen();
}
else if(ae.getSource() == server_quit)
{
try
{
this.serv_soc.close();
}
catch(Exception e)
{
System.out.println("Server Close"+e);
}
}
}
public void listen()
{
try
{
this.serv_soc = new ServerSocket(Integer.parseInt(p_type.getText()));
while(true)
{
Socket soc = serv_soc.accept();
System.out.println("Accept");
BufferedReader name_server_in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream name_server_out = new DataOutputStream(soc.getOutputStream());
String r ;
r=name_server_in.readLine();
String div[] = r.split(",");
String replysentence =new String();
if(input_nodes.size()==0)
{
replysentence = "null";
}
else
{
for(int i=0; i<input_nodes.size(); i++)
{
if(i<input_nodes.size()-1)
replysentence = replysentence + ((Node_Info)input_nodes.get(i)).getIP()+"/"+((Node_Info)input_nodes.get(i)).getPort()+";";
else
replysentence = replysentence + ((Node_Info)input_nodes.get(i)).getIP()+"/"+((Node_Info)input_nodes.get(i)).getPort();
}
}
Node_Info node = new Node_Info();
node.setIP(div[0]);
node.setPort(div[1]);
input_nodes.add(node);
name_server_out.writeBytes(replysentence+'\n');
}
}
catch(Exception e)
{
System.out.println("Name_Server:"+e);
}
}
public static void main(String[] args) throws Exception
{
Name_Server name_server = new Name_Server();
//name_server.listen();
}
}
为什么触发开始,这个服务端好像有一种死掉的感觉,界面都没有了,关也关不掉,不知道是什么原因?请大侠帮忙~~
package server;import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
public class Name_Server extends JFrame implements ActionListener{
private Vector input_nodes = new Vector();
private JButton server_start;
private JButton server_quit;
private JLabel copy;
private JLabel ip;
private JLabel port;
private JTextField i_type;
private JTextField p_type;
Container contentpane;
private ServerSocket serv_soc;
public Name_Server()
{
this.setTitle("Name Server...");
this.setSize(350,300);
this.setVisible(true);
contentpane = this.getContentPane();
contentpane.setLayout(null);
copy = new JLabel("By Peng Li");
copy.setBounds(20,20,200,15);
contentpane.add(copy);
ip = new JLabel("IP:");
ip.setBounds(20,50,50,20);
contentpane.add(ip);
i_type = new JTextField();
i_type.setBounds(50,50,100,20);
contentpane.add(i_type);
port = new JLabel("PORT:");
port.setBounds(180,50,50,20);
contentpane.add(port);
p_type = new JTextField();
p_type.setBounds(220,50,100,20);
contentpane.add(p_type);
server_start = new JButton("Connect"); // Initializing the GUI Component
server_start.setBounds(20,90,80,35); // Positioning the GUI Component.
server_start.addActionListener(this);
contentpane.add(server_start);
server_quit = new JButton("Quit");
server_quit.setBounds(150,90,80,35); // Positioning the GUI Component.
server_quit.addActionListener(this);
contentpane.add(server_quit);
i_type.setText("127.0.0.1");
p_type.setText("6666");
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == server_start)
{
this.server_start.setEnabled(false);
this.listen();
}
else if(ae.getSource() == server_quit)
{
try
{
this.serv_soc.close();
}
catch(Exception e)
{
System.out.println("Server Close"+e);
}
}
}
public void listen()
{
try
{
this.serv_soc = new ServerSocket(Integer.parseInt(p_type.getText()));
while(true)
{
Socket soc = serv_soc.accept();
System.out.println("Accept");
BufferedReader name_server_in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream name_server_out = new DataOutputStream(soc.getOutputStream());
String r ;
r=name_server_in.readLine();
String div[] = r.split(",");
String replysentence =new String();
if(input_nodes.size()==0)
{
replysentence = "null";
}
else
{
for(int i=0; i<input_nodes.size(); i++)
{
if(i<input_nodes.size()-1)
replysentence = replysentence + ((Node_Info)input_nodes.get(i)).getIP()+"/"+((Node_Info)input_nodes.get(i)).getPort()+";";
else
replysentence = replysentence + ((Node_Info)input_nodes.get(i)).getIP()+"/"+((Node_Info)input_nodes.get(i)).getPort();
}
}
Node_Info node = new Node_Info();
node.setIP(div[0]);
node.setPort(div[1]);
input_nodes.add(node);
name_server_out.writeBytes(replysentence+'\n');
}
}
catch(Exception e)
{
System.out.println("Name_Server:"+e);
}
}
public static void main(String[] args) throws Exception
{
Name_Server name_server = new Name_Server();
//name_server.listen();
}
}
为什么触发开始,这个服务端好像有一种死掉的感觉,界面都没有了,关也关不掉,不知道是什么原因?请大侠帮忙~~
不知道你现在把你自己写那个东西弄清楚了吗?我还是劝你改改你的写代码的习惯啊,有点不爽啊!
别人不是不想看你的代码 因为你i的代码的格式写他乱了啊!我把你的代码复制 看了看,本来心情很好
想帮你看看到底有什么问题,但是你的格式真的让我很头疼,下面是我给你写的一个代码书写 格式;
以后要是有大量的声明 变量的事情,一些可以自定义的尽量有一些比较简单的字母去写,要是想这样的话
等代码很长的时候,要是出现什么错误的话,回头去找问题真的很麻烦的,所以说书写格式很重要啊!!! import java.awt.*;
import javax.swing.*; public class UserApplet extends JFrame
{
JPanel panel;
JLabel labelName;
JLabel labelPass;
JTextField texName;
JPasswordField texPass;
public UserApplet()
{
super("客户端");
panel = new JPanel();
getContentPane().add(panel);
labelName = new JLabel("用户姓名");
labelPass = new JLabel("密码");
texName = new JTextField(10);
texPass = new JPasswordField(10);
panel.add(labelName);
panel.add(texName);
panel.add(labelPass);
panel.add(texPass);
setSize(300,200);
setVisible(true);
}
public static void main(String[]args)
{
new UserApplet();
}
}要是想和成为朋友的话 就加我QQ:744313213 我的期待你们的到来!!!!!
不知道你现在把你自己写那个东西弄清楚了吗?我还是劝你改改你的写代码的习惯啊,有点不爽啊!
别人不是不想看你的代码 因为你i的代码的格式写他乱了啊!我把你的代码复制 看了看,本来心情很好
想帮你看看到底有什么问题,但是你的格式真的让我很头疼,下面是我给你写的一个代码书写 格式;
以后要是有大量的声明 变量的事情,一些可以自定义的尽量有一些比较简单的字母去写,要是想这样的话
等代码很长的时候,要是出现什么错误的话,回头去找问题真的很麻烦的,所以说书写格式很重要啊!!! import java.awt.*;
import javax.swing.*; public class UserApplet extends JFrame
{
JPanel panel;
JLabel labelName;
JLabel labelPass;
JTextField texName;
JPasswordField texPass;
public UserApplet()
{
super("客户端");
panel = new JPanel();
getContentPane().add(panel);
labelName = new JLabel("用户姓名");
labelPass = new JLabel("密码");
texName = new JTextField(10);
texPass = new JPasswordField(10);
panel.add(labelName);
panel.add(texName);
panel.add(labelPass);
panel.add(texPass);
setSize(300,200);
setVisible(true);
}
public static void main(String[]args)
{
new UserApplet();
}
}要是想和成为朋友的话 就加我QQ:744313213 我的期待你们的到来!!!!!
不知道你现在把你自己写那个东西弄清楚了吗?我还是劝你改改你的写代码的习惯啊,有点不爽啊!
别人不是不想看你的代码 因为你i的代码的格式写他乱了啊!我把你的代码复制 看了看,本来心情很好
想帮你看看到底有什么问题,但是你的格式真的让我很头疼,下面是我给你写的一个代码书写 格式;
以后要是有大量的声明 变量的事情,一些可以自定义的尽量有一些比较简单的字母去写,要是想这样的话
等代码很长的时候,要是出现什么错误的话,回头去找问题真的很麻烦的,所以说书写格式很重要啊!!! import java.awt.*;
import javax.swing.*; public class UserApplet extends JFrame
{
JPanel panel;
JLabel labelName;
JLabel labelPass;
JTextField texName;
JPasswordField texPass;
public UserApplet()
{
super("客户端");
panel = new JPanel();
getContentPane().add(panel);
labelName = new JLabel("用户姓名");
labelPass = new JLabel("密码");
texName = new JTextField(10);
texPass = new JPasswordField(10);
panel.add(labelName);
panel.add(texName);
panel.add(labelPass);
panel.add(texPass);
setSize(300,200);
setVisible(true);
}
public static void main(String[]args)
{
new UserApplet();
}
}要是想和成为朋友的话 就加我QQ:744313213 我的期待你们的到来!!!!!