主聊天方
package com.king.Char;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainChat extends Frame {
TextField tf = new TextField();
TextArea ta = new TextArea();
//为了方便在没连接是时候可以拿到这个socket 设置为成员变量
Socket s=null;
DataOutputStream dos=null;
public void chatLaunch() {
this.setLocation(500, 300);
this.setSize(500, 300);
this.setVisible(true);
this.setResizable(false);
this.add(tf, BorderLayout.SOUTH);
this.add(ta, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
tf.addActionListener(new myActionListener());
// new myListener()是监听对象 就是keyLISTENER 就是KeyAdapter 就是myListener
this.addKeyListener(new myListener());
//调用连接服务器
connectServlet();
}
// KeyAdapter实现了keyLISTENER接口
private class myListener extends KeyAdapter {
// 重写需要的方法
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_ENTER:
ta.setText("i am qiang");
break;
}
}
}
private class myActionListener implements ActionListener {
// 就只有一个方法void actionPerformed(ActionEvent e) 发生操作时调用。
public void actionPerformed(ActionEvent e) {
// trim()去掉空格
String str = tf.getText().trim();
ta.setText(str);
tf.setText("");
try {
// InputStreamgetInputStream() 返回此套接字的输入流。 把str的数据流写到socket里 然后连接 服务器就可以取得数据流
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void connectServlet() {
try {
s = new Socket("localhost", 8888);
dos=new DataOutputStream(s.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void disconnect(){
try {
dos.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new MainChat().chatLaunch();
}
}
服务方:
package com.king.Char;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer {
public static void main(String[] args) {
boolean statred = false;
try {
ServerSocket ss = new ServerSocket(8888);
statred = true;
while (statred) {
boolean bConnection = false;
Socket s = ss.accept();
bConnection = true;
DataInputStream dis = new DataInputStream(s.getInputStream());
while (bConnection) {
//.readUTF()会阻塞 一直等待接收数据 使用线程解决
String str = dis.readUTF();
}
dis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//不用为类服务 写内部类即可
class myThread implements Runnable{
//每个客户端有自己的相关信心
private Socket s;
private DataInputStream dis=null;
public void run(){
}
}
}