java_net 网络聊天室及其问题解决

昨天上课听老师讲跟着敲了一遍,然后晚上回宿舍自己整理思路敲了出来,但是却没有课堂上那么顺利。

自己找了大半天对比课堂上的笔记后发现,

PrintWriter  这个类里的 print 和 println 方法有小小不一样!

上网找了下,似乎是println 比 print 输出时加多了 “\r\n”


首先创建了一个基类

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Chat extends JFrame {

	/**创建带滚动条的文本区域*/
	protected JTextArea jta = new JTextArea();
	protected JScrollPane jsp = new JScrollPane(jta);
	
	
	protected JTextField jtf = new JTextField(20);
	protected JButton btn = new JButton("发送");
	
	
	protected JPanel  jp = new JPanel();
	
	protected Socket s ;
	protected PrintWriter pw;
	protected BufferedReader br;
	
	public Chat(String str){
		this.setTitle(str);
		
		
		this.initPane();
		
		
		
		this.setVisible(true);
		this.setSize(400,400);
		this.setDefaultCloseOperation(3);
	}
	
	protected void initPane() {
		this.add(jsp);
		this.jp.add(jtf);
		this.jp.add(btn);
	}

}


服务端


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.util.ArrayList;

public class ChatServer extends Chat {
	
	//存放句柄
	ArrayList arr = new ArrayList();
	
	public ChatServer(String str){
		
		super(str);
		
		this.init();
	}
	
	private void init() {
		try{
			ServerSocket ss = new ServerSocket(3333);
			
		while(true){
			this.s = ss.accept();

			InputStream is = s.getInputStream();
			this.br = new BufferedReader(new InputStreamReader(is));
			
			OutputStream os = s.getOutputStream();
			this.pw = new PrintWriter(os);
			
			this.arr.add(pw);
			
			new Mythread().start();
		}
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	class Mythread extends Thread{
		public void run(){
			try{
				while(true){
					String str = br.readLine();
					jta.append(str + "\r\n");
					
					for(int i=0;i<arr.size();i++){
						//取出每一个句柄,并把消息发送回去
						PrintWriter p = (PrintWriter)arr.get(i);
						p.println(str);
						p.flush();
					}
					
				}
				
			
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		new ChatServer("服务端");
	}
}

客户端

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ChatClient extends Chat{
	
	public ChatClient(String str){
		super(str);
		//添加显示面板
		this.add(jp,"South");
		
		btn.addActionListener(new Action());
		jtf.addActionListener(new Action());
		
		this.init();
		
	}
	private void init() {
		try{
			this.s = new Socket("127.0.0.1",3333);
			
			InputStream is = s.getInputStream();
			this.br = new BufferedReader(new InputStreamReader(is));
			
			OutputStream os = s.getOutputStream();
			this.pw = new PrintWriter(os);
			
			new Mythread().start();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	class Action implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			
			if(e.getID()==1001||e.getActionCommand().equals("发送")){
				String str = jtf.getText();
				pw.println(str);
				pw.flush();
				jtf.setText("");
			}
	
		}
		
	}
	
	class Mythread extends Thread{
		public void run(){
			try{
				while(true){			
					String str = br.readLine();
					jta.append(str +"\r\n");
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		new ChatClient("客户端");
	}
}



以下是运行演示



流程大概就是:

客户端把消息输入到 JTextFiled 单行文本区域中,摁回车或者是发送就能把当前消息发送到服务端

服务端再把消息发还给每一个连接上的客户端,达到信息共享。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSP课程设计 在线聊天系统 指导老师: 班 级: 学 号: 姓 名: 日 期:2010.1.8 在线聊天系统 开发设计 小组成员: 主要内容及拟解决问题:四大模块的建立,数据库的读写,聊天模块的调试,以及一些代码的调等。 课程实际要求:网络聊天工具大多数由服务器程序和客户端程序组成。主要有两种类型,一种是类似QQ,MSN类型的,需要用户安装软件才能聊天,另一种即是只需打开WEB浏览器就可以聊天的,类似QQ聊天室的功能 ,本系统的目标即模仿QQ聊天室,开发一个简单的能在机房里供大家聊天的Web程序。 摘 要 随着计算机技术飞速发展的今天, 随着Internet的普及和发展, 人们的生活和工作也越来越离不开信息网络的支持, 而聊天室是人们最常见, 最直接的网上交流的方式。本网站以网络交友为主,为广大用户提供一个借助网络进行人际交往的平台,也是网络与现实最贴近的实用型平台。本文所介绍的网络聊天系统是基于开放的web服务器应用程序开发设计的,其主要特性是能动态完成信息的传递,且具有高效的交互性,更有效的处理客户请求, 且具有更简单, 更方便的数据库访问方法, 易于维护和更新.针对以上方案,本系统采用Html、JavaScript等语言开发网站页面,用JSP、Java的技术实现动态设计,采用现在很经典frame框架,用MySQL作为系统数据库。JSP是当前网络编程的主流语言,在现代企事业单位的网络应用程序的开发中起到了重要作用。本系统是根据实际的需求而设计,通过用户ID密码的论证解决方案,对实际应用领域进行深入的调查分析,已经基本上成功地实现了设计要求,实现了语音,视频聊天等。 关键字 聊天室;JSP;Java;frame;Cookie;Application Abstract With the rapid development of computer technology today, with the Internet's popularity and development of people's life and work are increasingly inseparable from the support of the information network, while the chat room was the most common and most direct way of online communication. This site web dating mainly provide customers with a network of interpersonal relationships with the platform, but also network and the practical reality is most close to the platform. This paper described a network chat system is based on an open web server application development and design, and its main feature is dynamically complete the transmission of messages and has a highly efficient interactive, more effective handling of customer requests, and has a simpler and more easy database access methods, easy to maintain and update. view of the above program, the system uses Html, JavaScript and other language development web site pages, using JSP, Java dynamic design of the technology, using the framework is now very classic frame, using MySQL as a database. JSP is the current mainstream network programming language, in the modern enterprises in the development of web applications play an important role. This system is based on

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值