首页 \ 问答 \ 求一个java代码详细设计的例子,以接口为主的

求一个java代码详细设计的例子,以接口为主的

求一个java代码详细设计的例子,以接口为主的
更新时间:2023-07-01 06:07

最满意答案

import java.net.*;
import java.io.*;
import java.util.*;
public class ChatRoomServer {
	public static void main(String[] args) throws Exception{
		ServerSocket ss=new ServerSocket(8000);
		List sockets=new ArrayList();
		while(true){
			Socket s=ss.accept();
			sockets.add(s);
			Thread t=new ServerThread1(s,sockets);
			t.start();
		}
	}
}
class ServerThread1 extends Thread{
	Socket s;
	List sockets;
	public ServerThread1(Socket s,List sockets) {
		super();
		this.s = s;
		this.sockets=sockets;
	}
	public void run(){
		try {
			BufferedReader in=new BufferedReader(
					new InputStreamReader(s.getInputStream()) );
			while(true){
				String text=in.readLine();
				if (text==null) return;
				for(int i=0;i<sockets.size();i++){
					Socket s2=(Socket)sockets.get(i);
					PrintWriter out=new PrintWriter(s2.getOutputStream());
					out.println(text);
					out.flush();
				}

			}
		} catch (IOException e) {}
		finally{
			this.sockets.remove(s);
		}
	}
}

import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ChatRoomClient {
	JFrame frame;
	JTextField jtf;
	JTextArea jta;
	Socket s;
	BufferedReader in;
	PrintWriter out;

	public static void main(String[] args) {
		ChatRoomClient c=new ChatRoomClient();
		c.receive();
	}

	public ChatRoomClient(){
		frame=new JFrame("Chat Room");
		frame.setSize(400,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		jta=new JTextArea();
		JScrollPane jsp=new JScrollPane(jta);
		frame.add(jsp);

		jta.setEditable(false);

		jtf=new JTextField();
		frame.add(jtf,"South");

		frame.setVisible(true);

		try {
			s=new Socket("localhost",8000);
			in=new BufferedReader(new InputStreamReader(s.getInputStream()));
			out=new PrintWriter(s.getOutputStream());
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		jtf.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String text=jtf.getText();
				jtf.setText("");
				out.println(text);
				out.flush();
			}
		});
	}

	public void receive(){
		try {
			while(true){
				String text=in.readLine();
				jta.append(text+"\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
} 

先运行ChatRoomServer ,再运行ChatRoomClient

其他回答

你好!

我有简易聊天室的代码 功能不是很全 不知楼主要不?

我的回答你还满意吗~~

相关问答

更多
  • 求JAVA视频教程[2022-03-01]

    动力节点王勇老师的Java视频教程,有项目视频,有框架视频,你可以在到电驴网站搜索“动力节点”,就可以下载了 视频的简要目录如下: DRP分销资源计划项目之业务介绍 DRP分销资源计划项目之数据库设计 动力节点_DRP分销资源计划项目之HTML 动力节点_DRP分销资源计划项目之CSS 动力节点_DRP分销资源计划项目之JavaScript 动力节点_DRP分销资源计划项目之Ajax 动力节点_DRP分销资源计划项目之JSP 动力节点_DRP分销资源计划项目之Servlet 动力节点_DRP分销资源计划项目 ...
  • 手机即可破解!建议楼主下载爪哇杀手,可以说是一切通过短信收费的JAVA游戏均可破解!自己破解,无需依赖别人!还有一个修改游戏属性的软件叫八门神器,在游戏方面用处不容小觑。我已尽数说给楼主,望采纳,谢谢,
  • JAVA核心技术 (机械工业出版社) JAVA学习笔记 (清华大学出版社)
  • 求JAVA视频教程[2023-05-19]

    可以去疯狂软件教育中心的网站看一下,上面有很多JAVA教学的视频,从最基础的JAVA语言讲起,清晰易懂,可以下载来听听,肯定会对你学习有帮助的。
  • 求java基础教程[2023-09-13]

  • import java.net.*; import java.io.*; import java.util.*; public class ChatRoomServer { public static void main(String[] args) throws Exception{ ServerSocket ss=new ServerSocket(8000); List sockets=new ArrayList(); while(true){ Socket s=ss.accept(); sockets ...
  • 服务器的: package liaotian; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.S ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。