java socket聊天工具_java+socket 简易聊天工具
1、服務(wù)器端程序
package com.test3;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class MyServer3 extends JFrame implements ActionListener{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyServer3();
}
JTextArea jta = null;
JTextField jtf = null;
JButton sendBtn = null;
JPanel jp = null;
JScrollPane jsp = null;
//把信息發(fā)給客戶端的對象
PrintWriter pw = null;
public MyServer3()
{
//
jta = new JTextArea();
jtf = new JTextField(20);
sendBtn = new JButton("send");
sendBtn.addActionListener(this);
sendBtn.setActionCommand("send");
jsp = new JScrollPane(jta);
jp = new JPanel();
jp.add(jtf);
jp.add(sendBtn);
this.add(jsp,BorderLayout.CENTER);
this.add(jp,BorderLayout.SOUTH);
this.setSize(400, 300);
this.setTitle("QQ簡易聊天-服務(wù)器端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
try {
//服務(wù)器監(jiān)聽
ServerSocket ss = new ServerSocket(9988);
//等待客戶端連接
Socket s = ss.accept();
BufferedReader br = new BufferedReader
(new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(s.getOutputStream(),true);
//讀取從客戶端發(fā)來的信息
while(true) {
String info = br.readLine();
jta.append("client "+getTime()+"\r\n"+info+"\r\n");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("send")) {
//把服務(wù)器在jtf寫的內(nèi)容發(fā)送給客戶端
String info = jtf.getText();
jta.append("server "+getTime()+"\r\n"+info+"\r\n");
pw.println(info);
//清空發(fā)送框內(nèi)容
jtf.setText("");
}
}
//獲取系統(tǒng)時間
public String getTime()
{
Calendar c = Calendar.getInstance();//可以對每個時間域單獨修改
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
return hour+":"+ minute+":"+second;
}
}
2、客戶端程序
package com.test3;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class MyClient3 extends JFrame implements ActionListener{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyClient3();
}
JTextArea jta = null;
JTextField jtf = null;
JButton sendBtn = null;
JPanel jp = null;
JScrollPane jsp = null;
PrintWriter pw = null;
public MyClient3()
{
//
jta = new JTextArea();
jtf = new JTextField(20);
sendBtn = new JButton("send");
sendBtn.addActionListener(this);
sendBtn.setActionCommand("send");
jsp = new JScrollPane(jta);
jp = new JPanel();
jp.add(jtf);
jp.add(sendBtn);
this.add(jsp,BorderLayout.CENTER);
this.add(jp,BorderLayout.SOUTH);
this.setSize(400, 300);
this.setTitle("QQ簡易聊天-客戶端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
try {
Socket s = new Socket("127.0.0.1",9988);
BufferedReader br = new BufferedReader
(new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(s.getOutputStream(),true);
while(true) {
//不停的讀取服務(wù)器端發(fā)來的信息
String info = br.readLine();
jta.append("server "+getTime()+"\r\n"+info+"\r\n");
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("send")) {
String info = jtf.getText();
//把客戶端發(fā)送的信息顯示到j(luò)ta
jta.append("client "+getTime()+"\r\n"+info+"\r\n");
pw.println(info);
jtf.setText("");
}
}
//獲取系統(tǒng)時間
public String getTime()
{
Calendar c = Calendar.getInstance();//可以對每個時間域單獨修改
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
return hour+":"+ minute+":"+second;
}
}
3、演示
總結(jié)
以上是生活随笔為你收集整理的java socket聊天工具_java+socket 简易聊天工具的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java web临时文件删除_什么时候删
- 下一篇: win7 docker java开发环境