2017-07-23 74 views
-1

我有一个非常奇怪的问题,如果我从服务器/客户端发送消息到服务器/客户端,他们将不会在控制台中发送。 我已经尝试了整整一天来修复它,并在如何修复它之前使用了它,但我无法找到我的问题的答案,这就是为什么我在这里问它。发送和接收Java Socket错误

My Code: 

Serversocket: 
package me.jackboyplay.sockets_server; 

import java.io.IOException; 
import java.net.ServerSocket; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author JackboyPlay 
*/ 
public class Server { 

    public static void main(String[] args) { 
     ServerSocket socket = null; 
     try { 
      socket = new ServerSocket(1243); 
      System.out.println("Gestartet..."); 
     } catch (IOException ex) { 
      Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     if(socket == null){ 
      System.out.println("NULL -> Aus"); 
      return; 
     } 
     while(true){ 
      try { 
       new Clients(socket.accept()).start(); 
      } catch (IOException ex) { 
       Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 

} 

    package me.jackboyplay.sockets_server; 

import java.io.DataInputStream; 
import java.io.IOException; 
import java.io.PrintStream; 
import java.net.Socket; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author JackboyPlay 
*/ 
public class Clients extends Thread{ 

    Socket socket; 
    DataInputStream dis; 
    PrintStream dos; 

    public Clients(Socket socket){ 
     this.socket = socket; 
     try { 
      dis = new DataInputStream(socket.getInputStream()); 
      dos = new PrintStream(socket.getOutputStream()); 
     } catch (IOException ex) { 
      Logger.getLogger(Clients.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    @Override 
    public void run(){ 
     System.out.println("Thread started..."); 
     String receive = null; 
     while(true){ 
      try { 
       receive = dis.readLine(); 
      } catch (IOException ex) { 
       Logger.getLogger(Clients.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      if(receive != null){ 
       System.out.println(receive); 
       dos.println("Out"); 
       dos.flush(); 
      } 
     } 
    } 

} 
Client: 

    package me.jackboyplay.sockets_client; 

import java.io.DataInputStream; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author JackboyPlay 
*/ 
public class Client { 

    public static void main(String[] args) throws IOException { 
     Socket socket = null; 
     try { 
      socket = new Socket("localhost", 1243); 
     } catch (IOException ex) { 
      Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     if(socket == null){ 
      return; 
     } 
     DataInputStream dis = null; 
     PrintWriter dos = null; 
     try { 
      dis = new DataInputStream(socket.getInputStream()); 
      dos = new PrintWriter(socket.getOutputStream()); 
     } catch (IOException ex) { 
      Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     String receive = null; 
     while(true){ 
      Scanner s = new Scanner(System.in); 
      try { 
       receive = dis.readLine(); 
      } catch (IOException ex) { 
       Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      if(receive != null){ 
       dos.println("Hallo"); 
       dos.flush(); 
       try { 
        receive = dis.readLine(); 
       } catch (IOException ex) { 
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); 
       } 
       System.out.println(receive); 
      } 
     } 
    } 

} 
+1

我假设你没有收到编译器错误?那么它是一个运行时错误(例外)?意外的结果?如果您在问题中显示了多个文件,请尝试将它们与一些非代码文本(如文件名)分开。如果你还没有完成,请花点时间[阅读如何提出好问题](http://stackoverflow.com/help/how-to-ask)。您应该也可以阅读Eric Lippert的[如何调试小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –

+2

您在服务器上做的第一件事是等到客户端发送一行文本。你在客户端上说的第一件事就是等到服务器发送一行文本。你实现了一个僵局。 –

回答

0

两端从插座开始接收。你陷入僵局。重新考虑你的应用协议