格式差不多就是 登录(新用户注册)-选择(留言,聊天室,新闻)-a。给某人留言(查看留言)-b。到达某个聊天室(创建一个聊天室)-c。查看某个关键字的新闻
是ASP动态网页,,,我是要交作业的……呜呜……没那么高级的……
谁帮我做一个网站,即创建一个聊天室,最好复杂一点……
答案:4 悬赏:10 手机版
解决时间 2021-02-04 16:12
- 提问者网友:献世佛
- 2021-02-04 01:25
最佳答案
- 五星知识达人网友:夜余生
- 2021-02-04 02:55
//用JAVA简单写了个聊天室的……拿着玩吧
import java.util.*;
import java.net.*;
import java.io.*;
public class Server{
public static final Vector users=new Vector();
public static void main(String args[]){
ServerSocket server=null;
try{
server=new ServerSocket(10000);
System.out.println("服务启动");
while(true){
Socket s=server.accept();
User user=new User(s);
user.start();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(server!=null){
try{server.close();}catch(Exception e){}
}
}
}
}
class User extends Thread{
private Socket s;
private String userName;
private InetAddress ip;
public User(Socket s){
this.s=s;
}
public void run(){
InputStream input=null;
OutputStream output=null;
try{
input=s.getInputStream();
output=s.getOutputStream();
BufferedReader buf=new BufferedReader(new
InputStreamReader(input));
PrintWriter pw=new
PrintWriter(new OutputStreamWriter(output),true);
pw.println("你好,请输入姓名:");
userName=buf.readLine();
ip=s.getInetAddress();
Server.users.add(this); //add online users;
sendAll(userName+"慢悠悠的来到了聊天室");
while(true){
String line=buf.readLine();
if(line==null||line.trim().length()==0){
pw.println("禁止刷屏");
continue;
}
if(line.equals("bye")){
sendAll(userName+"悻悻的离开了");
Server.users.remove(this);
break;
}
if(line.equals(":)")){
sendAll(userName+"嘿嘿干笑了一声,统统告诉我密码");
continue;
}
if(line.equals("online")){
Iterator it=Server.users.iterator();
while(it.hasNext()){
User user=(User)it.next();
pw.println(user.getIp()+"\t"+user.getUserName());
}
pw.println("=======总共"+Server.users.size()+"人");
}
sendAll(userName+"微笑着说:"+line);
}
}catch(Exception e){
}finally{
if(s!=null)try{s.close();}catch(Exception e){}
}
}
public synchronized void sendMessage(String message){
try{
PrintWriter pw=new PrintWriter(new OutputStreamWriter(
s.getOutputStream()));
pw.println(message);
pw.flush(); //flush
}catch(Exception e){}
}
public void sendAll(String message){
Iterator it=Server.users.iterator();
while(it.hasNext()){
User user=(User)it.next();
user.sendMessage(message);
}
}
public String getUserName(){
return this.userName;
}
public InetAddress getIp(){
return ip;
}
}
import java.util.*;
import java.net.*;
import java.io.*;
public class Server{
public static final Vector users=new Vector();
public static void main(String args[]){
ServerSocket server=null;
try{
server=new ServerSocket(10000);
System.out.println("服务启动");
while(true){
Socket s=server.accept();
User user=new User(s);
user.start();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(server!=null){
try{server.close();}catch(Exception e){}
}
}
}
}
class User extends Thread{
private Socket s;
private String userName;
private InetAddress ip;
public User(Socket s){
this.s=s;
}
public void run(){
InputStream input=null;
OutputStream output=null;
try{
input=s.getInputStream();
output=s.getOutputStream();
BufferedReader buf=new BufferedReader(new
InputStreamReader(input));
PrintWriter pw=new
PrintWriter(new OutputStreamWriter(output),true);
pw.println("你好,请输入姓名:");
userName=buf.readLine();
ip=s.getInetAddress();
Server.users.add(this); //add online users;
sendAll(userName+"慢悠悠的来到了聊天室");
while(true){
String line=buf.readLine();
if(line==null||line.trim().length()==0){
pw.println("禁止刷屏");
continue;
}
if(line.equals("bye")){
sendAll(userName+"悻悻的离开了");
Server.users.remove(this);
break;
}
if(line.equals(":)")){
sendAll(userName+"嘿嘿干笑了一声,统统告诉我密码");
continue;
}
if(line.equals("online")){
Iterator it=Server.users.iterator();
while(it.hasNext()){
User user=(User)it.next();
pw.println(user.getIp()+"\t"+user.getUserName());
}
pw.println("=======总共"+Server.users.size()+"人");
}
sendAll(userName+"微笑着说:"+line);
}
}catch(Exception e){
}finally{
if(s!=null)try{s.close();}catch(Exception e){}
}
}
public synchronized void sendMessage(String message){
try{
PrintWriter pw=new PrintWriter(new OutputStreamWriter(
s.getOutputStream()));
pw.println(message);
pw.flush(); //flush
}catch(Exception e){}
}
public void sendAll(String message){
Iterator it=Server.users.iterator();
while(it.hasNext()){
User user=(User)it.next();
user.sendMessage(message);
}
}
public String getUserName(){
return this.userName;
}
public InetAddress getIp(){
return ip;
}
}
全部回答
- 1楼网友:风格不统一
- 2021-02-04 05:11
大哥,首先你得有服务器,还有没人免费给你做网站。现实的社会,现实的金钱。哎!!
- 2楼网友:掌灯师
- 2021-02-04 03:56
额,,我不得不说。。。这个你得出钱。可没有人会帮你免费做这东西。而且不便宜
- 3楼网友:不甚了了
- 2021-02-04 03:34
楼主还是关闭此问题吧!这都是商业级别的了。。不会有人免费提供
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