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代码,现在问题是当两个客户端运行的时候,就无法实现通信,服务端卡在那里,实在是不理解,谁帮我看看,再次谢过了。
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代码,现在问题是当两个客户端运行的时候,就无法实现通信,服务端卡在那里,实在是不理解,谁帮我看看,再次谢过了。
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!");
} }
}}
这个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了。
a client has connected!
successfully create connection!
waiting for client...
服务端没问题..检查你的客户端代码吧...
启动服务:
http://localhost:8888/ 上面就有反应了..
a client has connected!
successfully create connection!
waiting for client...
服务端没问题..检查你的客户端代码吧...
启动服务:
http://localhost:8888/ 上面就有反应了..
主要是你的Client类定义的是什么。
看你既然放到Vector类了,应该是把客户端的信息封装的类把。
1.如果你的Client类仅仅是封装类,那么肯定有问题了。因为是循环,所以会多次建立,ss.accept()方法会阻塞你第二次建立,正如4楼兄弟所说的。
2.如果Client是线程类,独立处理的话,那么问题就出现在你的Client类处理客户端之间的通信代码了,我觉得你还是把Client代码贴出来让大家看看比较好。