服务端:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.event.AncestorListener;
import java.io.*;
import java.net.*;public class server {
private JTextArea recevieArea;
private JTextArea sentArea;
private JButton submitbutton;
private static ServerSocket serverSocket;
private static Socket socket;
private BufferedReader br;
private DataOutputStream out;
private String s = "";
private JButton submit; public server() {
} class frame extends JFrame {
public frame() {
setResizable(false);
setVisible(true);
this.setBounds(300, 200, 350, 300);
this.setTitle("Server");
panel p = new panel();
Container c = getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
c.add(p);
show();
repaint(); }
} class panel extends JPanel {
public panel() { this.setBorder(null); submitbutton = new JButton("sent");
submitbutton.setBounds(236, 200, 66, 35);
add(submitbutton);
setLayout(null);
submitbutton.addActionListener(new lis()); final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 220, 99);
add(scrollPane); recevieArea = new JTextArea(5, 20);
scrollPane.setViewportView(recevieArea);
recevieArea.setLineWrap(true); final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 134, 220, 101);
add(scrollPane_1); sentArea = new JTextArea(5, 20);
scrollPane_1.setViewportView(sentArea);
sentArea.setLineWrap(true); submit = new JButton();
submit.setText("连接");
submit.setBounds(236, 159, 66, 35);
add(submit);
}
} class lis implements ActionListener { public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == submit) { serverSocket = new ServerSocket(5678);
socket = serverSocket.accept();
System.out.print("link ok");
}
if (e.getSource() == submitbutton) {
s = sentArea.getText().trim();
recevieArea.append(s + "\n");
sentArea.setText("");
out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(recevieArea.getText());
}
} catch (Exception ee) {
System.out.print(ee.getMessage());
}
}
} public static void main(String args[]) throws Exception {
new server().new frame(); }
}客户端:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.AncestorListener;
import java.io.*;
import java.net.*;public class client {
private JTextArea recevieArea;
private JTextArea sentArea;
private JButton submitbutton;
private Socket socket;
private PrintWriter pw;
private DataOutputStream dos;
private String s="";
private JButton submit;
public client(){
}
class frame extends JFrame{
public frame(){
setResizable(false);
setVisible(true);
this.setBounds(300,200,350,300);
this.setTitle("Client");
panel p = new panel();
Container c = getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
c.add(p);
show();
repaint();
}
}
class lis implements ActionListener { public void actionPerformed(ActionEvent e) {
if(e.getSource()==submit){
try{
socket=new Socket("127.0.0.1",5678);
System.out.print("link ok");
}catch(Exception ee){
System.out.print(ee.getMessage());
}
}
}
}
class panel extends JPanel{
public panel(){
this.setBorder(null);
submitbutton = new JButton("sent");
submitbutton.setBounds(236, 200, 66, 35);
add(submitbutton);
setLayout(null);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 220, 99);
add(scrollPane);
recevieArea = new JTextArea(5,20);
scrollPane.setViewportView(recevieArea);
recevieArea.setLineWrap(true); final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 134, 220, 101);
add(scrollPane_1);
sentArea = new JTextArea(5,20);
scrollPane_1.setViewportView(sentArea);
sentArea.setLineWrap(true);
submit = new JButton();
submit.setText("连接");
submit.setBounds(236, 159, 66, 35);
add(submit);
submit.addActionListener(new lis());
}
} public static void main(String args []){
new client().new frame();
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.event.AncestorListener;
import java.io.*;
import java.net.*;public class server {
private JTextArea recevieArea;
private JTextArea sentArea;
private JButton submitbutton;
private static ServerSocket serverSocket;
private static Socket socket;
private BufferedReader br;
private DataOutputStream out;
private String s = "";
private JButton submit; public server() {
} class frame extends JFrame {
public frame() {
setResizable(false);
setVisible(true);
this.setBounds(300, 200, 350, 300);
this.setTitle("Server");
panel p = new panel();
Container c = getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
c.add(p);
show();
repaint(); }
} class panel extends JPanel {
public panel() { this.setBorder(null); submitbutton = new JButton("sent");
submitbutton.setBounds(236, 200, 66, 35);
add(submitbutton);
setLayout(null);
submitbutton.addActionListener(new lis()); final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 220, 99);
add(scrollPane); recevieArea = new JTextArea(5, 20);
scrollPane.setViewportView(recevieArea);
recevieArea.setLineWrap(true); final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 134, 220, 101);
add(scrollPane_1); sentArea = new JTextArea(5, 20);
scrollPane_1.setViewportView(sentArea);
sentArea.setLineWrap(true); submit = new JButton();
submit.setText("连接");
submit.setBounds(236, 159, 66, 35);
add(submit);
}
} class lis implements ActionListener { public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == submit) { serverSocket = new ServerSocket(5678);
socket = serverSocket.accept();
System.out.print("link ok");
}
if (e.getSource() == submitbutton) {
s = sentArea.getText().trim();
recevieArea.append(s + "\n");
sentArea.setText("");
out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(recevieArea.getText());
}
} catch (Exception ee) {
System.out.print(ee.getMessage());
}
}
} public static void main(String args[]) throws Exception {
new server().new frame(); }
}客户端:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.AncestorListener;
import java.io.*;
import java.net.*;public class client {
private JTextArea recevieArea;
private JTextArea sentArea;
private JButton submitbutton;
private Socket socket;
private PrintWriter pw;
private DataOutputStream dos;
private String s="";
private JButton submit;
public client(){
}
class frame extends JFrame{
public frame(){
setResizable(false);
setVisible(true);
this.setBounds(300,200,350,300);
this.setTitle("Client");
panel p = new panel();
Container c = getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
c.add(p);
show();
repaint();
}
}
class lis implements ActionListener { public void actionPerformed(ActionEvent e) {
if(e.getSource()==submit){
try{
socket=new Socket("127.0.0.1",5678);
System.out.print("link ok");
}catch(Exception ee){
System.out.print(ee.getMessage());
}
}
}
}
class panel extends JPanel{
public panel(){
this.setBorder(null);
submitbutton = new JButton("sent");
submitbutton.setBounds(236, 200, 66, 35);
add(submitbutton);
setLayout(null);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 220, 99);
add(scrollPane);
recevieArea = new JTextArea(5,20);
scrollPane.setViewportView(recevieArea);
recevieArea.setLineWrap(true); final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 134, 220, 101);
add(scrollPane_1);
sentArea = new JTextArea(5,20);
scrollPane_1.setViewportView(sentArea);
sentArea.setLineWrap(true);
submit = new JButton();
submit.setText("连接");
submit.setBounds(236, 159, 66, 35);
add(submit);
submit.addActionListener(new lis());
}
} public static void main(String args []){
new client().new frame();
}
}
{
String name=null; //用来存储客户端连接后的name信息
DataInputStream dis=null; //用来实现客户端接受服务器数据的输入流
PrintStream ps=null; //用来实现从客户端发送数据到服务器的打印流
Socket socket=null; //用来存储客户端的socket信息
chatApplet parent=null; //用来存储当前运行的chatApplet实例 public Listen(chatApplet p,String n,Socket s) //Listen类的构造器
{
//接受参数
parent=p;
name=n;
socket=s; try
{
//实例化两个数据流
dis=new DataInputStream(s.getInputStream());
ps=new PrintStream(s.getOutputStream()); }
catch(IOException e)
{
System.out.println("Error:"+e);
parent.disconnect();
}
} //end of Listen constractor
public void run() //线程运行方法
{
String msg=null;
while(true)
{
try{msg=dis.readLine();} //读取从服务器传来的信息
catch(IOException e)
{
System.out.println("Error:"+e);
parent.disconnect();
}
if (msg==null) //如果从服务器传来的信息为空则断开此次连接
{
parent.listen=null;
parent.soc=null;
parent.list1.clear();
return;
}
StringTokenizer st=new StringTokenizer(msg,":"); //用StringTokenizer类来实现读取分段字符
String keyword=st.nextToken(); //读取信息头即关键字用来识别是何种信息 if(keyword.equals("PEOPLE")) //如果是PEOPLE则是服务器发来的客户连接信息
//主要用来刷新客户端的用户列表
{
parent.list1.clear();
while(st.hasMoreTokens()) //遍历st取得目前所连接的客户
{
String str=st.nextToken();
parent.list1.addItem(str);
}
}
else if(keyword.equals("MSG")) //如果关键字是MSG则是服务器传来的聊天信息
//主要用来刷新客户端聊天信息区将每个客户的聊天内容显示出来
{
String usr=st.nextToken();
parent.chat_txt.appendText(usr);
parent.chat_txt.appendText(st.nextToken("\0"));
parent.chat_txt.appendText("\n\n");
}
else if(keyword.equals("QUIT")) //如果关键字是QUIT则是服务器关闭的信息
//用来切断此次连接
{
System.out.println("Quit");
try
{parent.listen.stop();
parent.listen=null;
parent.soc.close();
parent.soc=null;
}catch(IOException e)
{
System.out.println("Error:"+e);
}
parent.list1.clear();
return;
}
} } //end of run method
} //end of Listen inner class
这是我以前写的,可能对你有所帮助
mport java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos =null;
TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); public static void main(String[] args) {
new ChatClient().launch();
} public void launch() {
setLocation(300, 400);
setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connection();
} public void connection() {
try {
s = new Socket("192.168.0.55", 8888);
dos = new DataOutputStream(s.getOutputStream()); //你所要求的流!
System.out.println("connected");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
public void disconnect(){
try {
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener { public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
taContent.setText(str);
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
// dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} }}