用socket做个通讯   这个是服务端  为什么就接受不到客户端发送的数据呢???
大家帮我写一个客户端吧。。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;public class imServer{

private int port=8000;
private ServerSocket server;
private ExecutorService esc;
private int pool_size=4;
public imServer() throws Exception{
server =new ServerSocket(port);
esc=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*pool_size);

}
public void server() {
Socket sc=null;
while(true){
try {
 sc=server.accept();
 try {
Thread.sleep(1000);
} catch (InterruptedException e) {

e.printStackTrace();
}
esc.execute(new Handler(sc));
} catch (IOException e) {

e.printStackTrace();
}

}
}
public static void main(String s[]){
try {
new imServer().server();
} catch (Exception e) {

e.printStackTrace();
}
}
}class Handler implements Runnable{
private Socket sc=null;
static String msg=null;
DataInputStream read;
static DataOutputStream write;
public Handler( Socket sc) throws IOException{
this.sc=sc;
 read=getReader(sc);
 write=getWriter(sc);
}
public void run(){
try{
   System.out.print(1230);
   Thread.sleep(1000);
   while(true){
   msg=read.readUTF();
  System.out.print(msg);
 new Thread(new write()).start();
   }
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(sc!=null)sc.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void send(){
if(!msg.equals(null)){
try {
write.writeUTF(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
}
private DataOutputStream getWriter(Socket sc) throws IOException{

return  new DataOutputStream(
new BufferedOutputStream(sc.getOutputStream()));
}
private DataInputStream getReader(Socket sc) throws IOException{

return new DataInputStream(new BufferedInputStream(sc
.getInputStream()));
}
}
  class write implements Runnable{
  public void run(){
  Handler.send();
  }
  
  }

解决方案 »

  1.   

    Socket 是在不擅长,呵呵能猫这个是你问的??
      

  2.   

    telnet localhost 8000这个命令就能测试了啊
      

  3.   

    楼主 参考 看看
    http://www.java114.com/content19/content758.htmlhttp://xhinker.com/2009/09/05/JavaSocket%E9%80%9A%E4%BF%A1JAVA%E5%AE%9E%E7%8E%B0.aspxhttp://www.fzs8.net/Java/J2EE/2007-05-24/3954.htmlhttp://www.jspcn.net/htmlnews/11453816491561575.html
      

  4.   

    怎么接收不到信息?你随便另外写一个测试用例跑客户端就可以啊,你不会是把客户端代码放在你new imServer().server(); 后面吧,那肯定是执行不到的public class As extends TestCase {
        public void test(){ 
         try{
    Socket socket = new Socket("127.0.0.1", 8000);
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());

    while (true) {
    out.write("hello world".getBytes());
    }
    }catch(Exception e) {

    }
    }