2011-11-22 88 views
0

可能重复期间发送一些数据:
Java socket sends some data during connection to server的Java套接字连接到服务器2

我有两个非常非常简单的Java程序 - 客户端和server.They建立通过socket连接。 服务器从流中读取字符并将其打印到控制台。

import java.net.*; 
import java.io.*; 
import java.util.Date; 
public class DataServerSIMS { 
    static final int LISTENING_PORT = 2002; 
    public static void main(String[] args) { 
     ServerSocket listener; //Checks the connection requests 
     Socket connection;  //To interact with other progs 
     Reader incoming;   //Stream 
     try { 
      listener = new ServerSocket(LISTENING_PORT); 
      TextIO.putln("Listening on port "+LISTENING_PORT); 
      while (true) { 
       connection = listener.accept(); 
       try { 
        incoming = new InputStreamReader(connection.getInputStream()); 
        while (true) { 
         int ch = incoming.read(); 
         if (ch == 1 || ch == '\r') 
          break; 
          System.out.print((char)ch); 
        } 
        System.out.println(); 
        incoming.close(); 
       } 
       catch (IOException e) { 
        TextIO.putln("Error: "+e); 
       }//end try recieving data 
      }//end while 
     } 
     catch (Exception e) { 
      TextIO.putln("Sorry, the server has shut down."); 
      TextIO.putln("Error: "+e); 
      return; 
     } 
    } 
} 

客户端只建立套接字连接。

import java.net.*; 
import java.io.*; 
import java.util.Date; 
public class TempClientSIMS { 
    //static final int LISTENING_PORT = 32007; 
    static final int LISTENING_PORT = 2002; 
    public static void main(String[] args) { 
     ServerSocket listener; //Checsk the connection requests 
     Socket connection;  //To interact with other progs 
     Socket connectionToServer = null; //Socket 
     Reader incoming;   //Stream 
     try { 

      TextIO.putln("Localhost connects to:"+LISTENING_PORT); 

      connectionToServer = new Socket ("localhost", 2002); 

      TextIO.putln("Connected "); 

      long i=0; 
      while (i<2000000000) { 
       i++; 
      } 


     } 
     catch (Exception e) { 
      TextIO.putln("Sorry, the server has shut down."); 
      TextIO.putln("Error: "+e); 
      return; 
     } 
    } 


} 

客户端不会向服务器发送任何数据。

我在PC上启动了这两个程序。 客户印刷:

Localhost connects to:2002 
Connected 
Press any key to continue . . . 

服务器打印:

Listening on port 2002 
FdsfsdfsError: java.net.SocketException: Connection reset 

为什么这个字符串 “Error: java.net.SocketException: Connection reset” 打印服务器的控制台上很明显。 但我不知道这是什么意思:“Fdsfsdfs”? 我可以防止打印这个字符串“Fdsfsdfs”吗?

回答

1

当您从服务器上的流读取时,您不检查EOS。如果读取的结果等于-1,则应该中断while循环。

事情是这样的:

while (true) { 
    int ch = incoming.read(); 
    if (ch == -1) 
     break; 

    } 

    ... 

还可以使用Thread.sleep()方法暂停执行。

+0

感谢您的回答。这真的很有用! –

1

当插座关闭时,ch将为<。你的代码无法处理。

+0

好的!谢谢!这对我也很有趣:在I/O操作执行后,如果我想稍后再执行另一个I/O操作,我应该关闭这些套接字吗? –

+0

@Vladimir如果得到<0或任何除SocketTimeoutException之外的IOException,则必须关闭它。 – EJP

+0

如果我想要执行两个连续写入,那么如何处理:OutputStream outToServerStream; outToServerStream = connectionToServer.getOutputStream(); outToServerStream.write(OutputArray); outToServerStream.flush();这是正常的做法吗?或者,在第一次写入操作之后关闭套接字连接并为下一次写入操作建立新连接会更好? –

相关问题