菜鸟急求用Java实现的聊天室群聊的源代码
解决方案 »
- JAXP是如何在底层工作的
- 关于public void paint(Graphics g)
- Menu类的add()参数问题
- 关于java运行无结果的问题
- 能否用java判断本机与另一台机器的通与断
- 求问
- 求救: 在线等待 (下午2:00前需要解决)
- 通过DatagramPacket的GETDATA()得到的信息,在TEXTAERA中不能正常显示中文,但可以显示英文,郁闷中,怎样才能显示中文?
- 帮我加点注释intn=30到nj*=i;,谢了
- 请问我想编一个想windows时间/日期 属性那样的程序,请各位大侠给个思路好吗?
- 不好意思请问如何验证四则运算数学表达式的合法性?
- 帮我做个程序利用图形界面编一个类似驾驶理论考试的程序 至少包括10个题目 5个选择 5个判断
JAVA聊天室源代码
服务端:
import java.io.*;
import java.net.*; public class chatSvr
{
public static void main(String[] str)
{
ServerSocket soc;
Socket svrSoc;
ObjectOutputStream objOut=null;
ObjectInputStream objIn=null;
try{
System.out.println("Waiting for client's connecting...");
soc=new ServerSocket(6666);
svrSoc=soc.accept();
System.out.println("The client connected, you can Exit this program by type 'QUIT'");
objOut=new ObjectOutputStream(svrSoc.getOutputStream());
objIn=new ObjectInputStream(svrSoc.getInputStream());
}catch(Exception e){System.exit(0);}
sendMsgOut send=new sendMsgOut(objOut);
send.start();
getMsgFromClient get=new getMsgFromClient(objIn);
get.start();
}
}
class sendMsgOut extends Thread
{
ObjectOutputStream objOut=null;
public sendMsgOut(ObjectOutputStream out)
{
objOut=out;
}
public void run()
{
String strMsg="";
while(true)
{
try{
strMsg=(new BufferedReader(new InputStreamReader(System.in))).readLine();
objOut.writeObject(strMsg);
if (strMsg.equals("QUIT"))System.exit(0);
}catch(Exception e){}
//System.out.println(strMsg);
}
}
}
class getMsgFromClient extends Thread
{
ObjectInputStream objIn;
public getMsgFromClient(ObjectInputStream in)
{
objIn=in;
}
public void run()
{
String strMsg="";
while(true)
{
try{
strMsg=(String)objIn.readObject();
System.out.println("The client said:"+strMsg);
if (strMsg.equals("QUIT"))System.exit(0);
}catch(Exception e){}
}
}
} 客户端:
import java.io.*;
import java.net.*; public class chatClient
{
public static void main(String[] str)
{
String serverName="";
if (str.length>=1)serverName=str[0];
else {
System.out.println("You must provide a server Name you want to connect to,");
System.out.println("And run your program by input in Dos Prompt Like this:");
System.out.println("java chatClient serverName");
System.exit(0);
}
Socket cltSoc;
ObjectOutputStream objOut=null;
ObjectInputStream objIn=null;
try{
InetAddress address=InetAddress.getByName(serverName);
cltSoc=new Socket(address,6666);
objOut=new ObjectOutputStream(cltSoc.getOutputStream());
objIn=new ObjectInputStream(cltSoc.getInputStream());
System.out.println("Connected to the server successfully...");
System.out.println("If you want to exit this program, type 'QUIT' !");
}catch(Exception e){System.exit(0);}
getMsg get=new getMsg(objIn);
get.start();
sendMsg send =new sendMsg(objOut);
send.start();
}
}
class getMsg extends Thread
{ ObjectInputStream objIn;
public getMsg(ObjectInputStream in)
{
objIn=in;
}
public void run()
{
String strMsg="";
while(true)
{
try{
strMsg=(String)objIn.readObject();
System.out.println("The server said:"+strMsg);
if (strMsg.equals("QUIT"))System.exit(0);
}catch(Exception e){}
}
}
}
class sendMsg extends Thread
{
ObjectOutputStream objOut;
public sendMsg(ObjectOutputStream out)
{
objOut=out;
}
public void run()
{
String strMsg="";
while(true)
{
try{
strMsg=(new BufferedReader(new InputStreamReader(System.in))).readLine();
objOut.writeObject(strMsg);
if (strMsg.equals("QUIT"))System.exit(0);
}catch(Exception e){}
//System.out.println(strMsg);
}
}
}