java即时通讯_java即时通信小例子

packagecom.chact;importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.Color;importjava.awt.Font;importjava.awt.Frame;importjava.awt.TextArea;importjava.awt.TextField;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.io.UnsupportedEncodingException;importjava.net.Socket;importjava.net.UnknownHostException;importjavax.swing.JOptionPane;importorg.json.JSONException;importorg.json.JSONObject;

@SuppressWarnings("serial")public class ChactClient extendsFrame {/*** 定义所有要使用的成员变量。

**/

public String Other = null;public Button mButton = new Button("使用名称");public static TextField mTextField = new TextField(20);public static TextField usernameTextField = new TextField(20);public static TextArea mTextArea = new TextArea(30, 30);public staticBoolean Flag;

Socket socket= null;static StringBuffer stringBuffer = newStringBuffer();public static Socket s = null;

OutputStream outputStream= null;

OutputStreamWriter writer= null;static String ChactContent = null;/***

* 定义的主方法

*

*@paramargs

*@throwsUnknownHostException

*@throwsIOException*/

public static void main(String[] args) throwsUnknownHostException, IOException {newChactClient().launchFrame();/*** 开启一个新的线程用来连接到服务器端*/Thread mThread= newThread() {

@Overridepublic voidrun() {try{

s= new Socket("123.206.76.188", 12345);

}catch(UnknownHostException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}catch(IOException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}/********************************************************************************/

/***

* 循环的从服务器读取数据

**/

while (true) {try{

InputStream iso=s.getInputStream();

InputStreamReader sior= new InputStreamReader(iso, "UTF-8");

BufferedReader bufferedReader= newBufferedReader(sior);

String string;while ((string = bufferedReader.readLine()) != null) {

System.out.println(string);//这里使用JSON数据格式进行数据的传递

JSONObject jsonObject= newJSONObject(string);//如果数据是自己发送的就不必再进行读取和显示

if (!jsonObject.getString("username").equals(usernameTextField.getText().trim())) {

mTextArea.append(jsonObject.getString("username") + ": "

+ jsonObject.getString("Content") + "\n");

mTextArea.setForeground(Color.CYAN);

stringBuffer= null;

}else{

stringBuffer= null;

}

}

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(JSONException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

};

mThread.start();

}/****************************************************************************/

/***

* 定义可见的窗口

*

*@throwsUnknownHostException

*@throwsIOException*/

public void launchFrame() throwsUnknownHostException, IOException {

Frame frame= newFrame();

frame.setTitle("开拓者即时聊天系统");

frame.setLocation(300, 300);//this.setBounds(300,300,300,300);

frame.setSize(600, 800);

frame.setResizable(false);

frame.add(mButton, BorderLayout.LINE_START);

frame.add(usernameTextField, BorderLayout.LINE_END);

frame.add(mTextArea, BorderLayout.NORTH);

frame.add(mTextField, BorderLayout.SOUTH);

mTextArea.setFont(new Font("微软雅黑", Font.PLAIN, 15));

frame.pack();

mTextField.addActionListener(newTFlistener());

mTextArea.setEditable(false);

mTextField.setEditable(false);

mButton.addActionListener(newActionListener() {/*** 首先进行用户名的读取如果为空则提示先输入用户名

*

**/@Overridepublic voidactionPerformed(ActionEvent e) {//TODO Auto-generated method stub//String Other;

Other =usernameTextField.getText().trim();

Other= Other.replace('\n', ' ');//System.out.println(Other);

if (!Other.equals("")) {

usernameTextField.setEditable(false);

mTextField.setEditable(true);

}else{

JOptionPane.showMessageDialog(null, "昵称不能为空!", "标题", JOptionPane.INFORMATION_MESSAGE);

}

}

});/***

* 定义关闭窗口的的事件监听器

**/frame.addWindowListener(newWindowAdapter() {

@Overridepublic voidwindowClosing(WindowEvent e) {//提示对方下线的JSON数据

String JSON=

"{" + "'username'" + ":" + Other + ',' +

"'Content'" + ":" + "已下线!!!" +

"}";try{

outputStream=socket.getOutputStream();

}catch(IOException e2) {//TODO Auto-generated catch block

e2.printStackTrace();

}try{

writer= new OutputStreamWriter(outputStream, "UTF-8");

}catch(UnsupportedEncodingException e2) {//TODO Auto-generated catch block

e2.printStackTrace();

}try{

writer.write(JSON+ "\n");

}catch(IOException e2) {//TODO Auto-generated catch block

e2.printStackTrace();

}try{

writer.flush();

}catch(IOException e2) {//TODO Auto-generated catch block

e2.printStackTrace();

}

System.exit(0);try{

socket.close();

destory();

}catch(IOException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}

}

});

frame.setVisible(true);//获取与服务器的连接

Contection();

}/***

* 定义与服务器连接的函数

*

*@throwsUnknownHostException

*@throwsIOException*/

public void Contection() throwsUnknownHostException, IOException {

socket= new Socket("123.206.76.188", 12345);

System.out.println("成功连接到客户端");

}/***

* 监听回车键进行数据的发送的监听器

*

*

*@authorAdministrator-SYSTEM

**/

private class TFlistener implementsActionListener {

@Overridepublic voidactionPerformed(ActionEvent e) {

ChactContent=mTextField.getText().trim();try{

String JSON=

"{" + "'username'" + ":" + Other + ',' +

"'Content'" + ":" + ChactContent +

"}";

outputStream=socket.getOutputStream();

writer= new OutputStreamWriter(outputStream, "UTF-8");

writer.write(JSON+ "\n");

writer.flush();

mTextArea.setForeground(Color.BLACK);

mTextArea.append("我说: " + ChactContent + "\n");

mTextField.setText("");

}catch(IOException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}

}

}//进行资源的释放

public void destory() throwsIOException {

outputStream.close();

writer.close();

socket.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值