2014-11-25 110 views
1

我有一个服务器(用java编写),用于监听特定端口上的连接。 我有一个客户端(用C#编写),它与java服务器连接并尝试发送一些数据,但连接在服务器端出现以下错误时被重置。 “java.net.SocketException异常:连接复位”从dotnet客户端连接到java服务器时收到“java.net.SocketException:连接重置”

下面是客户端代码: -

public void ConnectServer() 
    { 
     try 
     { 
      if (Connect()) 
      { 
       Broadcast("check"); 
      } 
     } 
     catch (Exception ex) 
     { 
      Logger.Text = ex.Message; 
     } 
    } 

    private bool Connect() 
    { 
     bool flag = false; 
     try 
     { 
      if (!clientSocket.Connected) 
      { 
       clientSocket.Connect(ConfigurationManager.AppSettings["SMSRequestNotifierServer"].ToString(), Convert.ToInt32(ConfigurationManager.AppSettings["SMSRequestNotifierPort"].ToString())); 
       clientSocket.LingerState = new LingerOption(true,10); 
       isConnected = true; 
       flag = true; 
      } 
      else 
      { 
       flag = true; 
      } 
     } 
     catch (Exception ex) 
     { 
      Logger.Text = ex.Message; 
      flag = false;   } 

     return flag; 
    } 

    private void Broadcast(String msg) 
    { 
     using (NetworkStream serverStream = clientSocket.GetStream()) 
     { 
      byte[] outStream = System.Text.Encoding.ASCII.GetBytes(msg); 
      serverStream.Write(outStream, 0, outStream.Length); 
      serverStream.Flush(); 
      serverStream.Close(); 
      serverStream.Dispose(); 
      clientSocket.Close(); 
     } 
    } 

任何人都能指出我我在这段代码中我的连接越来越重做错了什么?在代码

还有一个发现是,虽然我调试这一行,应该在流写入的数据和服务器应该接受它,但没有任何反应: -

serverStream.Write(outStream, 0, outStream.Length); 

执行以下线会抛出的“java。 net.SocketException:连接重置“在服务器端。

serverStream.Flush(); 
serverStream.Close(); 
+0

你能告诉我clientSocket是如何初始化的吗? – Ehsan 2014-11-25 08:06:11

+0

让我在上面的问题 – hellowahab 2014-11-25 10:05:52

回答

0

你当发送收到此错误,不连接时。发生这种情况时,您发送到已被同行关闭的连接,以及其他可能性较低的情况。

例如:

  1. 你送的东西,对方不理解;它关闭了连接;你继续写作;这是接下来发生的事情。

或者

  • 对端送点东西给你你应该阅读,你是不是,因为你已经关闭了套接字。
  • 检查应用程序协议。

    +0

    添加代码我已经经历了几个相关的答案,但无法弄清楚我的上述代码。我甚至尝试过使用TELNET,并且它在指定的IP和端口上正常工作。 – hellowahab 2014-11-25 10:09:46

    0

    最初的问题是在My Dotnet客户端代码中,套接字在建立后立即关闭。 为此,我必须使用 http://www.codeproject.com/Articles/19071/Quick-tool-A-minimalistic-Telnet-library

    下面是解决我的问题代码

    TelnetConnection oTelnetConnection = new TelnetConnection(ConfigurationManager.AppSettings["SMSRequestNotifierServer"].ToString(), Convert.ToInt32(ConfigurationManager.AppSettings["SMSRequestNotifierPort"].ToString())); 
    Logger.Text += oTelnetConnection.Read(); 
    oTelnetConnection.WriteLine("check"); 
    
    
    
    // minimalistic telnet implementation 
    // conceived by Tom Janssens on 2007/06/06 for codeproject 
    // 
    // http://www.corebvba.be 
    
    
    
    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.Net.Sockets; 
    
    namespace MinimalisticTelnet 
    { 
        enum Verbs { 
         WILL = 251, 
         WONT = 252, 
         DO = 253, 
         DONT = 254, 
         IAC = 255 
        } 
    
        enum Options 
        { 
         SGA = 3 
        } 
    
        class TelnetConnection 
        { 
         TcpClient tcpSocket; 
    
         int TimeOutMs = 100; 
    
         public TelnetConnection(string Hostname, int Port) 
         { 
          tcpSocket = new TcpClient(Hostname, Port); 
    
         } 
    
         public string Login(string Username,string Password,int LoginTimeOutMs) 
         { 
          int oldTimeOutMs = TimeOutMs; 
          TimeOutMs = LoginTimeOutMs; 
          string s = Read(); 
          if (!s.TrimEnd().EndsWith(":")) 
           throw new Exception("Failed to connect : no login prompt"); 
          WriteLine(Username); 
    
          s += Read(); 
          if (!s.TrimEnd().EndsWith(":")) 
           throw new Exception("Failed to connect : no password prompt"); 
          WriteLine(Password); 
    
          s += Read(); 
          TimeOutMs = oldTimeOutMs; 
          return s; 
         } 
    
         public void WriteLine(string cmd) 
         { 
          Write(cmd + "\n"); 
         } 
    
         public void Write(string cmd) 
         { 
          if (!tcpSocket.Connected) return; 
          byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(cmd.Replace("\0xFF","\0xFF\0xFF")); 
          tcpSocket.GetStream().Write(buf, 0, buf.Length); 
         } 
    
         public string Read() 
         { 
          if (!tcpSocket.Connected) return null; 
          StringBuilder sb=new StringBuilder(); 
          do 
          { 
           ParseTelnet(sb); 
           System.Threading.Thread.Sleep(TimeOutMs); 
          } while (tcpSocket.Available > 0); 
          return sb.ToString(); 
         } 
    
         public bool IsConnected 
         { 
          get { return tcpSocket.Connected; } 
         } 
    
         void ParseTelnet(StringBuilder sb) 
         { 
          while (tcpSocket.Available > 0) 
          { 
           int input = tcpSocket.GetStream().ReadByte(); 
           switch (input) 
           { 
            case -1 : 
             break; 
            case (int)Verbs.IAC: 
             // interpret as command 
             int inputverb = tcpSocket.GetStream().ReadByte(); 
             if (inputverb == -1) break; 
             switch (inputverb) 
             { 
              case (int)Verbs.IAC: 
               //literal IAC = 255 escaped, so append char 255 to string 
               sb.Append(inputverb); 
               break; 
              case (int)Verbs.DO: 
              case (int)Verbs.DONT: 
              case (int)Verbs.WILL: 
              case (int)Verbs.WONT: 
               // reply to all commands with "WONT", unless it is SGA (suppres go ahead) 
               int inputoption = tcpSocket.GetStream().ReadByte(); 
               if (inputoption == -1) break; 
               tcpSocket.GetStream().WriteByte((byte)Verbs.IAC); 
               if (inputoption == (int)Options.SGA) 
                tcpSocket.GetStream().WriteByte(inputverb == (int)Verbs.DO ? (byte)Verbs.WILL:(byte)Verbs.DO); 
               else 
                tcpSocket.GetStream().WriteByte(inputverb == (int)Verbs.DO ? (byte)Verbs.WONT : (byte)Verbs.DONT); 
               tcpSocket.GetStream().WriteByte((byte)inputoption); 
               break; 
              default: 
               break; 
             } 
             break; 
            default: 
             sb.Append((char)input); 
             break; 
           } 
          } 
         } 
        } 
    } 
    
    相关问题