各位帮帮忙吧 谢谢了!
主要问题是
正确的进入chat(Socket a,Socket b){} 开始提供client1,client2 通信时
client1端发来的消息 一次到达client1本身的Server线程 (这里不对 )
另一次到达进入 client2的Server线程的 public void chat(Socket a,Socket b){}里 (这里是对的)
我感觉方法有问题 : 问: 怎么确定client 发送的消息到达本身的Server 还是进入通信方法中;
//Server 代码
import java.net.*;
import java.io.*;
import java.util.*;
public class Server {
public Server() {
}
public static void main(String[] args)throws IOException{
int i=0;
ServerSocket server=new ServerSocket(7777);
Hashtable<Integer,connectServer> saveThread=new Hashtable<Integer,connectServer>();
while(true){
try{
i=i+1;
System.out.println("waiting!!!");
Socket client=server.accept();
System.out.println("accept client finished!");
Integer ID=new Integer(i);
chatServer chatserver=new chatServer(client,saveThread);
chatserver.start();
saveThread.put(ID,new connectServer(i,client)); //保存Socket 对应的key每次简单的增1
}
catch(IOException e){
System.out.println(e);
}
}
}
}
class chatServer extends Thread{
Socket s;
Hashtable<Integer,connectServer> saveThread;
BufferedReader in;
PrintWriter out;
public chatServer(Socket s,Hashtable<Integer,connectServer>saveThread)throws IOException{
this.s=s;
this.saveThread=saveThread;
in=new BufferedReader(new InputStreamReader(s.getInputStream()));//创建输入流
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);//创建输出流
}
public void run(){
out.println("begin!");
while(true){
String s1=null;
try{
s1=in.readLine();
if(s1.equals("friend"))
{// 和好友通信!!
s1=in.readLine();
Integer ID=new Integer(s1); // 即Hashtable 的key
Socket chatone=new Socket();
chatone=saveThread.get(ID).socket;//注意client端输入的ID 以免越界出错 这里没做避免越界的处理!!
chat(s,chatone); //提供两个client通信!
break;
}else{
out.println("default!!");
System.out.println("default!!");
}
}
catch(Exception ex1){
System.out.println(ex1);
}
}
}
public void chat(Socket a,Socket b){ //通信方法 提供 两个client间的通信
try{
final BufferedReader isFromClient1 = new BufferedReader(new InputStreamReader(a.getInputStream()));
final PrintWriter osToClient1=new PrintWriter(new BufferedWriter(new OutputStreamWriter(a.getOutputStream())),true);
final BufferedReader isFromClient2 = new BufferedReader(new InputStreamReader(b.getInputStream()));
final PrintWriter osToClient2=new PrintWriter(new BufferedWriter(new OutputStreamWriter(b.getOutputStream())),true);
System.out.println("here! chat begin!");
System.out.println(a);
System.out.println(b);
new Thread(){
public void run(){
while(true){
try{
String str1=isFromClient1.readLine();
if(str1!=null)
{
osToClient2.println(str1);
}
else{
System.out.println("null");
}
try
{
sleep(500);
}
catch(InterruptedException ex1){
System.out.println(ex1);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
}.start();
new Thread(){
public void run(){
while(true){
try{
String str2=isFromClient2.readLine();
if(str2!=null)
{
osToClient1.println(str2);
}
else{System.out.println("null");
}
try
{
sleep(500);
}
catch(InterruptedException ex1){
System.out.println(ex1);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
finally{
}
}
}
}.start();
}
catch(Exception e){
System.out.println(e);
}
}
}
class connectServer{
int index;
Socket socket;
public connectServer(int index,Socket socket){
this.index=index;
this.socket=socket;
}
}
[此贴子已经被作者于2006-12-26 22:59:24编辑过]