|
楼主 |
发表于 2012-12-6 20:49:35
|
显示全部楼层
package com.example;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class ClientTest extends JFrame implements ActionListener
{
JTextField tf;
JButton button;
Socket socket;
public ClientTest()
{
tf = new JTextField();
add(tf, BorderLayout.NORTH);
button = new JButton("发送");
add(button, BorderLayout.SOUTH);
button.addActionListener(this);
}
public static void main(String[] args)
{
ClientTest frame = new ClientTest();
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Socket socket = null;
String message = tf.getText().toString();
try
{
socket = new Socket("192.168.1.1", 2001);
//PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
//out.print(message);
out.writeUTF(message);
out.close();
socket.close();
}
catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} |
|