不知道为什么请大家帮忙一下public class ChatServer
{
public static void main(String args[]) {
ServerSocket server = null;
Socket socket = null;
Hashtable chatList;
chatList = new Hashtable();
while (true)
{
try
{
server = new ServerSocket(6666);
}
catch (IOException e1)
{
System.out.println("Listening......");
}
try
{
socket = server.accept();
InetAddress address = socket.getInetAddress();
System.out.println("用户的IP:"+address);
}
catch (IOException e)
{
if(socket != null) {
ServerThread chatThread = new ServerThread(socket,chatList);
chatThread.start();
}
else {
continue;
}
}
}
}class ServerThread extends Thread
{
String name = null,sex = null;
Socket socket = null;
DataOutputStream out = null;
DataInputStream in = null;
Hashtable chatList = null; ServerThread(Socket t,Hashtable list) {
chatList = list;
socket = t;
try
{
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e)
{
}
} public void run() {
while (true)
{
String s = null; try
{
s = in.readUTF();
System.out.println(s);
if(s.startsWith("姓名:")) {
name = s.substring(s.indexOf(":")+1,s.indexOf("性别"));
sex = s.substring(s.lastIndexOf(":")+1);
boolean boo = chatList.containsKey(name);
if(boo==false) {
chatList.put(name,this);
Enumeration enum1 = chatList.elements();
while(enum1.hasMoreElements()) {
ServerThread th = (ServerThread)enum1.nextElement();
th.out.writeUTF("聊天者:"+name+"性别"+sex);
if(th != this) {
out.writeUTF("聊天者:"+th.name+"性别"+th.sex);
}
}
}
}
else if (s.startWith("聊天:"))
{
String message = s.substring(s.indexOf(":")+1);
Enumeration enum1 = chatList.elements();
while (enum1.hasMoreElements)
{
((ServerThread)enum1.nextElements()).out.writeUTF("聊天内容:"+message);
}
}
else if (s.startsWith("用户离线:"))
{
Enumeration enum1 = chatList.elements();
while(enum1.hasMoreElements()) {
try
{
ServerThread th = (ServerThread)enum1.nextElement();
if (th != this&&th.isAlive)
{
th.out.writeUTF("用户离线:"+name);
}
}
catch (IOException eee)
{
}
}
chatList.remove(name);
socket.close();
System.out.println(name+"用户离线");
break;
}
}
catch (IOException ee)
{
Enumeration enum1 = chatList.elements();
while (enum1.hasMoreElements())
{
try
{
ServerThread th = (ServerThread)enum1.nextElement();
if (th != this&&th.isAlive())
{
th.out.writeUTF("用户离线:"+name);
}
}
catch (IOException eee)
{
}
}
chatList.remove(name);
try
{
socket.close();
}
catch (IOException eee)
{
}
System.out.println(name+"用户离线了");
break;
}
}
}
}
{
public static void main(String args[]) {
ServerSocket server = null;
Socket socket = null;
Hashtable chatList;
chatList = new Hashtable();
while (true)
{
try
{
server = new ServerSocket(6666);
}
catch (IOException e1)
{
System.out.println("Listening......");
}
try
{
socket = server.accept();
InetAddress address = socket.getInetAddress();
System.out.println("用户的IP:"+address);
}
catch (IOException e)
{
if(socket != null) {
ServerThread chatThread = new ServerThread(socket,chatList);
chatThread.start();
}
else {
continue;
}
}
}
}class ServerThread extends Thread
{
String name = null,sex = null;
Socket socket = null;
DataOutputStream out = null;
DataInputStream in = null;
Hashtable chatList = null; ServerThread(Socket t,Hashtable list) {
chatList = list;
socket = t;
try
{
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e)
{
}
} public void run() {
while (true)
{
String s = null; try
{
s = in.readUTF();
System.out.println(s);
if(s.startsWith("姓名:")) {
name = s.substring(s.indexOf(":")+1,s.indexOf("性别"));
sex = s.substring(s.lastIndexOf(":")+1);
boolean boo = chatList.containsKey(name);
if(boo==false) {
chatList.put(name,this);
Enumeration enum1 = chatList.elements();
while(enum1.hasMoreElements()) {
ServerThread th = (ServerThread)enum1.nextElement();
th.out.writeUTF("聊天者:"+name+"性别"+sex);
if(th != this) {
out.writeUTF("聊天者:"+th.name+"性别"+th.sex);
}
}
}
}
else if (s.startWith("聊天:"))
{
String message = s.substring(s.indexOf(":")+1);
Enumeration enum1 = chatList.elements();
while (enum1.hasMoreElements)
{
((ServerThread)enum1.nextElements()).out.writeUTF("聊天内容:"+message);
}
}
else if (s.startsWith("用户离线:"))
{
Enumeration enum1 = chatList.elements();
while(enum1.hasMoreElements()) {
try
{
ServerThread th = (ServerThread)enum1.nextElement();
if (th != this&&th.isAlive)
{
th.out.writeUTF("用户离线:"+name);
}
}
catch (IOException eee)
{
}
}
chatList.remove(name);
socket.close();
System.out.println(name+"用户离线");
break;
}
}
catch (IOException ee)
{
Enumeration enum1 = chatList.elements();
while (enum1.hasMoreElements())
{
try
{
ServerThread th = (ServerThread)enum1.nextElement();
if (th != this&&th.isAlive())
{
th.out.writeUTF("用户离线:"+name);
}
}
catch (IOException eee)
{
}
}
chatList.remove(name);
try
{
socket.close();
}
catch (IOException eee)
{
}
System.out.println(name+"用户离线了");
break;
}
}
}
}
解决方案 »
- Graphics 问题
- [javascript求助] event的cancelBubble属性问题
- 我做c/s啊想用连接池啊!!!!!!!!!!!!但是没找到相关资料啊
- 请问如何根据指定大小对文件进行ZIP压缩?急
- 关于jdbc中的数据源,和JNDI
- 凯撒加密作业完成部分,高人请进来指正....
- 100分相似!!!十万火急!!利用socket如何与http服务器通讯???????
- 请教关于JConnect连接Sybase数据库的问题
- 问个很菜的问题
- 求教高手点拨“一个3W行代码工程量的Login”步骤的错误排查的经验。
- HSQL 中文乱码问题!!!
- swing KeyStroke在JTextFile与JButton中的效果为何不同?
public class ChatServer
{
public static void main(String args[]) {
ServerSocket server = null;
Socket socket = null;
Hashtable chatList;
chatList = new Hashtable(); while (true)
{
try
{
server = new ServerSocket(6666);
}
catch (IOException e1)
{
System.out.println("Listening......");
}
try
{
socket = server.accept();
InetAddress address = socket.getInetAddress();
System.out.println("用户的IP:"+address);
}
catch (IOException e)
{
if(socket != null) {
ServerThread chatThread = new ServerThread(socket,chatList);
chatThread.start();
}
else {
continue;
}
}
}
}
}//这里少了个中括号class ServerThread extends Thread
{
String name = null,sex = null;
Socket socket = null;
DataOutputStream out = null;
DataInputStream in = null;
Hashtable chatList = null; ServerThread(Socket t,Hashtable list) { chatList = list;
socket = t;
try
{
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e)
{
}
} public void run() { while (true)
{
String s = null; try
{
s = in.readUTF();
System.out.println(s);
if(s.startsWith("姓名:")) {
name = s.substring(s.indexOf(":")+1,s.indexOf("性别"));
sex = s.substring(s.lastIndexOf(":")+1);
boolean boo = chatList.containsKey(name);
if(boo==false) {
chatList.put(name,this);
Enumeration enum1 = chatList.elements();
while(enum1.hasMoreElements()) {
ServerThread th = (ServerThread)enum1.nextElement();
th.out.writeUTF("聊天者:"+name+"性别"+sex);
if(th != this) {
out.writeUTF("聊天者:"+th.name+"性别"+th.sex);
}
}
}
}
else if (s.startWith("聊天:"))
{
String message = s.substring(s.indexOf(":")+1);
Enumeration enum1 = chatList.elements();
while (enum1.hasMoreElements)
{
((ServerThread)enum1.nextElements()).out.writeUTF("聊天内容:"+message);
}
}
else if (s.startsWith("用户离线:"))
{
Enumeration enum1 = chatList.elements();
while(enum1.hasMoreElements()) {
try
{
ServerThread th = (ServerThread)enum1.nextElement();
if (th != this&&th.isAlive)
{
th.out.writeUTF("用户离线:"+name);
}
}
catch (IOException eee)
{
}
}
chatList.remove(name);
socket.close();
System.out.println(name+"用户离线");
break;
}
}
catch (IOException ee)
{
Enumeration enum1 = chatList.elements();
while (enum1.hasMoreElements())
{
try
{
ServerThread th = (ServerThread)enum1.nextElement();
if (th != this&&th.isAlive())
{
th.out.writeUTF("用户离线:"+name);
}
}
catch (IOException eee)
{
}
}
chatList.remove(name);
try
{
socket.close();
}
catch (IOException eee)
{
}
System.out.println(name+"用户离线了");
break;
}
}
}
}
你的程序还有错误!