JSP虚拟主机,jsp空间,java空间,java虚拟空间JSP虚拟主机,jsp空间,java空间,java虚拟空间

用java实现nc功能实例



作者:    文章来源:
发布日期:2006年04月25日
/*
socket编程实例
一个用java实现nc的功能的小例子
请各位高手指教:shengshuai@126.com
*/

import java.net.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class fs
{
public fs()
{
fsframe Frame1 = new fsframe();

Frame1.validate();

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = Frame1.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
Frame1.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
//Frame1.setlocation(0,0);
Frame1.setDefaultCloseOperation(3);
Frame1.show();
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}catch(Exception e)
{
e.printStackTrace();
}
new fs();
}
}




class fsframe extends JFrame
{
String indata = null;
DataInputStream in;
DataOutputStream out;
Socket conn;

JLabel la = new JLabel("请输入:");

JButton lianjie = new JButton("连接");
JButton act = new JButton("执行");

JTextField hostfield = new JTextField("www.126.com",10);
JTextField portfield = new JTextField("80",2);

JTextArea log = new JTextArea(10,0);
JTextArea input = new JTextArea(4,0);

JPanel contorlpanel = new JPanel();
JPanel inputPanel = new JPanel();

JScrollPane logscrollpane = new JScrollPane();
JScrollPane inputsrollpane = new JScrollPane();

public fsframe()
{


setSize(Toolkit.getDefaultToolkit().getScreenSize().width/2,380);
try
{


this.setResizable(false);
this.getContentPane().setLayout(new BorderLayout());
this.setBackground(Color.red);

log.setText("");
log.setEditable(false);
logscrollpane.getViewport().add(log);
this.getContentPane().add(logscrollpane,BorderLayout.NORTH);

contorlpanel.setLayout(new FlowLayout());
contorlpanel.add(la);
contorlpanel.add(hostfield);
contorlpanel.add(portfield);
contorlpanel.add(lianjie);
contorlpanel.add(act);
this.getContentPane().add(contorlpanel,BorderLayout.CENTER);

inputsrollpane.getViewport().add(input);
this.getContentPane().add(inputsrollpane,BorderLayout.SOUTH);




lianjie.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

String host = hostfield.getText();
int port = Integer.parseInt(portfield.getText());

if(host.equals("")||(portfield.getText().equals("")))
JOptionPane.showMessageDialog(null, "主机和端口为空!");

log.append("正在连接…… ");

try{

conn = new Socket(InetAddress.getByName(host),port);
in = new DataInputStream(conn.getInputStream());
out = new DataOutputStream(conn.getOutputStream());
log.append("已经连接至主机"+host+"端口:"+port+" ");
//while((indata=in.readLine())!="") log.append(indata);
}catch(UnknownHostException un)
{
log.append("UnknownHostException:"+un+" ");
JOptionPane.showMessageDialog(null, un);
}catch(IOException iod)
{
log.append("IOException:"+iod+" ");
JOptionPane.showMessageDialog(null,iod);
}
}
});



act.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ac){

try{

log.append(input.getText()+" ");
out.writeBytes(input.getText());
out.writeBytes(" ");
input.setText("");
if((indata=in.readLine())!="") log.append(indata);
}catch(IOException iod)
{
log.append("IOException:"+iod+" ");
JOptionPane.showMessageDialog(null,iod);
}

}
});



}catch(Exception e)
{
log.append("Exception"+e);
}
}


}
Copyright © 2002-2012 JSPCN.net. All rights reserved.
JSP中文网    备案号:粤ICP备09171188号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号