教你巧用PHP+MySQL搭建一个聊天室

教你巧用PHP+MySQL搭建一个聊天室,第1张

CS模式的QQ这是服务器:ChatServerjavaimport javanet;

import javaio;

public class ChatServer

{

final static int thePort=8189;

ServerSocket theServer;

ChatHandler[] chatters;

int numbers=0;

public static void main(String args[])

{

ChatServer app=new ChatServer();

apprun();

}

public ChatServer()

{

try

{

theServer=new ServerSocket(thePort);

chatters=new ChatHandler[10];

}

catch(IOException io){}

}

public void run()

{

try

{

Systemoutprintln("服务器已经建立!");

while(numbers<10)

{

Socket theSocket=theServeraccept();

ChatHandler chatHandler=new ChatHandler(theSocket,this);

chatters[numbers]=chatHandler;

numbers++;

}

}catch(IOException io){}

}

public synchronized void removeConnectionList(ChatHandler c)

{

int index=0;

for(int i=0;i<=numbers-1;i++)

if(chatters[i]==c)index=i;

for(int i=index;i<numbers-1;i++)

chatters[i]=chatters[i+1];

chatters[numbers-1]=null;

numbers--;

}

public synchronized String returnUsernameList()

{

String line="";

for(int i=0;i<=numbers-1;i++)

line=line+chatters[i]user+":";

return line;

}

public void broadcastMessage(String line)

{

Systemoutprintln("发布信息:"+line);

for(int i=0;i<=numbers-1;i++)

chatters[i]sendMessage(line);

}

}====================================================这是客户端:ChatClientjavaimport javaawt;

import javaawtevent;

import javaxswing;

import javanet;

import javaio;

public class ChatClient extends Thread implements ActionListener

{

JTextField messageField,IDField,ipField,portField;

JTextArea message,users;

JButton connect,disconnect;

String user="";

String userList[]=new String[10];

Socket theSocket;

BufferedReader in;

PrintWriter out;

boolean connected=false;

Thread thread;

public static void main(String args[])

{

JFrame frame=new JFrame("聊天室");

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

ChatClient cc=new ChatClient();

JPanel content=cccreateComponents();

framegetContentPane()add(content);

framesetSize(550,310);

framesetVisible(true);

}

public JPanel createComponents()

{

JPanel pane=new JPanel(new BorderLayout());

message=new JTextArea(10,35);

messagesetEditable(false);

JPanel paneMsg=new JPanel();

paneMsgsetBorder(BorderFactorycreateTitledBorder("聊天内容"));

paneMsgadd(message);

users=new JTextArea(10,10);

JPanel listPanel=new JPanel();

listPanelsetBorder(BorderFactorycreateTitledBorder("在线用户:"));

listPaneladd(users);

messageField=new JTextField(50);

IDField=new JTextField(5);

ipField=new JTextField("LocalHost");

portField=new JTextField("8189");

connect=new JButton("连 接");

disconnect=new JButton("断 开");

disconnectsetEnabled(false);

JPanel buttonPanel=new JPanel();

buttonPaneladd(new Label("服务器IP:"));

buttonPaneladd(ipField);

buttonPaneladd(new Label("端口:"));buttonPaneladd(portField);

buttonPaneladd(new Label("用户名:"));

buttonPaneladd(IDField);

buttonPaneladd(connect);

buttonPaneladd(disconnect);

paneadd(messageField,"South");

paneadd(buttonPanel,"North");

paneadd(paneMsg,"Center");

paneadd(listPanel,"West");

connectaddActionListener(this);

disconnectaddActionListener(this);

messageFieldaddActionListener(this);

IDFieldaddActionListener(this);

ipFieldaddActionListener(this);

return pane;

}

public void actionPerformed(ActionEvent e)

{

if(egetSource()==connect){

user=IDFieldgetText();

String ip=ipFieldgetText();

int port =IntegerparseInt(portFieldgetText());

if(!userequals("")&&connectToServer(ip,port,user))

{

disconnectsetEnabled(true);

connectsetEnabled(false);

}

}

if(egetSource()==disconnect)disconnectFromServer();

if(egetSource()==messageField)

if(theSocket!=null)

{

outprintln("MESSAGE:"+messageFieldgetText());

messageFieldsetText("");

}

}

public void disconnectFromServer()

{

if(theSocket!=null)

{

try

{

connected=false;

outprintln("LEAVE:"+user);

disconnectsetEnabled(false);

connectsetEnabled(true);

thread=null;

theSocketclose();

}catch(IOException io){}

theSocket=null;

messagesetText("");

userssetText("");

}

}

public boolean connectToServer(String ip,int port,String ID)

{

if(theSocket!=null)

return false;

try

{

theSocket=new Socket(ip,port);

in=new BufferedReader(new InputStreamReader(theSocketgetInputStream()));

out=new PrintWriter(new OutputStreamWriter(theSocketgetOutputStream()),true);

outprintln("USER:"+user);

messagesetText("");

connected=true;

thread=new Thread(this);

threadstart();

}catch(Exception e){return false;}

return true;

}

public void extractMessage(String line)

{

Systemoutprintln(line);

Message messageline;

messageline=new Message(line);

if(messagelineisValid())

{

if(messagelinegetType()equals("JOIN"))

{

user=messagelinegetBody();

messageappend(user+"进入了聊天室\n");

}

else if(messagelinegetType()equals("LIST"))

updateList(messagelinegetBody());

else if(messagelinegetType()equals("MESSAGE"))

messageappend(messagelinegetBody()+"\n");

else if(messagelinegetType()equals("REMOVE"))

messageappend(messagelinegetBody()+"离开了聊天室\n");

}

else

messageappend("出现问题:"+line+"\n");

}

public void updateList(String line)

{

userssetText("");

String str=line;

for(int i=0;i<10;i++)

userList[i]="";

int index=strindexOf(":");

int a=0;

while(index!=-1){

userList[a]=strsubstring(0,index);

str=strsubstring(index+1);

a++;

index=strindexOf(":");

}

for(int i=0;i<10;i++)

usersappend(userList[i]+"\n");

}

public void run(){

try{

String line="";

while(connected && line!=null){

line=inreadLine();

if(line!=null) extractMessage(line);

}

}catch(IOException e){}

}

} =======================================================import javanet;

