使用socket实现聊天对话

使用Socket建立客户端与服务器之间的连接
实现服务器与用户进行聊天的关键在于服务器与客户端不停的刷新,时刻接受相互传来的信息。

//客户端程序:
package text;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;



public class MyClient1 extends JFrame implements ActionListener{

JTextArea jta = null;
JTextField jtf = null;
JButton jb = null;
JPanel jpl= null;
JScrollPane jsp = null;
//把信息发给客户端的对象
PrintWriter pw =null;


public static void main(String [] args){
MyClient1 mc = new MyClient1();
}

public MyClient1(){
jta = new JTextArea();
jtf = new JTextField(20);
jb= new JButton("发送");
jb.addActionListener(this);
jpl = new JPanel();
jsp = new JScrollPane(jta);
jpl.add(jtf);
//jpl.add(jta);
jpl.add(jb);
this.add(jsp,"Center");
this.add(jpl,"South");
this.setTitle("客户端");
this.setSize(400,300);
this.setVisible(true);

try {
Socket s = new Socket("127.0.0.1",9988);
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);

pw = new PrintWriter(s.getOutputStream(),true);

while(true){
//不停地读取从服务器端发来的信息
String info = br.readLine();
jta.append("服务端:"+info+"\r\n");



}

} catch (UnknownHostException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}


}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//如果用户按下发送信息按钮
if(e.getSource()==jb){
//把服务器在框里写内容发送给客户端
String info = jtf.getText();
jta.append("客户端:"+info+"\r\n");
pw.println(info);//发送
jtf.setText("");//清空输入框
}
}
}

//服务器程序
package text;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class MyServer1 extends JFrame implements ActionListener{
/**
* 
*/
private static final long serialVersionUID = 1L;
JTextArea jta = null;
JTextField jtf = null;
JButton jb = null;
JPanel jpl= null;
JScrollPane jsp = null;
//把信息发给客户端的对象
PrintWriter pw =null;
public static void main(String [] args){
MyServer1 ms = new MyServer1();
}
public MyServer1(){
jta = new JTextArea();
jtf = new JTextField(20);
jb= new JButton("发送");
jb.addActionListener(this);
jpl = new JPanel();
jsp = new JScrollPane(jta);
jpl.add(jtf);
//jpl.add(jta);
jpl.add(jb);
this.add(jsp,"Center");
this.add(jpl,"South");
this.setTitle("server");
this.setSize(400,300);
this.setVisible(true);
//服务器监听
try {
ServerSocket ss= new ServerSocket(9988);
//等待客户端连接
Socket s = ss.accept();
//读取客户端发来的信息
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader brd = new BufferedReader(isr);
pw = new PrintWriter(s.getOutputStream(),true);
while(true){
//读取客户端信息
String info = brd.readLine();
//把客户端信息写到信息栏
jta.append("客户端:"+info+"\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void actionPerformed(ActionEvent e) {
//如果用户按下发送信息按钮
if(e.getSource()==jb){
//把服务器在框里写内容发送给客户端
String info = jtf.getText();
jta.append("服务端:"+info+"\r\n");
pw.println(info);//发送
jtf.setText("");//清空输入框
}
}

}

实现效果:
使用socket实现聊天对话_第1张图片

你可能感兴趣的:(programming)