程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 用java實現nc功能實例

用java實現nc功能實例

編輯:關於JAVA

/*

socket編程實例

一個用Java實現nc的功能的小例子

請各位高手指教:[email protected]

*/

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("正在連接……\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);

}

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved