1

ソケットを介していくつかのデータを送信しようとしているため、サーバー上でバイトに変換されてから文字列に戻されます。しかし、私は明らかに1つしかできません。

サーバーコード:

static void Read(IAsyncResult ar)
{
    int fileNameLen = 1;
    //int userNameLen = 9;

    State newState = (State)ar.AsyncState; //gets state of Socket
    Socket handler = newState.Socket_w; //passes Socket to handler

    int bytesRead = handler.EndReceive(ar); //terminates Data Receive from Socket.

    if (bytesRead > 0)
    {
        if (flag == 0)
        {
            fileNameLen = BitConverter.ToInt32(newState.buffer, 0); //gets filename length
            fileName = Encoding.UTF8.GetString(newState.buffer, 4, fileNameLen); //gets filename
            //userNameLen = BitConverter.ToInt32(newState.buffer, 8);
            //getUsername = Encoding.UTF8.GetString(newState.buffer, 8, fileNameLen);

            flag++;
        }
    }    
}

クライアントコード:

internal static void uploadFile(string host, string username, string getGame, string filename, string filepath)
{
    byte[] m_clientData;
    Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    byte[] fileName = Encoding.UTF8.GetBytes(username + "_" + filename);
    byte[] fileData = File.ReadAllBytes(filepath);
    byte[] fileNameLen = BitConverter.GetBytes(fileName.Length);
    //byte[] sendUsername = Encoding.UTF8.GetBytes(username);
    //byte[] sendUsernameLen = BitConverter.GetBytes(sendUsername.Length);
    //byte[] sendGame = Encoding.UTF8.GetBytes(getGame);
    //byte[] sendGameLen = BitConverter.GetBytes(sendGame.Length);

    m_clientData = new byte[4 + fileName.Length + fileData.Length];

    fileNameLen.CopyTo(m_clientData, 0);
    fileName.CopyTo(m_clientData, 4);
    fileData.CopyTo(m_clientData, 4 + fileName.Length);

    //sendUsernameLen.CopyTo(m_clientData, 0);
    //sendUsername.CopyTo(m_clientData, 4);

    //sendGameLen.CopyTo(m_clientData, 0);
    //sendGame.CopyTo(m_clientData, 4);

    clientSock.Connect(host, 8889);
    clientSock.Send(m_clientData); //tofix exception
    clientSock.Close();
}

サーバー上で適切に復号化できないようです。バッファサイズなどについて誰か助けてもらえますか?

4

1 に答える 1

0

Read does not know anything about what was sent; TCP is basically just a stream - so there is absolutely nothing to say that you have all the data in one call to Read; you could have:

  • exactly the amount of data you sent
  • part of one message
  • 17 messages
  • the end of one message and the start of the next
  • 1 solitary byte from a message

You need to devise some kind of framing protocol that lets the receiver know when they have an entire message. That could be as simple as a length prefix, or can be more complex. You should then buffer the data in memory (or process it gradually) until you have the entire message. One call to Read is very unlikely to represent a single and complete message. Indeed, this is guaranteed not to be the case if a message is larger than your newstate.buffer, but you can get the same result even for small messages and a large buffer.

于 2013-01-02T20:37:02.983 に答える