java聊天程序(包含服务端和客户端)
2014-02-19 16:03
369 查看
Server:
public class ChatServer {
boolean stat = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();//用于存客户端
public static void main(String[] args) {
new ChatServer().start();
}
public void start(){
try {
ss = new ServerSocket(8888);
stat = true;
} catch(BindException e){ //Sever端已经运行,当重复运行时抛异常
System.out.println("端口正在使用中...");
System.out.println("请关掉相关程序并重新运行服务器!"); //还会抛别的异常,所以直接关闭窗口
System.exit(0);
} catch(IOException e) {
e.printStackTrace();
}
try{
while(stat){
Socket s = ss.accept();
System.out.println("a client connected!" ); //测试语句写在最左边,以后没用可以删除或注掉
Client c = new Client(s); //每建立一个客户端,就new一个客户端对象,启动一个线程
new Thread(c).start();
clients.add(c); //勿忘写,将每个客户端加入到容器里
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private boolean cont = false;
public Client(Socket s){
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());//初始化
dos = new DataOutputStream(s.getOutputStream());
cont = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str){ //用于发送给客户端
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this); //移除那个退出的对象
System.out.println("一个客户退出了");
//e.printStackTrace();
}
}
public void run() {
try{
while(cont){
String str = dis.readUTF(); //阻塞式方法
System.out.println(str);
for(int i=0; i<clients.size(); i++){
Client c = clients.get(i); //取客户端
c.send(str);
}
/* 另外两种方法,但不适用,它会锁定服务端
for(Iterator<Client> it = clients.iterator(); it.hasNext();){
Client c = it.next();
c.send(str);
}
Iterator<Client> it = clients.iterator();
while(it.hasNext()){
Client c = it.next();
c.send(str);
}
*/
}
} catch (EOFException e){ //readUTF()阻塞式方法,所以关闭客户端会抛异常
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
s = null;//更严格的方法,等于空就没人去用了,垃圾收集器就回收走
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Client:
public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean cont = false;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
this.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 TfListent());
setVisible(true);
connect();
tRecv.start(); //启动线程
}
public void connect(){
try {
s = new Socket("127.0.0.1",8888);//注意不要定义成Socket s,这就成了局部变量而不是成员变量了
System.out.println("connected!");
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
cont = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect(){
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
/*//无法解决readUTF阻塞式方法
try {
cont = false; //关闭线程
tRecv.join(); //合并线程,彻底让他停止
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
dos.close(); //线程停止之后才能关流,不然抛SocketException异常
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}
private class TfListent implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable{
public void run() {
try {
while(cont){
String str = dis.readUTF();
taContent.setText(taContent.getText() + str + '\n');
}
} catch (SocketException e){
System.out.println("退出了,bye!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class ChatServer {
boolean stat = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();//用于存客户端
public static void main(String[] args) {
new ChatServer().start();
}
public void start(){
try {
ss = new ServerSocket(8888);
stat = true;
} catch(BindException e){ //Sever端已经运行,当重复运行时抛异常
System.out.println("端口正在使用中...");
System.out.println("请关掉相关程序并重新运行服务器!"); //还会抛别的异常,所以直接关闭窗口
System.exit(0);
} catch(IOException e) {
e.printStackTrace();
}
try{
while(stat){
Socket s = ss.accept();
System.out.println("a client connected!" ); //测试语句写在最左边,以后没用可以删除或注掉
Client c = new Client(s); //每建立一个客户端,就new一个客户端对象,启动一个线程
new Thread(c).start();
clients.add(c); //勿忘写,将每个客户端加入到容器里
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private boolean cont = false;
public Client(Socket s){
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());//初始化
dos = new DataOutputStream(s.getOutputStream());
cont = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str){ //用于发送给客户端
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this); //移除那个退出的对象
System.out.println("一个客户退出了");
//e.printStackTrace();
}
}
public void run() {
try{
while(cont){
String str = dis.readUTF(); //阻塞式方法
System.out.println(str);
for(int i=0; i<clients.size(); i++){
Client c = clients.get(i); //取客户端
c.send(str);
}
/* 另外两种方法,但不适用,它会锁定服务端
for(Iterator<Client> it = clients.iterator(); it.hasNext();){
Client c = it.next();
c.send(str);
}
Iterator<Client> it = clients.iterator();
while(it.hasNext()){
Client c = it.next();
c.send(str);
}
*/
}
} catch (EOFException e){ //readUTF()阻塞式方法,所以关闭客户端会抛异常
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
s = null;//更严格的方法,等于空就没人去用了,垃圾收集器就回收走
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Client:
public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean cont = false;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
this.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 TfListent());
setVisible(true);
connect();
tRecv.start(); //启动线程
}
public void connect(){
try {
s = new Socket("127.0.0.1",8888);//注意不要定义成Socket s,这就成了局部变量而不是成员变量了
System.out.println("connected!");
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
cont = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect(){
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
/*//无法解决readUTF阻塞式方法
try {
cont = false; //关闭线程
tRecv.join(); //合并线程,彻底让他停止
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
dos.close(); //线程停止之后才能关流,不然抛SocketException异常
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}
private class TfListent implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable{
public void run() {
try {
while(cont){
String str = dis.readUTF();
taContent.setText(taContent.getText() + str + '\n');
}
} catch (SocketException e){
System.out.println("退出了,bye!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- Java写的TCP聊天程序,服务端收不到客户端发送的数据
- Java基础---Java---网络编程---TCP的传输、客户端和服务端的互访、建立一个文本转换器、编写一个聊天程序
- Java基础---Java---网络编程---TCP的传输、客户端和服务端的互访、建立一个文本转换器、编写一个聊天程序
- Java实现多个客户端聊天程序
- java在线聊天项目1.1版 ——开启多个客户端,分别实现注册和登录功能,使用客户端与服务端信息request机制,重构线程,将单独的登录和注册线程合并
- 详解基于java的Socket聊天程序——服务端(附demo)
- 仿QQ聊天程序SOCKET编程服务端与客户端,成功通信
- java实现客户端与服务端互传信息聊天(带界面)
- java在线聊天项目0.4版本 制作服务端接收连接,客户端连接功能 新增客户端窗口打开时光标指向下边文本域功能,使用WindowListener监听WindowAdapter
- JAVA 聊天程序(客户端)
- Java学习笔记(二)-------客户端一对多(TCP)多人聊天小程序
- C# Socket聊天程序(一个服务端,多个客户端)
- Java实现多个客户端聊天程序
- C#聊天程序服务端与客户端完整实例代码
- C# Socket聊天程序(一个服务端,多个客户端)
- java在线聊天项目0.8版 实现把服务端接收到的信息返回给每一个客户端窗口中显示功能
- JAVA笔记15__TCP服务端、客户端程序 / ECHO程序 /
- java实现的客户端向服务端上传文件的程序
- Java实现多个客户端聊天程序
- 一个Java实现的简单的多个客户端聊天程序(未测试)