一起学习
/*
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("",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("正在连接……\n");
try{
conn = new Socket(InetAddress.getByName(host),port);
in = new DataInputStream(conn.getInputStream());
out = new DataOutputStream(conn.getOutputStream());
log.append("已经连接至主机" host "端口:" port "\n");
//while((indata=in.readLine())!="") log.append(indata);
}catch(UnknownHostException un)
{
log.append("UnknownHostException:" un "\n");
JOptionPane.showMessageDialog(null, un);
}catch(IOException iod)
{
log.append("IOException:" iod "\n");
JOptionPane.showMessageDialog(null,iod);
}
}
});
act.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ac){
try{
log.append(input.getText() "\n");
out.writeBytes(input.getText());
out.writeBytes("\n");
input.setText("");
if((indata=in.readLine())!="") log.append(indata);
}catch(IOException iod)
{
log.append("IOException:" iod "\n");
JOptionPane.showMessageDialog(null,iod);
}
}
});
}catch(Exception e)
{
log.append("Exception" e);
}
}
}
下载本文示例代码
用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例用java实现nc功能实例
阅读(192) | 评论(0) | 转发(0) |