package chat;import java.io.*;
import java.net.*;
import java.util.*;public class Server implements Runnable{

private final int PORT = 8888;
private ServerSocket ss;
private Vector<Client> vClient = new Vector<Client>();

public static void main(String[] args) {

new Thread(new Server()).start();
}

Server() {
try {
ss = new ServerSocket(PORT);
//bStarted = true;
} catch (IOException e) {
System.out.println("can't create the server!");
System.exit(0);
}
}



public void run(){
while (true) {
System.out.println("waiting for client...");
try {
Socket s = ss.accept();
System.out.println("a client has connected!");

Client client = new Client(s);
vClient.add(client);
System.out.println("successfully create connection!");
System.out.println("\n");

/**
while (true) {
System.out.println(dis.readUTF());
}
*/
} catch (IOException e) {
System.out.println("System error!");
} }
}}这是我写的socket代码,现在问题是当两个客户端运行的时候,就无法实现通信,服务端卡在那里,实在是不理解,谁帮我看看,再次谢过了。

解决方案 »

  1.   

    dis是什么啊?我整个网页就看到一个 dis 变量啊
      

  2.   


    package chat;import java.io.*;
    import java.net.*;
    import java.util.*;public class Server implements Runnable { private final int PORT = 8888; private ServerSocket ss; private Vector<Client> vClient = new Vector<Client>(); public static void main(String[] args) { new Thread(new Server()).start();
    } Server() {
    try {
    ss = new ServerSocket(PORT);
    // bStarted = true;
    } catch (IOException e) {
    System.out.println("can't create the server!");
    System.exit(0);
    }
    } public void run() {
    while (true) {
    System.out.println("waiting for client...");
    try {
    Socket s = ss.accept();
    System.out.println("a client has connected!"); Client client = new Client(s);
    vClient.add(client);
    System.out.println("successfully create connection!");
    System.out.println("\n"); /**
     * while (true) { System.out.println(dis.readUTF()); }
     */
    } catch (IOException e) {
    System.out.println("System error!");
    } }
    }}
      

  3.   

    不过也不用多看,问题肯定是在这里的,
    这个Server的结构有问题,try {
    Socket s = ss.accept();
    System.out.println("a client has connected!");Client client = new Client(s);
    vClient.add(client);
    System.out.println("successfully create connection!");
    System.out.println("\n");/**
    while (true) {
    System.out.println(dis.readUTF());
    }
    */
    }因为ServerSocket是同步阻塞的。accept之后,又出现一个循环。
    如果这个地方不能结束的话,
    那么Server是无法再accept其他Client的connect的。这里正确的处理方式,再new Thread()
    把Client的send/recv操作放到这个Thread里面做。
    就OK了。
      

  4.   

    waiting for client...
    a client has connected!
    successfully create connection!
    waiting for client...
    服务端没问题..检查你的客户端代码吧...
    启动服务:
    http://localhost:8888/  上面就有反应了..
      

  5.   

    waiting for client...
    a client has connected!
    successfully create connection!
    waiting for client...
    服务端没问题..检查你的客户端代码吧...
    启动服务:
    http://localhost:8888/  上面就有反应了..
      

  6.   

    你代码都没有贴完全,所以没办法定位到底是那里出的问题,说说我的想法把。
    主要是你的Client类定义的是什么。
    看你既然放到Vector类了,应该是把客户端的信息封装的类把。
    1.如果你的Client类仅仅是封装类,那么肯定有问题了。因为是循环,所以会多次建立,ss.accept()方法会阻塞你第二次建立,正如4楼兄弟所说的。
    2.如果Client是线程类,独立处理的话,那么问题就出现在你的Client类处理客户端之间的通信代码了,我觉得你还是把Client代码贴出来让大家看看比较好。
      

  7.   

    你这里不就是实现了服务器端的监听和得到的client对象放到一个Vector中吗,都没有实现对客户端和服务器端之间的通信。而且,你最好还是在有客户端连接上来的时候创建一个线程来实现对这个客户端的操作的处理。
      

  8.   

    感谢大家这么关注我的帖子,我的Client类就是线程的,多谢4楼和8楼的提醒,我的毛病的确出现在Client类中处理通信的部分,低级错误呀,呵呵。现在已经解决了。。
      

  9.   

    不错了...我给用了CTRL+C还注释了几行代码..还输入了URL测....一分都冒得....再也不回这种低级问题了...