Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android socket中:在服务器中只接收数组[]的一部分,socket中是否有最大大小?_Java_C#_Android_Sockets - Fatal编程技术网

Java 在android socket中:在服务器中只接收数组[]的一部分,socket中是否有最大大小?

Java 在android socket中:在服务器中只接收数组[]的一部分,socket中是否有最大大小?,java,c#,android,sockets,Java,C#,Android,Sockets,我在安卓系统中开发了clint,服务器是c# 1-当我以“hello world”的形式发送小字符串时,它成功地发送到服务器,并且客户端也从服务器接收到响应消息 2-现在,我尝试从发送一个大字符串(实际上它是一个图像表示) android,字符串msg的大小是23.000字节大字符串的问题是: 1- the server c# did not receive all the string bytes 2- the response Msg did not received to the cl

我在安卓系统中开发了clint,服务器是c# 1-当我以“hello world”的形式发送小字符串时,它成功地发送到服务器,并且客户端也从服务器接收到响应消息

2-现在,我尝试从发送一个大字符串(实际上它是一个图像表示) android,字符串msg的大小是23.000字节大字符串的问题是:

1- the server c# did not receive  all the string bytes 
2- the response Msg did not received to the client
两个实验中的代码都是相同的,只是字符串大小不同,请告诉我

这是android JAVA客户端唯一的连接功能: public void ConnectToServer() {

这是c#server:


我给你们最好的建议是(我很有意识地把这个作为一个“答案…”)

…住手

这里您的“直接目标”(因此也是“您的直接问题”)是:“通过套接字成功发送23KB。”

在这一点上,你正在“做一件已经做过数千次的事情”。事实上,你正在发明一个完整的协议。当然,这完全没有必要,因为无论你现在做什么,“它已经做过了。”停下来,寻找已经知道如何为您解决此问题的现有技术。(您将有大量自己的bug需要解决。因此,您可以在任何地方(!)随意获取已调试的代码。)


阿加斯:不要做已经做过的事。™ 我猜问题出在您的C#应用程序中的
int k=s.Receive(b);
行中。这是因为如此大的数据块不一定会在一个数据包中到达,这意味着对
Socket.Receive()的一次调用将无法使用整个流
。您可以尝试将其放入循环中,并一直读取所有数据,直到没有可用数据为止(流的末尾)。请查看,尤其是底部的备注:

如果您使用的是面向连接的套接字,则接收方法 将读取尽可能多的可用数据”

这就是说,如果有任何可用数据,它将立即返回该数据-它不会等待流结束,然后返回所有可用数据并将其加载到缓冲区中(因为流可能是千兆字节长的,并且可能不适合内存)。下面的示例显示了一个快速示例,说明如何开始实现以块形式接收数据并将其缓冲到MemoryStream中

Socket s = yoursocket;
MemoryStream bufferStream = new MemoryStream();
byte[] buffer = new byte[1024];
int count;
// Keep reading blocks from the stream until there is no more data being received.
// As specified in the docs, Receive will block until there is data - or the end of the
// stream has been reached.
while((count = s.Receive(buffer)) != 0)
{
    // Write all received bytes into the buffer stream
    // (which is an in memory byte stream)
    bufferStream.Write(buffer, 0, count);
}

// Get the entire byte array from the stream
byte[] entireData = bufferStream.ToArray();

// Convert the received data to a string using ascii encoding
string receivedDataAsString = System.Text.Encoding.ASCII.GetString(entireData);
作为旁注,由于您发送图像时不应使用文本编码(如ASCII编码),但这与阻止一次接收所有数据的实际问题无关。我希望这能让您了解如何着手解决问题

EDIT:Socket.Receive()在远程对等方结束流之前不会返回0。不再发送数据并不构成流的结束。因此,为了使上面的示例循环(
while((count=s.Receive(buffer))!=0)
中断,远程对等方需要完全关闭流。(在您的情况下使用
DataOutputStream.close()
)。此外,您还需要捕获Socket.Receive引发的一些异常,这些异常基于远程对等方在不同情况下显示为断开连接的情况(例如超时)。如果您希望在不等待流关闭的情况下处理数据,则需要在数据到达时使用数据-在我的示例中,直接在while循环中。例如:

while((count = s.Receive(buffer)) != 0)
    {
        // Convert the current buffer to a string (using the number of read bytes as the length of this string)
        string newText = System.Text.Encoding.ASCII.GetString(buffer, 0, count);
       //Console.WriteLine($"New data received: {newText}");
    }

对于这样大小的包,您可能无法在一次调用中接收到所有字节。您可能需要实现这样的功能:服务器被告知将接收多少字节,然后接收,直到累积了那么多字节。因此java或c代码中的问题??请您解释一下您的答案。这个问题真的浪费了我的时间输入法:(如果你的
send
呼叫成功完成,那么这可能没问题。如果你没有接收到所有字节,那么你的接收端需要进行更改。我如何知道客户是否成功发送了消息?这如何回答问题。灌洗。它如何回答?简单:指出其他人有…not“可能有”,但H-A-S……以(Java)包的形式提出了一个完整而全面的解决方案。在这个疯狂的行业中,你将积累的第一个智慧是:“不要做一件已经做过的事情。”因此,不要把垃圾桶投入到“已经做过的事情”中Moras和OP一起,为了寻求另一个解决一个老掉牙的问题的独一无二的“解决方案”,我说:“住手!”仍然看不到问题的答案,兄弟。冷静点。在从事编程行业35年后,我想我不必在被要求时“退出”。你当然可以花无限的时间创建一个“无论年轻的土耳其人程序员首先想到什么问题”,都是“独一无二的、定制的”解决方案。我希望当我说我已经删除了数千字节这样的源代码时,我不会听起来像个老骗子。直到你学会从“出现的第一个问题”中解脱出来,然后”这个问题的第一个解决方案是,“你会浪费(!)大量的时间和金钱。对于娜达来说。说真的:“这是一个兔子洞!!”D-O-N'-T…G-O…I-N!!除非你首先让自己满意,你别无选择,只能这么做。”是的,当然,套接字读取有读取限制。“但是,当你开始编造你自己的(!)…独一无二的(!!)…解决方法时,”你刚刚无意中掉进了一个蛇洞。谢谢你的回复。我试图以块的形式接收数据。通过在c#中使用断点…我们无法跳出循环我不知道。我使用了更小的字符串,而且循环仍然没有结束。我的代码中的更新是可用的
public static class Program
{
    public static void Main(string[] args)
    {
        IPAddress ipAdress = IPAddress.Parse("192.168.1.2");

        // Initializes the Listener
        TcpListener listener = new TcpListener(ipAdress, 4003);
        // Start Listeneting at the specified port
        listener.Start();

        Console.WriteLine("Server running - Port: 4003");
        Console.WriteLine("Local end point:" + listener.LocalEndpoint);
        Console.WriteLine("Waiting for connections...");

        Socket s = listener.AcceptSocket();
        // When accepted
        Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);


        MemoryStream bufferStream = new MemoryStream();
        byte[] buffer = new byte[1024];
        int count;
        //keep reading blocks from stream until there is no more data received
        //receive will block until there is data or the end of the stream has been reached
        while((count = s.Receive(buffer))!=0)
        {
            //write all received bytes into buffer stream ( which is in memory byte stream)
            bufferStream.Write(buffer, 0, count);
        }
        //get the entire brte array from the stream
        byte[] entireData = bufferStream.ToArray();
        //convert the received data to string using ascii incoding 
        String ReceivingDataString = System.Text.Encoding.ASCII.GetString(entireData);


        Console.WriteLine("Automatic message sent!");

        ///send ack

        ASCIIEncoding asen = new ASCIIEncoding();
        s.Send(asen.GetBytes("The string was recieved by the server number 1."));
        Console.WriteLine("\nSent Acknowledgement");
        // ack sent 
        s.Close();


    }
Socket s = yoursocket;
MemoryStream bufferStream = new MemoryStream();
byte[] buffer = new byte[1024];
int count;
// Keep reading blocks from the stream until there is no more data being received.
// As specified in the docs, Receive will block until there is data - or the end of the
// stream has been reached.
while((count = s.Receive(buffer)) != 0)
{
    // Write all received bytes into the buffer stream
    // (which is an in memory byte stream)
    bufferStream.Write(buffer, 0, count);
}

// Get the entire byte array from the stream
byte[] entireData = bufferStream.ToArray();

// Convert the received data to a string using ascii encoding
string receivedDataAsString = System.Text.Encoding.ASCII.GetString(entireData);
while((count = s.Receive(buffer)) != 0)
    {
        // Convert the current buffer to a string (using the number of read bytes as the length of this string)
        string newText = System.Text.Encoding.ASCII.GetString(buffer, 0, count);
       //Console.WriteLine($"New data received: {newText}");
    }