2013-02-25 69 views
-3

我做了一个简单的应用程序连接在Java 客户端的服务器实在不行插座写不响应

我使用新的Socket(“localhost”的,4444);为客户端 和新的ServerSocket(4444);服务器

客户: http://www.hastebin.com/siqamonebu.coffee

服务器: http://www.hastebin.com/rivebetani.avrasm

我贴在hastebin的代码,因为其大,张贴在这里 我不知道坏了哪一部分,但服务器永远不会收到你说的消息

+3

'我贴在hastebin的代码,因为它能够big'。请阅读http://sscce.org – SJuan76 2013-02-25 14:12:08

+0

你真的认为我会读吗? :P – user2107534 2013-02-25 14:17:05

+1

@ user2107534你会更好。因为这里的大多数人都会拒绝回答那些不能承受“你曾经试过”和“sscce”测试的问题。 – Cubic 2013-02-25 14:19:09

回答

2

的ChatServer - 广播到所有连接的客户端

在一个命令提示符:java ChartServer
在另一:java ChatClient localhost(或服务器正在运行,其中的IP地址)
而另:java ChatClient localhost(或服务器运行的IP地址)

开始在客户端聊天OWS。

服务器这样的...

// xagyg wrote this, but you can copy it 
import java.io.*; 
import java.net.*; 
import java.util.*; 

public class ChatServer { 

    public static List list = new ArrayList(); 

    public static void main(String[] args) throws Exception { 

     ServerSocket svr = new ServerSocket(4444); 

     System.out.println("Chat Server started!"); 

     while (true) { 
      try { 
       Socket s = svr.accept(); 
       synchronized(list) { 
        list.add(s);    
       }         
       new Handler(s, list).start(); 
      } 
      catch (IOException e) { 
       // print out the error, but continue! 
       System.err.println(e); 
      } 
     } 
    } 
} 

class Handler extends Thread { 

    private Socket s; 
    private String ipaddress; 
    private List list; 

    Handler (Socket s, List list) throws Exception { 
     this.s = s; 
     ipaddress = s.getInetAddress().toString(); 
     this.list = list; 
    } 

    public void run() { 

     try { 

     BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream())); 
     String message; 
     //MyDialog x = (MyDialog)map.get(ipaddress.substring(1)); 
     while ((message = reader.readLine()) != null) { 
      if (message.equals("quit")) { 
       synchronized(list) { 
        list.remove(s); 
       } 
       break; 
      } 
      synchronized(list) { 
       for (Object object: list) { 
        Socket socket = (Socket)object; 
        if (socket==s) continue; 
        PrintWriter writer = new PrintWriter(socket.getOutputStream()); 
        writer.println(ipaddress + ": " + message); 
        writer.flush(); 
       } 
      } 
     } 
     try { reader.close(); } catch (Exception e) {} 
     } 
     catch (Exception e) { 
     System.err.println(e); 
     } 
    } 
} 

客户端这样的...

// xagyg wrote this, but you can copy it 
import java.util.*; 
import java.io.*; 
import java.net.*; 

public class ChatClient { 


    public static void main(String[] args) throws Exception { 

     Socket s = new Socket(args[0], 4444); 
     BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
     PrintWriter out = new PrintWriter(s.getOutputStream()); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
     String message; 
     new SocketReader(in).start(); 
     while ((message = reader.readLine())!=null) { 
      out.println(message); 
      out.flush(); 
      if (message.equals("quit")) break; 
     } 
     in.close(); 
     out.close(); 
    }   
} 

class SocketReader extends Thread { 

    BufferedReader in; 

    public SocketReader(BufferedReader in) { 
     this.in = in; 
    } 

    public void run() { 
     String message; 
     try { 
      while ((message = in.readLine())!=null) { 
       System.out.println(message); 
      } 
     } 
     catch (Exception e) { 
      throw new RuntimeException(e); 
     }   
    } 
} 
0

我认为这是因为你如何启动你的服务器,尝试使用这个结构,因为你正在创建运行方法内的ServerSocket,这可能会导致冲突:

public class ThreadSocketServer implements Runnable { 

    private int port; 
    private boolean isRunning; 
    private ServerSocket ss; 

    public ThreadSocketServer(int port, boolean initialStatus) { 
     this.port = port; 
     isRunning = initialStatus; 
     System.out.println("Start the server!"); 
     try { 
      ss = new ServerSocket(port); 
      Thread threadServer = new Thread(this); 
      threadServer.start(); 
     } catch (IOException e) { 
      System.err.println(e.getClass() + " - " + e.getMessage()); 
     } 
    } 

    @Override 
    public void run() { 

     while (isRunning) { 
      try { 
       Socket client = ss.accept(); 

       ObjectInputStream ois = new ObjectInputStream(
         client.getInputStream()); 
       //Process here 
       ois.close(); 
      } catch (IOException e) { 
       System.err.println(e.getClass() + " - " + e.getMessage()); 
       isRunning = false; 
      } catch (ClassNotFoundException e) { 
       System.err.println(e.getClass() + " - " + e.getMessage()); 
       isRunning = false; 
      } 
     } 

    } 

    public static void main(String[] args) { 
     new ThreadSocketServer(1085, true); 
    } 
+0

没有修复它:/ – user2107534 2013-02-25 14:35:18

+0

mmm ...你试过复制和粘贴我的代码来测试它吗?然后,也许你可以调整你的代码,并替换你的逻辑,而不是我的try块中的代码:Socket client = ...直到ois.close()。 – 2013-02-26 13:43:50