import javaio;

class ChatHandler extends Thread{

Socket theSocket;

BufferedReader in;

PrintWriter out;

int thePort;

ChatServer parent;

String user="";

boolean disconnect=false;

public ChatHandler(Socket socket,ChatServer parent){

try{

theSocket=socket;

thisparent=parent;

in=new BufferedReader(new InputStreamReader(theSocketgetInputStream()));

out=new PrintWriter(new OutputStreamWriter(theSocketgetOutputStream()),true);

thePort=theSocketgetPort();

start();

}catch(IOException io){}

}

public void sendMessage(String line){

outprintln(line);

}

public void setupUserName(String setname){

user=setname;

//Systemoutprint(user+"参加");

parentbroadcastMessage("JOIN:"+user);

}

public void extractMessage(String line){

Message messageline;

messageline = new Message(line);

if(messagelineisValid()){

if(messagelinegetType()equals("USER")){

setupUserName(messagelinegetBody());

parentbroadcastMessage("LIST:"+parentreturnUsernameList());

}

else if(messagelinegetType()equals("MESSAGE")){

parentbroadcastMessage("MESSAGE:"+user+"说: "+messagelinegetBody());

}

else if(messagelinegetType()equals("LEAVE")){

String c=disconnectClient();

parentbroadcastMessage("REMOVE:"+c);

parentbroadcastMessage("LIST:"+parentreturnUsernameList());

}

}

else

sendMessage("命令不存在!");

}

public String disconnectClient(){

try{

inclose();

outclose();

theSocketclose();

parentremoveConnectionList(this);

disconnect=true;

}catch(Exception ex){}

return user;

}

public void run(){

String line,name;

boolean valid=false;

try{

while((line=inreadLine())!=null){

Systemoutprintln("收到:"+line);

extractMessage(line);

}

}catch(IOException io){}

}

}

=========================================================

Messagejavapublic class Message{

private String type;

private String body;

private boolean valid;

public Message(String messageLine){

valid=false;

type=body=null;

int pos=messageLineindexOf(":");

if(pos>=0){

type=messageLinesubstring(0,pos)toUpperCase();

body=messageLinesubstring(pos+1);

valid=true;

}

}

public Message(String type,String body){

valid=true;

thistype=type;

thisbody=body;

}

public String getType(){

return type;

}

public String getBody(){

return body;

}

public boolean isValid(){

return valid;

}

} ==================================================共有4个文件,先运行服务段端。。。 这是我以前学的时候写过的!希望能帮的上你

爆吧是一种有组织地对目标贴吧发送大量水帖、垃圾帖,以将吧内各帖挤下去,使用户不能在贴吧内正常交流的行为。本文将深入探究爆吧的起源和影响,帮助读者更好地了解该现象。

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » 教你巧用PHP+MySQL搭建一个聊天室

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情