日期:2014-05-20  浏览次数:20656 次

关于java socket服务器的问题
大家看一下我的代码
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()));