import java.io.*;
import java.net.*;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.FileChannel;
class StreamCopier implements Runnable {
private InputStream in;
private BufferedOutputStream out;
public StreamCopier(InputStream in, BufferedOutputStream out)
{
this.in = in;
this.out = out;
}
public void run()
{
try
{
int n;
byte[] buffer = new byte[4096];
while ((n = in.read(buffer)) != -1)
{
out.write(buffer, 0, n);
out.flush();
}
}
catch (IOException e)
{
System.out.println(e);
}
}
}
class InputCopier implements Runnable
{
private BufferedInputStream in;
private OutputStream out;
public InputCopier(BufferedInputStream in, OutputStream out)
{
this.in = in;
this.out = out;
}
public void run()
{
try
{
int n;
byte buffer[] = new byte[4096];
while ((n = in.read(buffer)) != -1)
{
out.write(buffer, 0, n);
out.flush();
}
out.close();
}
catch (AsynchronousCloseException e)
{
}
catch (IOException e)
{
System.out.println(e);
}
}
}
public class Test
{
private static Socket socket;
public static void main(String[] args)throws IOException, InterruptedException, NoSuchFieldException, IllegalAccessException
{
try
{
ServerSocket serverSocket=new ServerSocket(5000);
socket=serverSocket.accept();
BufferedInputStream in=new BufferedInputStream(socket.getInputStream());
BufferedOutputStream out=new BufferedOutputStream(socket.getOutputStream());
// BufferedOutputStream err=new BufferedOutputStream(socket.getErrorStream());
Process process = Runtime.getRuntime().exec("java Hello");
Thread outThread = new Thread(new StreamCopier(process.getInputStream(), out));
outThread.start();
// Thread errThread = new Thread(new StreamCopier(process.getErrorStream(), err));
// errThread.start();
Thread inThread = new Thread(new InputCopier(in, process.getOutputStream()));
inThread.start();
process.waitFor();
System.in.close();
outThread.join();
// errThread.join();
inThread.join();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
测试类包含服务器套接字程序打印输出到JTextArea中超过在Java客户端服务器网络
import java.io.*;
import java.net.*;
class User
{
private static Socket socket;
public static void main(String args[])
{
try
{
socket = new Socket("localhost",5000);
Thread t1 = new A(socket);
t1.start();
Thread t2 = new B(socket);
t2.start();
t1.join();
t2.join();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class B extends Thread
{
Socket socket;
B(Socket socket)
{
this.socket=socket;
}
public void run()
{
try
{
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
InputStream in=System.in;
int n;
byte buffer[] = new byte[4096];
while ((n = in.read(buffer)) != -1)
{
out.write(buffer, 0, n);
out.flush();
}
}
catch(Exception e)
{}
}
}
class A extends Thread
{
Socket socket;
A(Socket socket)
{
this.socket=socket;
}
public void run()
{
try
{
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
OutputStream out = System.out;
int n;
byte[] buffer = new byte[4096];
while ((n = in.read(buffer)) != -1)
{
out.write(buffer, 0, n);
out.flush();
}
}
catch(Exception e)
{}
}
}
用户类含客户端套接字程序。
的输出我想打印过程的输出到JTextArea中,并通过输入通过JTextField的处理。
UI代码。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Client {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Client window = new Client();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Client() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea txtrTextAreaFor = new JTextArea();
txtrTextAreaFor.setText("Text Area For Output of process");
txtrTextAreaFor.setBounds(36, 11, 289, 143);
frame.getContentPane().add(txtrTextAreaFor);
textField = new JTextField();
textField.setBounds(36, 165, 191, 43);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnSubmit = new JButton("Submit");
btnSubmit.setBounds(236, 185, 89, 23);
frame.getContentPane().add(btnSubmit);
}
}
使用的SwingWorker读取来自流的内容,使用发布/处理功能,从事件指派线程 – MadProgrammer
的范围内更新的文本框看一看[并发在Swing(HTTP:// docs.oracle.com/javase/tutorial/uiswing/concurrency/)和[Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html)了解更多详情 – MadProgrammer